Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom admin site for reordering app list #2421

Merged
merged 7 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/core/admin/admin_site.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 2 additions & 2 deletions src/core/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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()),
]
Loading