2019-08-28 03:46:04 +02:00
|
|
|
import flask_login as login
|
2017-01-06 12:05:31 +01:00
|
|
|
from flask_admin import Admin
|
|
|
|
from flask_admin.contrib.sqla import ModelView
|
2015-03-31 20:15:22 +02:00
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
from models import Location, Order, OrderItem, Product, User
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ModelBaseView(ModelView):
|
|
|
|
def is_accessible(self):
|
|
|
|
if login.current_user.is_anonymous():
|
|
|
|
return False
|
|
|
|
|
|
|
|
return login.current_user.is_admin()
|
|
|
|
|
|
|
|
|
|
|
|
class UserAdminModel(ModelBaseView):
|
2019-09-05 03:33:29 +02:00
|
|
|
column_searchable_list = ("username",)
|
2015-03-31 20:15:22 +02:00
|
|
|
inline_models = None
|
|
|
|
|
|
|
|
|
2015-08-18 21:19:41 +02:00
|
|
|
class ProductAdminModel(ModelBaseView):
|
2019-09-05 03:33:29 +02:00
|
|
|
column_searchable_list = ("name",)
|
2015-08-18 21:19:41 +02:00
|
|
|
inline_models = None
|
|
|
|
|
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
class LocationAdminModel(ModelBaseView):
|
2019-09-05 03:33:29 +02:00
|
|
|
column_searchable_list = ("name", "address", "website")
|
2015-03-31 20:15:22 +02:00
|
|
|
inline_models = None
|
2019-09-05 03:33:29 +02:00
|
|
|
form_columns = ("name", "address", "website", "telephone")
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
def init_admin(app, db):
|
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-08-28 03:46:04 +02:00
|
|
|
admin.add_view(UserAdminModel(User, db.session))
|
|
|
|
admin.add_view(LocationAdminModel(Location, db.session))
|
|
|
|
admin.add_view(ProductAdminModel(Product, db.session))
|
|
|
|
admin.add_view(ModelBaseView(Order, db.session))
|
|
|
|
admin.add_view(ModelBaseView(OrderItem, db.session))
|