Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsocol committed Dec 5, 2023
1 parent 8883703 commit 98334a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
27 changes: 17 additions & 10 deletions adminplus/sites.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from collections.abc import Sequence
from collections import namedtuple
import inspect
from typing import Union
from typing import Any, Callable, NewType, Sequence, Union

from django.contrib.admin.sites import AdminSite
from django.urls import URLPattern, URLResolver, path
from django.urls import URLPattern, URLResolver, path, re_path
from django.utils.text import capfirst
from django.views.generic import View


_FuncT = NewType('_FuncT', Callable[..., Any])

AdminView = namedtuple('AdminView',
['path', 'view', 'name', 'urlname', 'visible'])


def is_class_based_view(view):
return inspect.isclass(view) and issubclass(view, View)

Expand All @@ -18,11 +24,11 @@ class AdminPlusMixin(object):
index_template = 'adminplus/index.html' # That was easy.

def __init__(self, *args, **kwargs):
self.custom_views = []
self.custom_views: list[AdminView] = []
return super().__init__(*args, **kwargs)

def register_view(self, path, name=None, urlname=None, visible=True,
view=None):
def register_view(self, slug, name=None, urlname=None, visible=True,
view=None) -> Union[None, Callable[[_FuncT], _FuncT]]:
"""Add a custom admin view. Can be used as a function or a decorator.
* `path` is the path in the admin where the view will live, e.g.
Expand All @@ -35,10 +41,11 @@ def register_view(self, path, name=None, urlname=None, visible=True,
the custom view should be visible in the admin dashboard or not.
* `view` is any view function you can imagine.
"""
def decorator(fn):
def decorator(fn: _FuncT):
if is_class_based_view(fn):
fn = fn.as_view()
self.custom_views.append((path, fn, name, urlname, visible))
self.custom_views.append(
AdminView(slug, fn, name, urlname, visible))
return fn
if view is not None:
decorator(view)
Expand All @@ -48,9 +55,9 @@ def decorator(fn):
def get_urls(self) -> Sequence[Union[URLPattern, URLResolver]]:
"""Add our custom views to the admin urlconf."""
urls: list[Union[URLPattern, URLResolver]] = super().get_urls()
for slug, view, _, urlname, _ in self.custom_views:
for av in self.custom_views:
urls.insert(
0, path(r'^%s$' % slug, self.admin_view(view), name=urlname))
0, re_path('^%s$' % av.path, self.admin_view(av.view), name=av.urlname))
return urls

def index(self, request, extra_context=None):
Expand Down
13 changes: 8 additions & 5 deletions adminplus/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def foo(request):

urls = site.get_urls()

# the default admin contains a catchall view, so each will match 2
foo_urls = [u for u in urls if u.resolve('foo')]
self.assertEqual(1, len(foo_urls))
self.assertEqual(2, len(foo_urls))
bar_urls = [u for u in urls if u.resolve('bar/baz')]
self.assertEqual(1, len(bar_urls))
self.assertEqual(2, len(bar_urls))
qux_urls = [u for u in urls if u.resolve('baz-qux')]
self.assertEqual(1, len(qux_urls))
self.assertEqual(2, len(qux_urls))

def test_urlname(self):
"""Set URL pattern names correctly."""
Expand All @@ -73,11 +74,13 @@ def bar(request):

urls = site.get_urls()
foo_urls = [u for u in urls if u.resolve('foo')]
self.assertEqual(1, len(foo_urls))

# the default admin contains a catchall view, so this will capture two
self.assertEqual(2, len(foo_urls))
self.assertEqual('foo', foo_urls[0].name)

bar_urls = [u for u in urls if u.resolve('bar')]
self.assertEqual(1, len(bar_urls))
self.assertEqual(2, len(bar_urls))
assert bar_urls[0].name is None

def test_base_template(self):
Expand Down

0 comments on commit 98334a4

Please sign in to comment.