2022-04-19 23:20:03 +02:00
|
|
|
"Module for everything related to Admin users"
|
2020-07-29 21:01:47 +02:00
|
|
|
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):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Class for the base view of the model"
|
2020-07-29 23:14:57 +02:00
|
|
|
# pylint: disable=too-few-public-methods, no-self-use
|
2020-07-29 21:01:47 +02:00
|
|
|
def is_accessible(self) -> bool:
|
2022-04-19 23:20:03 +02:00
|
|
|
"Function to check if the logged in user is an admin"
|
2020-07-29 21:01:47 +02:00
|
|
|
return login.current_user.is_admin()
|
|
|
|
|
|
|
|
|
|
|
|
class UserAdminModel(ModelBaseView):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Class for the model of a UserAdmin"
|
2020-07-29 23:14:57 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2020-07-29 21:01:47 +02:00
|
|
|
column_searchable_list = ("username",)
|
2020-07-29 23:14:57 +02:00
|
|
|
column_editable_list = ("username",)
|
|
|
|
column_default_sort = "username"
|
2020-07-29 21:01:47 +02:00
|
|
|
inline_models = None
|
|
|
|
|
|
|
|
|
2020-07-29 23:14:57 +02:00
|
|
|
class OrderAdminModel(ModelBaseView):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Class for the model of a OrderAdmin"
|
2020-07-29 23:14:57 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
2020-07-29 23:26:07 +02:00
|
|
|
column_default_sort = ("starttime", True)
|
2022-05-20 23:29:05 +02:00
|
|
|
column_list = ["starttime", "stoptime", "location_name", "location_id", "courier", "association"]
|
2020-07-29 23:14:57 +02:00
|
|
|
column_labels = {
|
2022-04-19 22:03:00 +02:00
|
|
|
"starttime": "Start Time",
|
|
|
|
"stoptime": "Closing Time",
|
|
|
|
"location_id": "HLDS Location ID",
|
2022-05-20 23:29:05 +02:00
|
|
|
"association": "Association",
|
2022-04-19 22:03:00 +02:00
|
|
|
}
|
2020-07-29 23:14:57 +02:00
|
|
|
form_excluded_columns = ["items", "courier_id"]
|
|
|
|
can_delete = False
|
|
|
|
|
|
|
|
|
|
|
|
class OrderItemAdminModel(ModelBaseView):
|
2022-04-19 23:20:03 +02:00
|
|
|
"Class for the model of a OrderItemAdmin"
|
2020-07-29 23:14:57 +02:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
column_default_sort = ("order_id", True)
|
2020-07-29 23:26:07 +02:00
|
|
|
column_list = [
|
2022-04-19 22:03:00 +02:00
|
|
|
"order_id",
|
2022-05-20 19:41:42 +02:00
|
|
|
"slug",
|
2022-04-19 22:03:00 +02:00
|
|
|
"order.location_name",
|
|
|
|
"user_name",
|
|
|
|
"user",
|
|
|
|
"dish_name",
|
|
|
|
"dish_id",
|
|
|
|
"comment",
|
|
|
|
"price",
|
|
|
|
"paid",
|
|
|
|
"hlds_data_version",
|
2020-07-29 23:26:07 +02:00
|
|
|
]
|
|
|
|
column_labels = {
|
2022-04-19 22:03:00 +02:00
|
|
|
"order_id": "Order",
|
|
|
|
"order.location_name": "Order's Location",
|
|
|
|
"user_name": "Anon. User",
|
|
|
|
"user_id": "Registered User",
|
|
|
|
"hlds_data_version": "HLDS Data Version",
|
|
|
|
"dish_id": "HLDS Dish ID",
|
|
|
|
}
|
2020-07-29 23:14:57 +02:00
|
|
|
|
|
|
|
|
2020-07-29 21:01:47 +02:00
|
|
|
def init_admin(app: Flask, database: SQLAlchemy) -> None:
|
2020-07-29 23:14:57 +02:00
|
|
|
"Register admin views with Flask app."
|
2020-07-29 21:01:47 +02:00
|
|
|
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))
|