From f6753172ec9f3c64e32e0141dd3a3c62d4e473d2 Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 29 Jul 2020 23:14:57 +0200 Subject: [PATCH] Polish the admin interface a bit --- app/admin.py | 37 ++++++++++++++++++++++------------ app/app.py | 1 - app/templates/admin/index.html | 8 ++++++++ app/templates/layout.html | 4 ++-- 4 files changed, 34 insertions(+), 16 deletions(-) create mode 100644 app/templates/admin/index.html diff --git a/app/admin.py b/app/admin.py index 0ba2810..4ba9d99 100644 --- a/app/admin.py +++ b/app/admin.py @@ -1,5 +1,3 @@ -"Haldis admin related views and models" - import flask_login as login from flask import Flask from flask_admin import Admin @@ -10,27 +8,40 @@ from models import Order, OrderItem, OrderItemChoice, User class ModelBaseView(ModelView): - "Base model for admin related things" - # pylint: disable=R0201, R0903 + # pylint: disable=too-few-public-methods, no-self-use def is_accessible(self) -> bool: - "Check if the user has admin permission" - if login.current_user.is_anonymous(): - return False return login.current_user.is_admin() class UserAdminModel(ModelBaseView): - "Model for user admin" - # pylint: disable=R0903 + # pylint: disable=too-few-public-methods column_searchable_list = ("username",) + column_editable_list = ("username",) + column_default_sort = "username" inline_models = None +class OrderAdminModel(ModelBaseView): + # pylint: disable=too-few-public-methods + column_list = ["starttime", "stoptime", "location_name", "location_id", "courier"] + column_labels = { + "starttime": "Start time", "stoptime": "Closing time", + "location_name": "Location name", "location_id": "HLDS location ID", + "courier": "Courier"} + form_excluded_columns = ["items", "courier_id"] + column_default_sort = ("starttime", True) + can_delete = False + + +class OrderItemAdminModel(ModelBaseView): + # pylint: disable=too-few-public-methods + column_default_sort = ("order_id", True) + + def init_admin(app: Flask, database: SQLAlchemy) -> None: - "Initialize the admin related things in the app." + "Register admin views with Flask app." admin = Admin(app, name="Haldis", url="/admin", template_mode="bootstrap3") admin.add_view(UserAdminModel(User, database.session)) - admin.add_view(ModelBaseView(Order, database.session)) - admin.add_view(ModelBaseView(OrderItem, database.session)) - admin.add_view(ModelBaseView(OrderItemChoice, database.session)) + admin.add_view(OrderAdminModel(Order, database.session)) + admin.add_view(OrderItemAdminModel(OrderItem, database.session)) diff --git a/app/app.py b/app/app.py index e5e4f22..1a3dd98 100755 --- a/app/app.py +++ b/app/app.py @@ -69,7 +69,6 @@ def register_plugins(app: Flask) -> Manager: app_manager = Manager(app) app_manager.add_command("db", MigrateCommand) app_manager.add_command("runserver", Server(port=8000)) - # Add admin interface init_admin(app, db) # Init login manager diff --git a/app/templates/admin/index.html b/app/templates/admin/index.html new file mode 100644 index 0000000..fea67c3 --- /dev/null +++ b/app/templates/admin/index.html @@ -0,0 +1,8 @@ +{% extends 'admin/master.html' %} + +{% block body %} +

To edit the metadata of an order, go to Order. There is +no link from an order to the ordered items, so…

+

To edit or delete items from orders, go straight to Order Item.

+

To edit locations and their menus, edit the HLDS data files in the repository and submit a pull/merge request.

+{% endblock %} diff --git a/app/templates/layout.html b/app/templates/layout.html index ede4835..d825bf1 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -9,8 +9,8 @@ ('general_bp.about', 'About'), ('stats_blueprint.stats', 'Stats'), ] -%} -{% if not current_user.is_anonymous() and current_user.is_admin() -%} - {% set navbar = navbar + [('admin.index', 'Admin')] -%} +{% if current_user.is_admin() -%} + {% set navbar = navbar + [('admin.index', 'Admin')] -%} {% endif -%} {% set active_page = active_page|default('index') -%}