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
|
from django.apps import AppConfig, apps
from django.contrib.admin import AdminSite
from django.http import Http404
from django.template.response import TemplateResponse
from django.urls import NoReverseMatch, reverse
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _
class IshtarAdminSite(AdminSite):
site_header = _("Ishtar administration")
site_title = _("Ishtar administration")
MODEL_OVERLOAD = {
# (app, model) -> (new app, section)
("ishtar_common", "OperationType"): ("archaeological_operations", ""),
("authtoken", "Token"): ("ishtar_common", _("API")),
}
def _build_app_dict(self, request, label=None):
# copied from contrib/admin/sites.py
# overload to get add "admin_section" in models and use MODEL_OVERLOAD
app_dict = {}
if label:
models = {
m: m_a for m, m_a in self._registry.items()
if m._meta.app_label == label or self.MODEL_OVERLOAD.get(
(m._meta.app_label, m._meta.object_name), None
)
}
else:
models = self._registry
for model, model_admin in models.items():
app_label = model._meta.app_label
# Ishtar
object_name = model._meta.object_name
admin_section = getattr(model, "ADMIN_SECTION", "")
if (app_label, object_name) in self.MODEL_OVERLOAD:
app_label_for_dict, admin_section = \
self.MODEL_OVERLOAD[(app_label, object_name)]
else:
app_label_for_dict = app_label
# end Ishtar
has_module_perms = model_admin.has_module_permission(request)
if not has_module_perms:
continue
perms = model_admin.get_model_perms(request)
# Check whether user has any perm for this module.
# If so, add the module to the model_list.
if True not in perms.values():
continue
info = (app_label, model._meta.model_name)
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'object_name': model._meta.object_name,
'perms': perms,
'admin_url': None,
'add_url': None,
# Ishtar change
'admin_section': admin_section,
# End Ishtar change
}
if perms.get('change') or perms.get('view'):
model_dict['view_only'] = not perms.get('change')
try:
model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info,
current_app=self.name)
except NoReverseMatch:
pass
if perms.get('add'):
try:
model_dict['add_url'] = reverse('admin:%s_%s_add' % info,
current_app=self.name)
except NoReverseMatch:
pass
# Ishtar
if app_label_for_dict in app_dict:
app_dict[app_label_for_dict]['models'].append(model_dict)
else:
app_dict[app_label_for_dict] = {
'name': apps.get_app_config(app_label_for_dict).verbose_name,
'app_label': app_label_for_dict,
'app_url': reverse(
'admin:app_list',
kwargs={'app_label': app_label_for_dict},
current_app=self.name,
),
'has_module_perms': has_module_perms,
'models': [model_dict],
}
# Ishtar end
if label:
return app_dict.get(label)
return app_dict
def get_app_list(self, request):
# copied from contrib/admin/sites.py
# overload to sort models by "admin_section"
app_dict = self._build_app_dict(request)
# Sort the apps alphabetically.
app_list = sorted(app_dict.values(), key=lambda x: x['name'].lower())
# Sort the models alphabetically within each app.
for app in app_list:
app['models'].sort(key=lambda x: (x['admin_section'], x['name'])) # Ishtar change
return app_list
def app_index(self, request, app_label, extra_context=None):
# copied from contrib/admin/sites.py
# overload to sort models by "admin_section"
app_dict = self._build_app_dict(request, app_label)
if not app_dict:
raise Http404('The requested admin page does not exist.')
# Sort the models alphabetically within each app.
app_dict['models'].sort(key=lambda x: (x['admin_section'], x['name'])) # Ishtar change
app_name = apps.get_app_config(app_label).verbose_name
context = {
**self.each_context(request),
'title': _('%(app)s administration') % {'app': app_name},
'app_list': [app_dict],
'app_label': app_label,
**(extra_context or {}),
}
request.current_app = self.name
return TemplateResponse(request, self.app_index_template or [
'admin/%s/app_index.html' % app_label,
'admin/app_index.html'
], context)
admin_site = IshtarAdminSite()
class IshtarCommonConfig(AppConfig):
name = "ishtar_common"
verbose_name = _("Ishtar - Common")
|