From ae827bb717341b7a5c5227d2418558b985ec9ef5 Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Tue, 1 Oct 2024 15:45:37 +0500 Subject: [PATCH 1/6] Add custom admin site for reordering app list --- src/core/admin/admin_site.py | 17 +++++++++++++++++ src/core/urls/__init__.py | 4 ++-- 2 files changed, 19 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..a0f503cd3f5 --- /dev/null +++ b/src/core/admin/admin_site.py @@ -0,0 +1,17 @@ +from typing import Any + +from django.contrib import admin + +class AdminSite(admin.AdminSite): + + def __init__(self, *args, **kwargs): + super(AdminSite, self).__init__(*args, **kwargs) + self._registry.update(admin.site._registry) + + def get_app_list(self, request: Any, app_label=None) -> list[Any]: + app_list = super().get_app_list(request, app_label) + app_order = ['orders', 'notion', 'chains', 'products', 'otherapp'] + app_list.sort(key=lambda x: app_order.index(x['app_label']) if x['app_label'] in app_order else len(app_order)) + return app_list + +admin_site = AdminSite(name='custom_admin') diff --git a/src/core/urls/__init__.py b/src/core/urls/__init__.py index 9cc92707a80..75b61aa38c1 100644 --- a/src/core/urls/__init__.py +++ b/src/core/urls/__init__.py @@ -1,9 +1,9 @@ import debug_toolbar # type: ignore from django.conf.urls import include -from django.contrib import admin from django.urls import path from core.views import HomePageView +from core.admin.admin_site import admin_site api = [ path("v2/", include("core.urls.v2")), @@ -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()), ] From 383b8d6f1d039e16ac2c9a121a8fd7a7fc4dc092 Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Tue, 1 Oct 2024 16:09:25 +0500 Subject: [PATCH 2/6] fix lint --- src/core/admin/admin_site.py | 17 +++++++++-------- src/core/urls/__init__.py | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/core/admin/admin_site.py b/src/core/admin/admin_site.py index a0f503cd3f5..b04c66c214c 100644 --- a/src/core/admin/admin_site.py +++ b/src/core/admin/admin_site.py @@ -2,16 +2,17 @@ from django.contrib import admin -class AdminSite(admin.AdminSite): - def __init__(self, *args, **kwargs): - super(AdminSite, self).__init__(*args, **kwargs) +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: Any, app_label=None) -> list[Any]: + def get_app_list(self, request: Any, app_label: Any | None = None) -> list[Any]: app_list = super().get_app_list(request, app_label) - app_order = ['orders', 'notion', 'chains', 'products', 'otherapp'] - app_list.sort(key=lambda x: app_order.index(x['app_label']) if x['app_label'] in app_order else len(app_order)) + app_order = ["orders", "notion", "chains", "products", "otherapp"] + app_list.sort(key=lambda x: app_order.index(x["app_label"]) if x["app_label"] in app_order else len(app_order)) return app_list - -admin_site = AdminSite(name='custom_admin') + + +admin_site = AdminSite(name="custom_admin") diff --git a/src/core/urls/__init__.py b/src/core/urls/__init__.py index 75b61aa38c1..ccee72a863e 100644 --- a/src/core/urls/__init__.py +++ b/src/core/urls/__init__.py @@ -2,8 +2,8 @@ from django.conf.urls import include from django.urls import path -from core.views import HomePageView from core.admin.admin_site import admin_site +from core.views import HomePageView api = [ path("v2/", include("core.urls.v2")), From 4d8478892dd1916230cd8e9e21ed8c36ab52e6f8 Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Tue, 1 Oct 2024 17:22:27 +0500 Subject: [PATCH 3/6] fix after review --- .../tests/orders/services/tests_order_refunder.py | 5 +++++ src/core/admin/admin_site.py | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/apps/orders/tests/orders/services/tests_order_refunder.py b/src/apps/orders/tests/orders/services/tests_order_refunder.py index 9669f5da0e0..c599777546d 100644 --- a/src/apps/orders/tests/orders/services/tests_order_refunder.py +++ b/src/apps/orders/tests/orders/services/tests_order_refunder.py @@ -20,6 +20,11 @@ ] +@pytest.fixture(autouse=True) +def _set_locale(settings): + settings.LANGUAGE_CODE = "en" + + @pytest.fixture(autouse=True) def _adjust_settings(settings): settings.BANKS_REFUNDS_ENABLED = True diff --git a/src/core/admin/admin_site.py b/src/core/admin/admin_site.py index b04c66c214c..a7d50934797 100644 --- a/src/core/admin/admin_site.py +++ b/src/core/admin/admin_site.py @@ -1,6 +1,7 @@ from typing import Any from django.contrib import admin +from django.http.request import HttpRequest class AdminSite(admin.AdminSite): @@ -8,11 +9,18 @@ def __init__(self, name: str) -> None: super().__init__(name=name) self._registry.update(admin.site._registry) - def get_app_list(self, request: Any, app_label: Any | None = None) -> list[Any]: + def get_app_list(self, request: HttpRequest, app_label: str | None = None) -> list[str]: app_list = super().get_app_list(request, app_label) - app_order = ["orders", "notion", "chains", "products", "otherapp"] - app_list.sort(key=lambda x: app_order.index(x["app_label"]) if x["app_label"] in app_order else len(app_order)) + app_list.sort(key=self._get_app_order_index) return app_list + def _get_app_order_index(self, element: Any) -> 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") From 7d261956328304b4f90131701fb91b94fc36ac55 Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Tue, 1 Oct 2024 19:15:56 +0500 Subject: [PATCH 4/6] remove setting locale for test --- .../orders/tests/orders/services/tests_order_refunder.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/apps/orders/tests/orders/services/tests_order_refunder.py b/src/apps/orders/tests/orders/services/tests_order_refunder.py index c599777546d..9669f5da0e0 100644 --- a/src/apps/orders/tests/orders/services/tests_order_refunder.py +++ b/src/apps/orders/tests/orders/services/tests_order_refunder.py @@ -20,11 +20,6 @@ ] -@pytest.fixture(autouse=True) -def _set_locale(settings): - settings.LANGUAGE_CODE = "en" - - @pytest.fixture(autouse=True) def _adjust_settings(settings): settings.BANKS_REFUNDS_ENABLED = True From 72f121e882451841eea3f62d9a32b42c378a1ef7 Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Tue, 1 Oct 2024 19:58:22 +0500 Subject: [PATCH 5/6] fix type for get_app_list method --- src/core/admin/admin_site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/admin/admin_site.py b/src/core/admin/admin_site.py index a7d50934797..02499d9be4e 100644 --- a/src/core/admin/admin_site.py +++ b/src/core/admin/admin_site.py @@ -9,7 +9,7 @@ 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[str]: + 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 From b9570bf09d79023d9a20be4b6221b603b38e645c Mon Sep 17 00:00:00 2001 From: Alex Ebenrode Date: Thu, 3 Oct 2024 08:58:50 +0500 Subject: [PATCH 6/6] fix type --- src/core/admin/admin_site.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/admin/admin_site.py b/src/core/admin/admin_site.py index 02499d9be4e..9eab6be0632 100644 --- a/src/core/admin/admin_site.py +++ b/src/core/admin/admin_site.py @@ -1,5 +1,3 @@ -from typing import Any - from django.contrib import admin from django.http.request import HttpRequest @@ -14,7 +12,8 @@ def get_app_list(self, request: HttpRequest, app_label: str | None = None) -> li app_list.sort(key=self._get_app_order_index) return app_list - def _get_app_order_index(self, element: Any) -> int: + @staticmethod + def _get_app_order_index(element: dict) -> int: app_order = ["orders", "notion", "chains", "products", "otherapp"] if element["app_label"] in app_order: