Add flask_table for admin view

This commit is contained in:
Stephan Porada 2019-07-10 14:36:31 +02:00
parent eef9ec9b37
commit 4ec9e2ff41
5 changed files with 41 additions and 2 deletions

View File

@ -1,4 +1,6 @@
from flask import render_template from flask import render_template
from ..models import User
from ..tables import AdminUserTable, AdminUserItem
from . import main from . import main
from ..decorators import admin_required from ..decorators import admin_required
from flask_login import login_required from flask_login import login_required
@ -13,4 +15,8 @@ def index():
@login_required @login_required
@admin_required @admin_required
def for_admins_only(): def for_admins_only():
return "For administrators!" users = User.query.order_by(User.username).all()
items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed) for u in users]
table = AdminUserTable(items)
return render_template('main/admin.html.j2', title='Administration Tools',
table=table.__html__())

29
app/tables.py Normal file
View File

@ -0,0 +1,29 @@
from flask_table import Table, Col
class AdminUserTable(Table):
"""
Declares the table describing colum by column.
"""
classes = ['highlight', 'responsive-table']
username = Col('Username')
email = Col('Email')
role_id = Col('Role')
confirmed = Col('Confrimed Status')
class AdminUserItem(object):
"""
Describes one item like one row per table.
"""
def __init__(self, username, email, role_id, confirmed):
self.username = username
self.email = email
self.role_id = role_id
self.confirmed = confirmed
if self.role_id == 1:
self.role_id = 'User'
elif self.role_id == 2:
self.role_id = 'Admin'

View File

@ -64,6 +64,9 @@
</li> </li>
<li><a href="{{ url_for('main.index') }}"><i class="material-icons">opacity</i>Opaque</a></li> <li><a href="{{ url_for('main.index') }}"><i class="material-icons">opacity</i>Opaque</a></li>
<li><a href="{{ url_for('auth.settings') }}"><i class="material-icons">settings</i>Settings</a></li> <li><a href="{{ url_for('auth.settings') }}"><i class="material-icons">settings</i>Settings</a></li>
{% if current_user.is_administrator() %} <!-- Shows only for admins -->
<li><a href="{{ url_for('main.for_admins_only') }}"><i class="material-icons">build</i>Administration</a></li>
{% endif %}
</ul> </ul>
</header> </header>

View File

@ -1,11 +1,11 @@
{% extends "base.html.j2" %} {% extends "base.html.j2" %}
{% block page_content %} {% block page_content %}
<h1>Administration tools</h1>
<div class="col s12"> <div class="col s12">
<div class="card large"> <div class="card large">
<div class="card-content"> <div class="card-content">
<span class="card-title">User list</span> <span class="card-title">User list</span>
{{ table }}
</div> </div>
</div> </div>
</div> </div>

View File

@ -5,3 +5,4 @@ Flask-Migrate==2.5.2
Flask-SQLAlchemy==2.4.0 Flask-SQLAlchemy==2.4.0
Flask-WTF==0.14.2 Flask-WTF==0.14.2
python-dotenv==0.10.3 python-dotenv==0.10.3
Flask-Table==0.5.0