mirror of
				https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
				synced 2025-11-04 04:12:45 +00:00 
			
		
		
		
	Add flask_table for admin view
This commit is contained in:
		@@ -1,4 +1,6 @@
 | 
			
		||||
from flask import render_template
 | 
			
		||||
from ..models import User
 | 
			
		||||
from ..tables import AdminUserTable, AdminUserItem
 | 
			
		||||
from . import main
 | 
			
		||||
from ..decorators import admin_required
 | 
			
		||||
from flask_login import login_required
 | 
			
		||||
@@ -13,4 +15,8 @@ def index():
 | 
			
		||||
@login_required
 | 
			
		||||
@admin_required
 | 
			
		||||
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
									
								
							
							
						
						
									
										29
									
								
								app/tables.py
									
									
									
									
									
										Normal 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'
 | 
			
		||||
@@ -64,6 +64,9 @@
 | 
			
		||||
        </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>
 | 
			
		||||
        {% 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>
 | 
			
		||||
    </header>
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,11 @@
 | 
			
		||||
{% extends "base.html.j2" %}
 | 
			
		||||
 | 
			
		||||
{% block page_content %}
 | 
			
		||||
<h1>Administration tools</h1>
 | 
			
		||||
<div class="col s12">
 | 
			
		||||
  <div class="card large">
 | 
			
		||||
    <div class="card-content">
 | 
			
		||||
      <span class="card-title">User list</span>
 | 
			
		||||
      {{ table }}
 | 
			
		||||
    </div>
 | 
			
		||||
  </div>
 | 
			
		||||
</div>
 | 
			
		||||
 
 | 
			
		||||
@@ -5,3 +5,4 @@ Flask-Migrate==2.5.2
 | 
			
		||||
Flask-SQLAlchemy==2.4.0
 | 
			
		||||
Flask-WTF==0.14.2
 | 
			
		||||
python-dotenv==0.10.3
 | 
			
		||||
Flask-Table==0.5.0
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user