From 2efc9533ec6263a757a6f2f7e4778ed5ee120eaf Mon Sep 17 00:00:00 2001 From: Patrick Jentsch Date: Tue, 14 Mar 2023 14:24:52 +0100 Subject: [PATCH] Add breadcrumbs to admin package --- app/__init__.py | 1 + app/admin/__init__.py | 17 ++++++++++++++- app/admin/json_routes.py | 22 +++++++++++++++++++ app/admin/routes.py | 46 ++++++++++------------------------------ 4 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 app/admin/json_routes.py diff --git a/app/__init__.py b/app/__init__.py index 318502f4..bc2243cf 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -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 diff --git a/app/admin/__init__.py b/app/admin/__init__.py index b5936b4c..be000a9a 100644 --- a/app/admin/__init__.py +++ b/app/admin/__init__.py @@ -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 diff --git a/app/admin/json_routes.py b/app/admin/json_routes.py new file mode 100644 index 00000000..d8b45425 --- /dev/null +++ b/app/admin/json_routes.py @@ -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//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 diff --git a/app/admin/routes.py b/app/admin/routes.py index 95ce7730..15ff0d9f 100644 --- a/app/admin/routes.py +++ b/app/admin/routes.py @@ -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, '.', 'admin_panel_settingsAdministration') def index(): return redirect(url_for('.users')) @bp.route('/users') +@register_breadcrumb(bp, '.users', 'groupUsers') 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/') +@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//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//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