-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels_admin.py
112 lines (80 loc) · 3.36 KB
/
models_admin.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Flask-Admin Models
"""
from flask import flash, redirect, url_for, request
from flask_security import current_user
from flask_admin import AdminIndexView
from flask_admin.contrib.sqla import ModelView
from constants import ROLE_OWNER
###############################################################################
class SecureAdminIndexView(AdminIndexView):
def is_accessible(self):
return current_user.has_role(ROLE_OWNER)
def inaccessible_callback(self, name, **kwargs):
flash("You do not have permission to view this resource.", "error")
return redirect(url_for("show_home"))
class SecureModelView(ModelView):
def is_accessible(self):
return current_user.has_role(ROLE_OWNER)
def inaccessible_callback(self, name, **kwargs):
flash("You do not have permission to view this resource.", "error")
return redirect(url_for("show_home"))
###############################################################################
class BaseModelView(SecureModelView):
column_display_pk = True
column_hide_backrefs = False
can_export = True
can_create = False
can_edit = True
can_view_details = True
can_delete = False
create_modal = False
edit_modal = True
details_modal = True
can_set_page_size = True
def __init__(self, model, session, **kwargs):
if self.form_excluded_columns:
self.form_excluded_columns = list(self.form_excluded_columns)
else:
self.form_excluded_columns = []
# if columns were excluded from the list view
# exclude them from create / edit forms as well
if self.column_exclude_list:
for field in self.column_exclude_list:
self.form_excluded_columns.append(field)
# exclude relationships from showing up in the create / edit forms
for relationship in model.__mapper__.relationships:
self.form_excluded_columns.append(relationship.key)
self.form_excluded_columns = tuple(self.form_excluded_columns)
super().__init__(model, session, **kwargs)
###############################################################################
class UserModelView(BaseModelView):
column_exclude_list = ("password", "fs_uniquifier")
column_searchable_list = ("username", "email")
form_excluded_columns = (
"confirmed_at",
"last_login_at", "current_login_at",
"last_login_ip", "current_login_ip", "login_count"
)
class TaskModelView(BaseModelView):
column_searchable_list = ("category", "title", "short", "help")
form_excluded_columns = ("category",)
class LabelModelView(BaseModelView):
column_searchable_list = ("label", "description")
class AnnotationModelView(BaseModelView):
column_searchable_list = ("annotator_id",)
column_formatters = {
"token":
lambda v, c, m, p: f"<{m.token_id}: {m.token.text}>",
"src_token":
lambda v, c, m, p: f"<{m.src_id}: {m.src_token.text}>",
"dst_token":
lambda v, c, m, p: f"<{m.dst_id}: {m.dst_token.text}>",
"label":
lambda v, c, m, p: f"<{m.label_id}: {m.label.label}>",
"annotator":
lambda v, c, m, p: f"<{m.annotator_id}: {m.annotator.username}>",
}
###############################################################################