This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 450
/
__init__.py
169 lines (143 loc) · 5.29 KB
/
__init__.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf-8 -*
import logging
from werkzeug.utils import import_string
from flask import request, session
from flask.ext.admin import Admin
from ..models import (Link, Config, SubContentPurpose, ChannelType,
ContentTemplateType, Channel)
from .models import ModelAdmin, FileAdmin, BaseIndexView
from .views import (IndexView, InspectorView, LinkAdmin, ConfigAdmin,
SubContentPurposeAdmin, ChannelTypeAdmin,
ContentTemplateTypeAdmin, ChannelAdmin)
from .utils import _, _l
logger = logging.getLogger()
class QuokkaAdmin(Admin):
def register(self, model, view=None, *args, **kwargs):
View = view or ModelAdmin
self.add_view(View(model, *args, **kwargs))
# try:
# self.add_view(View(model, *args, **kwargs))
# except Exception as e:
# logger.warning(
# "admin.register({0}, {1}, {2}, {3}) error: {4}".format(
# model, view, args, kwargs, e.message
# )
# )
def create_admin(app=None):
index_view = IndexView()
return QuokkaAdmin(app, index_view=index_view)
def configure_admin(app, admin):
custom_index = app.config.get('ADMIN_INDEX_VIEW')
if custom_index:
admin.index_view = import_string(custom_index)()
if isinstance(admin._views[0], BaseIndexView):
del admin._views[0]
admin._views.insert(0, admin.index_view)
ADMIN = app.config.get(
'ADMIN',
{
'name': 'Quokka Admin',
'url': '/admin'
}
)
for k, v in list(ADMIN.items()):
setattr(admin, k, v)
babel = app.extensions.get('babel')
if babel:
try:
@babel.localeselector
def get_locale():
override = request.args.get('lang')
if override:
session['lang'] = override
return session.get('lang', 'en')
admin.locale_selector(get_locale)
except:
pass # Exception: Can not add locale_selector second time.
for entry in app.config.get('FILE_ADMIN', []):
try:
admin.add_view(
FileAdmin(
entry['path'],
entry['url'],
name=entry['name'],
category=entry['category'],
endpoint=entry['endpoint'],
roles_accepted=entry.get('roles_accepted')
)
)
except:
pass # TODO: check blueprint endpoisnt colision
# register all themes in file manager
for k, theme in app.theme_manager.themes.iteritems():
try:
if k == app.config.get('DEFAULT_THEME'):
suffix = "(Site theme)"
elif k == app.config.get('ADMIN_THEME'):
suffix = "(Admin theme)"
else:
suffix = "Theme"
admin.add_view(
FileAdmin(
theme.static_path,
"/_themes/{0}/".format(theme.identifier),
name="{0}: {1} static files".format(suffix,
theme.identifier),
category="Files",
endpoint="{0}_static_files".format(theme.identifier),
roles_accepted=('admin', "editor")
)
)
admin.add_view(
FileAdmin(
theme.templates_path,
"/theme_template_files/{0}/".format(theme.identifier),
name="{0}: {1} template files".format(suffix,
theme.identifier),
category="Files",
endpoint="{0}_template_files".format(theme.identifier),
roles_accepted=('admin', "editor")
)
)
except:
pass
# adding views
admin.add_view(InspectorView(category=_("Settings"),
name=_l("Inspector")))
# adding extra views
extra_views = app.config.get('ADMIN_EXTRA_VIEWS', [])
for view in extra_views:
admin.add_view(
import_string(view['module'])(
category=_(view.get('category')),
name=_l(view.get('name'))
)
)
# adding model views
admin.register(
Link,
LinkAdmin,
category=_("Content"),
name=_l("Link")
)
admin.register(Config,
ConfigAdmin,
category=_("Settings"),
name=_l("Config"))
admin.register(SubContentPurpose,
SubContentPurposeAdmin,
category=_("Settings"),
name=_l("Sub content purposes"))
admin.register(ChannelType, ChannelTypeAdmin,
category=_("Settings"), name=_l("Channel type"))
admin.register(ContentTemplateType,
ContentTemplateTypeAdmin,
category=_("Settings"),
name=_l("Template type"))
admin.register(Channel, ChannelAdmin,
category=_("Content"), name=_l("Channel"))
# avoid registering twice
if admin.app is None:
admin.init_app(app)
return admin