Add breadcrumbs to admin package

This commit is contained in:
Patrick Jentsch 2023-03-14 14:24:52 +01:00
parent b8e63d2342
commit 2efc9533ec
4 changed files with 50 additions and 36 deletions

View File

@ -61,6 +61,7 @@ def create_app(config: Config = Config) -> Flask:
init_error_handlers(app)
from .admin import bp as admin_blueprint
default_breadcrumb_root(admin_blueprint, '.admin')
app.register_blueprint(admin_blueprint, url_prefix='/admin')
from .api import bp as api_blueprint

View File

@ -1,5 +1,20 @@
from flask import Blueprint
from flask_login import login_required
from app.decorators import admin_required
bp = Blueprint('admin', __name__)
from . import routes
@bp.before_request
@login_required
@admin_required
def before_request():
'''
Ensures that the routes in this package can be visited only by users with
administrator privileges (login_required and admin_required).
'''
pass
from . import json_routes, routes

22
app/admin/json_routes.py Normal file
View File

@ -0,0 +1,22 @@
from flask import current_app
from threading import Thread
from app import db
from app.models import User
from . import bp
@bp.route('/users/<hashid:user_id>/delete', methods=['DELETE'])
def delete_user(user_id):
def _delete_user(app, user_id):
with app.app_context():
user = User.query.get(user_id)
user.delete()
db.session.commit()
User.query.get_or_404(user_id)
thread = Thread(
target=_delete_user,
args=(current_app._get_current_object(), user_id)
)
thread.start()
return {}, 202

View File

@ -1,34 +1,25 @@
from flask import current_app, flash, redirect, render_template, url_for
from flask_login import login_required
from threading import Thread
from flask import flash, redirect, render_template, url_for
from flask_breadcrumbs import register_breadcrumb
from app import db, hashids
from app.decorators import admin_required
from app.models import Role, User, UserSettingJobStatusMailNotificationLevel
from app.users.forms import (
EditNotificationSettingsForm
)
from app.users.forms import EditNotificationSettingsForm
from app.users.forms import EditProfileSettingsForm
from . import bp
from .forms import AdminEditUserForm
@bp.before_request
@login_required
@admin_required
def before_request():
'''
Ensures that the routes in this package can be visited only by users with
administrator privileges (login_required and admin_required).
'''
pass
from app.users.utils import (
user_endpoint_arguments_constructor as user_eac,
user_dynamic_list_constructor as user_dlc
)
@bp.route('')
@register_breadcrumb(bp, '.', '<i class="material-icons left">admin_panel_settings</i>Administration')
def index():
return redirect(url_for('.users'))
@bp.route('/users')
@register_breadcrumb(bp, '.users', '<i class="material-icons left">group</i>Users')
def users():
users = [x.to_json_serializeable(backrefs=True) for x in User.query.all()]
return render_template(
@ -39,12 +30,14 @@ def users():
@bp.route('/users/<hashid:user_id>')
@register_breadcrumb(bp, '.users.entity', '', dynamic_list_constructor=user_dlc)
def user(user_id):
user = User.query.get_or_404(user_id)
return render_template('admin/user.html.j2', title='User', user=user)
@bp.route('/users/<hashid:user_id>/edit', methods=['GET', 'POST'])
@register_breadcrumb(bp, '.users.entity.edit', 'Edit', endpoint_arguments_constructor=user_eac)
def edit_user(user_id):
user = User.query.get_or_404(user_id)
admin_edit_user_form = AdminEditUserForm(
@ -92,20 +85,3 @@ def edit_user(user_id):
title='Edit user',
user=user
)
@bp.route('/users/<hashid:user_id>/delete', methods=['DELETE'])
def delete_user(user_id):
def _delete_user(app, user_id):
with app.app_context():
user = User.query.get(user_id)
user.delete()
db.session.commit()
User.query.get_or_404(user_id)
thread = Thread(
target=_delete_user,
args=(current_app._get_current_object(), user_id)
)
thread.start()
return {}, 202