summaryrefslogtreecommitdiff
path: root/ishtar_common/apps.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-02-23 18:09:15 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2023-02-23 18:11:08 +0100
commit2ae0e6f81e570a75ac27d455db9834c93f66d1fe (patch)
treeade5f9523698ad0ec5bcf988834f73df8d140dc3 /ishtar_common/apps.py
parentd86abf7eb1d5a7ea8c8c0ecfde24680d3a5d7094 (diff)
downloadIshtar-2ae0e6f81e570a75ac27d455db9834c93f66d1fe.tar.bz2
Ishtar-2ae0e6f81e570a75ac27d455db9834c93f66d1fe.zip
Admin: overload index to add sub-section headers
Diffstat (limited to 'ishtar_common/apps.py')
-rw-r--r--ishtar_common/apps.py105
1 files changed, 104 insertions, 1 deletions
diff --git a/ishtar_common/apps.py b/ishtar_common/apps.py
index eca70b3e1..1f076462d 100644
--- a/ishtar_common/apps.py
+++ b/ishtar_common/apps.py
@@ -1,5 +1,7 @@
-from django.apps import AppConfig
+from django.apps import AppConfig, apps
from django.contrib.admin import AdminSite
+from django.urls import NoReverseMatch, reverse
+from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
@@ -7,6 +9,107 @@ class IshtarAdminSite(AdminSite):
site_header = _("Ishtar administration")
site_title = _("Ishtar administration")
+ MODEL_OVERLOAD = {
+ # (app, model) -> new app
+ ("ishtar_common", "OperationType"): "archaeological_operations"
+ }
+
+ 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
+ if (app_label, object_name) in self.MODEL_OVERLOAD:
+ app_label_for_dict = 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': getattr(model, "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
+
admin_site = IshtarAdminSite()
# AFAC