-
Notifications
You must be signed in to change notification settings - Fork 100
/
sites.py
367 lines (287 loc) · 12.3 KB
/
sites.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from http import HTTPStatus
from typing import Any, Callable, Dict, List, Optional, Union
from django.contrib.admin import AdminSite
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.validators import EMPTY_VALUES
from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse
from django.urls import URLPattern, path, reverse, reverse_lazy
from django.utils.functional import lazy
from django.utils.module_loading import import_string
from .settings import get_config
from .utils import hex_to_rgb
from .widgets import CHECKBOX_CLASSES, INPUT_CLASSES
class UnfoldAdminSite(AdminSite):
default_site = "unfold.admin.UnfoldAdminSite"
settings_name = "UNFOLD"
def __init__(self, name: str = "admin") -> None:
from .forms import AuthenticationForm
super().__init__(name)
if self.login_form is None:
self.login_form = AuthenticationForm
if get_config(self.settings_name)["SITE_TITLE"]:
self.site_title = get_config(self.settings_name)["SITE_TITLE"]
if get_config(self.settings_name)["SITE_HEADER"]:
self.site_header = get_config(self.settings_name)["SITE_HEADER"]
if get_config(self.settings_name)["SITE_URL"]:
self.site_url = get_config(self.settings_name)["SITE_URL"]
def get_urls(self) -> List[URLPattern]:
urlpatterns = [
path("search/", self.admin_view(self.search), name="search"),
path(
"toggle-sidebar/",
self.admin_view(self.toggle_sidebar),
name="toggle_sidebar",
),
] + super().get_urls()
return urlpatterns
def each_context(self, request: HttpRequest) -> Dict[str, Any]:
context = super().each_context(request)
context.update(
{
"form_classes": {
"text_input": INPUT_CLASSES,
"checkbox": CHECKBOX_CLASSES,
},
"site_logo": self._get_mode_images(
get_config(self.settings_name)["SITE_LOGO"], request
),
"site_icon": self._get_mode_images(
get_config(self.settings_name)["SITE_ICON"], request
),
"site_symbol": self._get_value(
get_config(self.settings_name)["SITE_SYMBOL"], request
),
"show_history": get_config(self.settings_name)["SHOW_HISTORY"],
"show_view_on_site": get_config(self.settings_name)[
"SHOW_VIEW_ON_SITE"
],
"colors": self._process_colors(
get_config(self.settings_name)["COLORS"]
),
"tab_list": self.get_tabs_list(request),
"styles": [
self._get_value(style, request)
for style in get_config(self.settings_name)["STYLES"]
],
"theme": get_config(self.settings_name).get("THEME"),
"scripts": [
self._get_value(script, request)
for script in get_config(self.settings_name)["SCRIPTS"]
],
"sidebar_show_all_applications": get_config(self.settings_name)[
"SIDEBAR"
].get("show_all_applications"),
"sidebar_show_search": get_config(self.settings_name)["SIDEBAR"].get(
"show_search"
),
"sidebar_navigation": self.get_sidebar_list(request)
if self.has_permission(request)
else [],
}
)
environment = get_config(self.settings_name)["ENVIRONMENT"]
if environment and isinstance(environment, str):
try:
callback = import_string(environment)
context.update({"environment": callback(request)})
except ImportError:
pass
return context
def index(
self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = None
) -> TemplateResponse:
app_list = self.get_app_list(request)
context = {
**self.each_context(request),
"title": self.index_title,
"subtitle": None,
"app_list": app_list,
"index": True,
**(extra_context or {}),
}
dashboard_callback = get_config(self.settings_name)["DASHBOARD_CALLBACK"]
if isinstance(dashboard_callback, str):
context = import_string(dashboard_callback)(request, context)
request.current_app = self.name
return TemplateResponse(
request, self.index_template or "admin/index.html", context
)
def toggle_sidebar(
self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = None
) -> HttpResponse:
if "toggle_sidebar" not in request.session:
request.session["toggle_sidebar"] = True
else:
request.session["toggle_sidebar"] = not request.session["toggle_sidebar"]
return HttpResponse(status=HTTPStatus.OK)
def search(
self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = None
) -> TemplateResponse:
query = request.GET.get("s").lower()
app_list = super().get_app_list(request)
results = []
if query in EMPTY_VALUES:
return HttpResponse()
for app in app_list:
if query in app["name"].lower():
results.append(app)
continue
models = []
for model in app["models"]:
if query in model["name"].lower():
models.append(model)
if len(models) > 0:
app["models"] = models
results.append(app)
return TemplateResponse(
request,
template="unfold/helpers/search_results.html",
context={
"results": results,
},
)
def login(
self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = None
) -> HttpResponse:
extra_context = {} if extra_context is None else extra_context
image = self._get_value(
get_config(self.settings_name)["LOGIN"].get("image"), request
)
redirect_field_name = self._get_value(
get_config(self.settings_name)["LOGIN"].get("redirect_after"), request
)
if image not in EMPTY_VALUES:
extra_context.update(
{
"image": image,
}
)
if redirect_field_name not in EMPTY_VALUES:
extra_context.update({REDIRECT_FIELD_NAME: redirect_field_name})
return super().login(request, extra_context)
def password_change(
self, request: HttpRequest, extra_context: Optional[Dict[str, Any]] = None
) -> HttpResponse:
from django.contrib.auth.views import PasswordChangeView
from .forms import AdminOwnPasswordChangeForm
url = reverse(f"{self.name}:password_change_done", current_app=self.name)
defaults = {
"form_class": AdminOwnPasswordChangeForm,
"success_url": url,
"extra_context": {**self.each_context(request), **(extra_context or {})},
}
if self.password_change_template is not None:
defaults["template_name"] = self.password_change_template
request.current_app = self.name
return PasswordChangeView.as_view(**defaults)(request)
def get_sidebar_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
navigation = get_config(self.settings_name)["SIDEBAR"].get("navigation", [])
results = []
def _get_is_active(link: str) -> bool:
if not isinstance(link, str):
link = str(link)
if link in request.path and link != reverse_lazy(f"{self.name}:index"):
return True
elif link == request.path == reverse_lazy(f"{self.name}:index"):
return True
return False
for group in navigation:
allowed_items = []
for item in group["items"]:
item["active"] = False
item["active"] = _get_is_active(item["link"])
for tab in get_config(self.settings_name)["TABS"]:
has_primary_link = False
has_tab_link_active = False
for tab_item in tab["items"]:
if item["link"] == tab_item["link"]:
has_primary_link = True
continue
if _get_is_active(tab_item["link"]):
has_tab_link_active = True
break
if has_primary_link and has_tab_link_active:
item["active"] = True
if isinstance(item["link"], Callable):
item["link"] = item["link"](request)
# Permission callback
item["has_permission"] = self._call_permission_callback(
item.get("permission"), request
)
# Badge callbacks
if "badge" in item and isinstance(item["badge"], str):
try:
callback = import_string(item["badge"])
item["badge_callback"] = lazy(callback)(request)
except ImportError:
pass
allowed_items.append(item)
group["items"] = allowed_items
results.append(group)
return results
def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
tabs = get_config(self.settings_name)["TABS"]
for tab in tabs:
allowed_items = []
for item in tab["items"]:
item["has_permission"] = self._call_permission_callback(
item.get("permission"), request
)
if isinstance(item["link"], Callable):
item["link"] = item["link"](request)
allowed_items.append(item)
tab["items"] = allowed_items
return tabs
def _get_mode_images(
self, images: Union[Dict[str, callable], callable, str], request: HttpRequest
) -> Union[Dict[str, str], str, None]:
if isinstance(images, dict):
if "light" in images and "dark" in images:
return {
"light": self._get_value(images["light"], request),
"dark": self._get_value(images["dark"], request),
}
return None
return self._get_value(images, request)
def _call_permission_callback(
self, callback: Union[str, Callable, None], request: HttpRequest
) -> bool:
if callback is None:
return True
if isinstance(callback, str):
try:
callback = import_string(callback)
except ImportError:
pass
if isinstance(callback, str) or isinstance(callback, Callable):
# We are not able to use here "is" because type is lazy loaded function
if lazy(callback)(request) == True: # noqa: E712
return True
return False
def _get_value(
self, instance: Union[str, Callable, None], *args: Any
) -> Optional[str]:
if instance is None:
return None
if isinstance(instance, str):
return instance
if isinstance(instance, Callable):
return instance(*args)
return None
def _replace_values(self, target: Dict, source: Dict, request: HttpRequest):
for key in source.keys():
if source[key] is not None and callable(source[key]):
target[key] = source[key](request)
else:
target[key] = source[key]
return target
def _process_colors(
self, colors: Dict[str, Dict[str, str]]
) -> Dict[str, Dict[str, str]]:
for name, weights in colors.items():
for weight, value in weights.items():
if value[0] != "#":
continue
colors[name][weight] = " ".join(str(item) for item in hex_to_rgb(value))
return colors