2019-09-10 15:17:35 +02:00
|
|
|
"Haldis admin related views and models"
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
import flask_login as login
|
2019-09-08 01:58:21 +02:00
|
|
|
from flask import Flask
|
2017-01-06 12:05:31 +01:00
|
|
|
from flask_admin import Admin
|
|
|
|
from flask_admin.contrib.sqla import ModelView
|
2019-09-08 01:58:21 +02:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2020-01-26 14:35:14 +01:00
|
|
|
from models import Order, OrderItem, OrderItemChoice, User
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ModelBaseView(ModelView):
|
2019-09-10 15:17:35 +02:00
|
|
|
"Base model for admin related things"
|
|
|
|
# pylint: disable=R0201, R0903
|
2019-09-08 01:58:21 +02:00
|
|
|
def is_accessible(self) -> bool:
|
2019-09-10 15:17:35 +02:00
|
|
|
"Check if the user has admin permission"
|
2015-03-31 20:15:22 +02:00
|
|
|
if login.current_user.is_anonymous():
|
|
|
|
return False
|
|
|
|
return login.current_user.is_admin()
|
|
|
|
|
|
|
|
|
|
|
|
class UserAdminModel(ModelBaseView):
|
2019-09-10 15:17:35 +02:00
|
|
|
"Model for user admin"
|
|
|
|
# pylint: disable=R0903
|
2019-09-05 03:33:29 +02:00
|
|
|
column_searchable_list = ("username",)
|
2015-03-31 20:15:22 +02:00
|
|
|
inline_models = None
|
|
|
|
|
|
|
|
|
2019-09-10 15:17:35 +02:00
|
|
|
def init_admin(app: Flask, database: SQLAlchemy) -> None:
|
|
|
|
"Initialize the admin related things in the app."
|
2019-09-05 03:33:29 +02:00
|
|
|
admin = Admin(app, name="Haldis", url="/admin", template_mode="bootstrap3")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-09-10 15:17:35 +02:00
|
|
|
admin.add_view(UserAdminModel(User, database.session))
|
|
|
|
admin.add_view(ModelBaseView(Order, database.session))
|
|
|
|
admin.add_view(ModelBaseView(OrderItem, database.session))
|
2020-01-26 14:35:14 +01:00
|
|
|
admin.add_view(ModelBaseView(OrderItemChoice, database.session))
|