-
Notifications
You must be signed in to change notification settings - Fork 28
/
dashboard.py
227 lines (180 loc) · 6.41 KB
/
dashboard.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from importlib import import_module
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.template.loader import render_to_string
from jet.dashboard import modules
from jet.dashboard.models import UserDashboardModule
from django.utils.translation import ugettext_lazy as _
from jet.ordered_set import OrderedSet
from jet.utils import get_admin_site_name, context_to_dict
try:
from django.template.context_processors import csrf
except ImportError:
from django.core.context_processors import csrf
class Dashboard(object):
columns = 2
children = None
available_children = None
app_label = None
context = None
modules = None
class Media:
css = ()
js = ()
def __init__(self, context, **kwargs):
for key in kwargs:
if hasattr(self.__class__, key):
setattr(self, key, kwargs[key])
self.children = self.children or []
self.available_children = self.available_children or []
self.set_context(context)
def set_context(self, context):
self.context = context
self.init_with_context(context)
self.load_modules()
def init_with_context(self, context):
pass
def load_module(self, module_fullname):
package, module_name = module_fullname.rsplit('.', 1)
package = import_module(package)
module = getattr(package, module_name)
return module
def create_initial_module_models(self, user):
module_models = []
i = 0
for module in self.children:
column = module.column if module.column is not None else i % self.columns
order = module.order if module.order is not None else int(i / self.columns)
module_models.append(UserDashboardModule.objects.create(
title=module.title,
app_label=self.app_label,
user=user.pk,
module=module.fullname(),
column=column,
order=order,
settings=module.dump_settings(),
children=module.dump_children()
))
i += 1
return module_models
def load_modules(self):
module_models = UserDashboardModule.objects.filter(
app_label=self.app_label,
user=self.context['request'].user.pk
).all()
if len(module_models) == 0:
module_models = self.create_initial_module_models(self.context['request'].user)
loaded_modules = []
for module_model in module_models:
module_cls = module_model.load_module()
if module_cls is not None:
module = module_cls(model=module_model, context=self.context)
loaded_modules.append(module)
self.modules = loaded_modules
def render(self):
context = context_to_dict(self.context)
context.update({
'columns': range(self.columns),
'modules': self.modules,
'app_label': self.app_label,
})
context.update(csrf(context['request']))
return render_to_string('jet.dashboard/dashboard.html', context)
def render_tools(self):
context = context_to_dict(self.context)
context.update({
'children': self.children,
'app_label': self.app_label,
'available_children': self.available_children
})
context.update(csrf(context['request']))
return render_to_string('jet.dashboard/dashboard_tools.html', context)
def media(self):
unique_css = OrderedSet()
unique_js = OrderedSet()
for js in getattr(self.Media, 'js', ()):
unique_js.add(js)
for css in getattr(self.Media, 'css', ()):
unique_css.add(css)
for module in self.modules:
for js in getattr(module.Media, 'js', ()):
unique_js.add(js)
for css in getattr(module.Media, 'css', ()):
unique_css.add(css)
class Media:
css = list(unique_css)
js = list(unique_js)
return Media
class AppIndexDashboard(Dashboard):
def get_app_content_types(self):
return self.app_label + '.*',
def models(self):
return self.app_label + '.*',
class DefaultIndexDashboard(Dashboard):
columns = 3
def init_with_context(self, context):
self.available_children.append(modules.LinkList)
# self.available_children.append(modules.Feed)
site_name = get_admin_site_name(context)
# append a link list module for "quick links"
self.children.append(modules.LinkList(
_('Quick links'),
layout='stacked',
draggable=True,
deletable=True,
collapsible=True,
children=[
[_('Return to site'), '/'],
[_('Change password'),
reverse('%s:password_change' % site_name)],
[_('Log out'), reverse('%s:logout' % site_name)],
],
column=0,
order=0
))
# append an app list module for "Applications"
self.children.append(modules.AppList(
_('Applications'),
exclude=('auth.*',),
column=1,
order=0
))
# append an app list module for "Administration"
self.children.append(modules.AppList(
_('Administration'),
models=('auth.*',),
column=2,
order=0
))
# append a recent actions module
self.children.append(modules.RecentActions(
_('Recent Actions'),
10,
column=0,
order=1
))
class DefaultAppIndexDashboard(AppIndexDashboard):
def init_with_context(self, context):
self.available_children.append(modules.LinkList)
self.children.append(modules.ModelList(
title=_('Application models'),
models=self.models(),
column=0,
order=0
))
self.children.append(modules.RecentActions(
include_list=self.get_app_content_types(),
column=1,
order=0
))
class DashboardUrls(object):
_urls = []
def get_urls(self):
return self._urls
def register_url(self, url):
self._urls.append(url)
def register_urls(self, urls):
self._urls.extend(urls)
urls = DashboardUrls()