diff --git a/README.rst b/README.rst index ec1fa56..455d829 100644 --- a/README.rst +++ b/README.rst @@ -107,3 +107,30 @@ Configuration {'model': 'auth.User', 'label': 'Staff'}, )}, ) + +4. If you override the default admin site object (django.contrib.admin.site), + add the setting `ADMIN_REORDER_SITE` to your settings.py: + + .. code-block:: python + + # my_project/admin.py + from django.contrib import admin + + + class CustomAdminSite(admin.AdminSite): + pass + + custom_admin = CustomAdminSite(name='CustomAdmin') + + # my_project/urls.py + from my_project.admin import custom_admin + + urlpatterns = [ + # ... + path('/admin', custom_admin.urls), + # ... + ] + + + # my_project/settings.py + ADMIN_REORDER_SITE = 'my_project.admin.custom_admin' diff --git a/admin_reorder/middleware.py b/admin_reorder/middleware.py index 9bf0a77..eca2091 100644 --- a/admin_reorder/middleware.py +++ b/admin_reorder/middleware.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals - +import importlib from copy import deepcopy from django.conf import settings @@ -23,6 +23,23 @@ MiddlewareMixin = object +def get_admin_index(request): + """Return the admin index from ADMIN_REORDER_SITE setting. If setting is + not found, return the index of django.contrib.admin.site. + """ + admin_site = getattr(settings, 'ADMIN_REORDER_SITE', None) + + if admin_site is not None: + *admin_module, admin_site = admin_site.split(".") + + admin_module = importlib.import_module('.'.join(admin_module)) + admin_site = getattr(admin_module, admin_site) + + return admin_site.index(request) + + return admin.site.index(request) + + class ModelAdminReorder(MiddlewareMixin): def init_config(self, request, app_list): @@ -39,7 +56,7 @@ def init_config(self, request, app_list): 'ADMIN_REORDER config parameter must be tuple or list. ' 'Got {config}'.format(config=self.config)) - admin_index = admin.site.index(request) + admin_index = get_admin_index(request) try: # try to get all installed models app_list = admin_index.context_data['app_list'] diff --git a/runtests.py b/runtests.py index 396efbe..5bd8f29 100644 --- a/runtests.py +++ b/runtests.py @@ -14,6 +14,7 @@ ROOT_URLCONF="admin_reorder.urls", INSTALLED_APPS=[ "django.contrib.auth", + "django.contrib.admin", "django.contrib.contenttypes", "django.contrib.sites", "admin_reorder", @@ -32,7 +33,8 @@ from django_nose import NoseTestSuiteRunner except ImportError: - raise ImportError("To fix this error, run: pip install -r requirements-test.txt") + raise ImportError( + "To fix this error, run: pip install -r requirements-test.txt") def run_tests(*test_args): diff --git a/tests/test_get_admin_index_custom_admin_site.py b/tests/test_get_admin_index_custom_admin_site.py new file mode 100644 index 0000000..6c155c2 --- /dev/null +++ b/tests/test_get_admin_index_custom_admin_site.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_django-modeladmin-reorder +------------ + +Tests for `django-modeladmin-reorder` get_admin_index function from middleware +module. +""" + +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin, GroupAdmin +from django.contrib.auth.models import User, Group +from django.test import RequestFactory, TestCase +from django.test.utils import override_settings +from django.urls import path + +from admin_reorder import middleware +import admin_reorder + + +class CustomAdminSite(admin.AdminSite): + pass + + +custom_admin = CustomAdminSite(name='CustomAdmin') +custom_admin.register(User, UserAdmin) +custom_admin.register(Group, GroupAdmin) +admin_reorder.custom_admin = custom_admin +urlpatterns = [path('', custom_admin.urls), ] + + +@override_settings( + ROOT_URLCONF=__name__, + ADMIN_REORDER_SITE='admin_reorder.custom_admin') +class Testadmin_reorder(TestCase): + def setUp(self): + self.factory = RequestFactory() + self.user = User.objects.create_superuser( + 'admin', 'foo@foo.com', 'admin') + + def test_get_admin_index_for_default_admin_site(self): + request = self.factory.get('/') + request.user = self.user + admin_index = middleware.get_admin_index(request) + self.assertEqual( + custom_admin.index(request).context_data, + admin_index.context_data + ) diff --git a/tests/test_get_admin_index_default_admin_site.py b/tests/test_get_admin_index_default_admin_site.py new file mode 100644 index 0000000..1dc9c5d --- /dev/null +++ b/tests/test_get_admin_index_default_admin_site.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +test_django-modeladmin-reorder +------------ + +Tests for `django-modeladmin-reorder` get_admin_index function from middleware +module. +""" + +from django.contrib import admin +from django.contrib.auth.models import User +from django.test import RequestFactory, TestCase +from django.test.utils import override_settings +from django.urls import path + +from admin_reorder import middleware + + +urlpatterns = [path('', admin.site.urls), ] + + +@override_settings(ROOT_URLCONF=__name__) +class Testadmin_reorder(TestCase): + def setUp(self): + self.factory = RequestFactory() + self.user = User.objects.create_superuser( + 'admin', 'foo@foo.com', 'admin') + + def test_get_admin_index_for_default_admin_site(self): + request = self.factory.get('/') + request.user = self.user + admin_index = middleware.get_admin_index(request) + self.assertEqual( + admin.site.index(request).context_data, + admin_index.context_data + )