-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadmin.py
More file actions
35 lines (27 loc) · 1.36 KB
/
admin.py
File metadata and controls
35 lines (27 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import redirect, request, url_for
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_babel import Babel
from auth import get_current_user
class AuthenticatedAdminIndexView(AdminIndexView):
def is_accessible(self):
return get_current_user() is not None
def inaccessible_callback(self, name, **kwargs):
return redirect(url_for('auth.login', next=request.url))
class AuthenticatedModelView(ModelView):
def is_accessible(self):
return get_current_user() is not None
def inaccessible_callback(self, name, **kwargs):
return redirect(url_for('auth.login', next=request.url))
def init_admin(app, db, todo_model, category_model):
"""Attach Babel and register secured admin views for the given models."""
Babel(app, locale_selector=lambda: 'en')
admin = Admin(app, name="Admin", template_mode="bootstrap4",
index_view=AuthenticatedAdminIndexView())
admin.add_view(AuthenticatedModelView(todo_model, db.session,
endpoint="todo_admin",
name="Todos"))
admin.add_view(AuthenticatedModelView(category_model, db.session,
endpoint="category_admin",
name="Categories"))
return admin