Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Work with custom admin site model #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'
21 changes: 19 additions & 2 deletions admin_reorder/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import importlib
from copy import deepcopy

from django.conf import settings
Expand All @@ -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):
Expand All @@ -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']
Expand Down
4 changes: 3 additions & 1 deletion runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):
Expand Down
50 changes: 50 additions & 0 deletions tests/test_get_admin_index_custom_admin_site.py
Original file line number Diff line number Diff line change
@@ -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', '[email protected]', '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
)
38 changes: 38 additions & 0 deletions tests/test_get_admin_index_default_admin_site.py
Original file line number Diff line number Diff line change
@@ -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', '[email protected]', '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
)