From 9303f0238ccf80f89230e1cf6b4891b65b77b094 Mon Sep 17 00:00:00 2001 From: AlexEbenrode <38831366+AlexEbenrode@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:20:39 +0500 Subject: [PATCH] Custom admin site for reordering app list (#2421) * Add custom admin site for reordering app list --- src/core/admin/admin_site.py | 25 +++++++++++++++++++++++++ src/core/urls/__init__.py | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/core/admin/admin_site.py diff --git a/src/core/admin/admin_site.py b/src/core/admin/admin_site.py new file mode 100644 index 00000000000..9eab6be0632 --- /dev/null +++ b/src/core/admin/admin_site.py @@ -0,0 +1,25 @@ +from django.contrib import admin +from django.http.request import HttpRequest + + +class AdminSite(admin.AdminSite): + def __init__(self, name: str) -> None: + super().__init__(name=name) + self._registry.update(admin.site._registry) + + def get_app_list(self, request: HttpRequest, app_label: str | None = None) -> list[dict]: + app_list = super().get_app_list(request, app_label) + app_list.sort(key=self._get_app_order_index) + return app_list + + @staticmethod + def _get_app_order_index(element: dict) -> int: + app_order = ["orders", "notion", "chains", "products", "otherapp"] + + if element["app_label"] in app_order: + return app_order.index(element["app_label"]) + + return len(app_order) + + +admin_site = AdminSite(name="custom_admin") diff --git a/src/core/urls/__init__.py b/src/core/urls/__init__.py index 9cc92707a80..ccee72a863e 100644 --- a/src/core/urls/__init__.py +++ b/src/core/urls/__init__.py @@ -1,8 +1,8 @@ import debug_toolbar # type: ignore from django.conf.urls import include -from django.contrib import admin from django.urls import path +from core.admin.admin_site import admin_site from core.views import HomePageView api = [ @@ -11,7 +11,7 @@ urlpatterns = [ path("api/", include(api)), - path("admin/", admin.site.urls), + path("admin/", admin_site.urls), path("__debug__/", include(debug_toolbar.urls)), path("", HomePageView.as_view()), ]