haldis/app/admin.py

48 lines
1.6 KiB
Python
Raw Normal View History

import flask_login as login
from flask import Flask
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_sqlalchemy import SQLAlchemy
from models import Order, OrderItem, OrderItemChoice, User
class ModelBaseView(ModelView):
2020-07-29 23:14:57 +02:00
# pylint: disable=too-few-public-methods, no-self-use
def is_accessible(self) -> bool:
return login.current_user.is_admin()
class UserAdminModel(ModelBaseView):
2020-07-29 23:14:57 +02:00
# pylint: disable=too-few-public-methods
column_searchable_list = ("username",)
2020-07-29 23:14:57 +02:00
column_editable_list = ("username",)
column_default_sort = "username"
inline_models = None
2020-07-29 23:14:57 +02:00
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:
2020-07-29 23:14:57 +02:00
"Register admin views with Flask app."
admin = Admin(app, name="Haldis", url="/admin", template_mode="bootstrap3")
admin.add_view(UserAdminModel(User, database.session))
2020-07-29 23:14:57 +02:00
admin.add_view(OrderAdminModel(Order, database.session))
admin.add_view(OrderItemAdminModel(OrderItem, database.session))