From bee0083dac612718592b15141c4af832a67dfbd6 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 3 Jun 2024 02:52:48 +0200 Subject: [PATCH] =?UTF-8?q?wtf,=20je=20to=20podle=20dokumentace=E2=80=A6?= =?UTF-8?q?=20a=20bugreportu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mamweb/admin.py | 48 ++++++++++++++++++--------------------- mamweb/apps.py | 12 ++++++++++ mamweb/settings_common.py | 3 ++- 3 files changed, 36 insertions(+), 27 deletions(-) create mode 100644 mamweb/apps.py diff --git a/mamweb/admin.py b/mamweb/admin.py index d905f186..5a1d9d1e 100644 --- a/mamweb/admin.py +++ b/mamweb/admin.py @@ -5,7 +5,6 @@ jako první) adminu k Semináři. """ import locale from django.contrib import admin -from django.contrib.admin import AdminSite from django.contrib.flatpages.models import FlatPage # Note: we are renaming the original Admin and Form as we import them! @@ -33,31 +32,28 @@ admin.site.register(FlatPage, FlatPageAdmin) locale.setlocale(locale.LC_COLLATE, 'cs_CZ.UTF-8') -# https://books.agiliq.com/projects/django-admin-cookbook/en/latest/set_ordering.html -# FIXME zpraseno pomocí toho, že Python umí bez problému přepisovat funkce -def get_app_list(self, request, app_label=None): - """ - Return a sorted list of all the installed apps that have been - registered in this site. - """ +# Ref: https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#overriding-default-admin-site +# Používá se z mamweb/apps.py +class MamwebAdminSite(admin.AdminSite): + # Přeuspořádání modelů + # Ref: https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.AdminSite.get_app_list + def get_app_list(self, request, app_label=None): + orig_app_list = super().get_app_list(request, app_label) + apps_by_label = {app['label']: app for app in orig_app_list} - app_dict = self._build_app_dict(request, label=app_label) - aplikace_nahore = [ - 'seminar', - 'personalni', - 'novinky', - 'korektury', - 'various', - 'prednasky', - 'soustredeni', - ] - app_list = [app_dict[label] for label in aplikace_nahore] + [app_dict[label] for label in app_dict if label not in aplikace_nahore] + aplikace_nahore = [ + 'seminar', + 'personalni', + 'novinky', + 'korektury', + 'various', + 'prednasky', + 'soustredeni', + ] + app_list = [apps_by_label[label] for label in aplikace_nahore] + [apps_by_label[label] for label in apps_by_label if label not in aplikace_nahore] + # TreeNody na konec + for app in app_list: + app['models'].sort(key=lambda x: locale.strxfrm('žž' + x['name'].lower()) if (x['name'].endswith("(Node)")) else locale.strxfrm(x['name'].lower())) - # Sort the models alphabetically within each app. - for app in app_list: - app['models'].sort(key=lambda x: locale.strxfrm('žž' + x['name'].lower()) if (x['name'].endswith("(Node)")) else locale.strxfrm(x['name'].lower())) - - return app_list - -AdminSite.get_app_list = get_app_list + return app_list diff --git a/mamweb/apps.py b/mamweb/apps.py new file mode 100644 index 00000000..a410d054 --- /dev/null +++ b/mamweb/apps.py @@ -0,0 +1,12 @@ +from django.contrib.admin import apps +# Nemůžeme importovat rovnou AdminConfig, jinak se autodetekuje˙:-/ +# https://code.djangoproject.com/ticket/32642 + +# Ref: https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#overriding-default-admin-site +# Pozn: nad tím je „Customizing the AdminSite class“, ale ta nepřepisuje +# default, takže bychom museli přepsat všechny registrace do admina. Proto +# radši overridujeme i AdminConfig +class MamwebAdminConfig(apps.AdminConfig): + default_site = 'mamweb.admin.MamwebAdminSite' + + diff --git a/mamweb/settings_common.py b/mamweb/settings_common.py index 92124364..7384b162 100644 --- a/mamweb/settings_common.py +++ b/mamweb/settings_common.py @@ -108,6 +108,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'django.contrib.auth', + 'mamweb.apps', # Utilities 'reversion', 'django_countries', @@ -159,7 +160,7 @@ INSTALLED_APPS = ( # 'admin_tools.theming', # 'admin_tools.menu', # 'admin_tools.dashboard', - 'django.contrib.admin', +# 'django.contrib.admin', # Nechat na konci (INSTALLED_APPS je uspořádané): 'django_cleanup.apps.CleanupConfig', # Uklízí media/