Skip to content

Commit

Permalink
support reversing admin paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ludvigalden committed Jan 25, 2024
1 parent 59c2f3a commit 816ce91
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/admin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

from django.conf.urls import include, url

from wagtail.core import urls as wagtail_urls
from wagtail.admin import urls as wagtailadmin_urls

from moore.urls import urlpatterns as base_urlpatterns
from moore.urls_utils import insert_urls_before

# Use the same `urlpatterns` as other domains as the base
urlpatterns = base_urlpatterns.copy()

# Append `wagtailadmin_urls` before `wagtail_urls`
for index, pattern in enumerate(urlpatterns):
if hasattr(pattern, 'url_patterns'):
if wagtail_urls.urlpatterns == pattern.url_patterns:
# Insert the new_pattern before the wagtail include
urlpatterns.insert(index, url(r'', include(wagtailadmin_urls)))
break
# Insert `wagtailadmin_urls` before `wagtail_urls`
urlpatterns = insert_urls_before(
urlpatterns,
before_name='wagtail_urls',
url=url(r'', include(wagtailadmin_urls))
)
22 changes: 20 additions & 2 deletions src/moore/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from search import views as search_views
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.admin import urls as wagtailadmin_urls

from .api import api_router
from .urls_utils import delete_urls

from members.views import member_check_api
from admin.views import redirect_admin
Expand All @@ -21,7 +23,11 @@
url(r'', include('events.urls')),
path('member_check_api/', member_check_api, name='member_check_api'),

re_path(r'^admin/(?P<path>.*)$', redirect_admin),
re_path(
r'^admin/(?P<path>.*)$',
redirect_admin,
name='wagtailadmin_redirect'
),

url(r'^documents/', include(wagtaildocs_urls)),

Expand All @@ -34,10 +40,14 @@

path('instagram/', include('instagram.urls')),

# We need to include the `wagtailadmin_urls` to support `reverse`.
# Unless running tests, /admin/* will redirect to admin.x/*.
url(r'^admin/', include(wagtailadmin_urls)),

# For anything not caught by a more specific rule above, hand over to
# Wagtail's page serving mechanism. This should be the last pattern in
# the list:
url(r'', include(wagtail_urls)),
url(r'', include((wagtail_urls, 'wagtail_urls'))),
]

if settings.DEBUG:
Expand All @@ -50,3 +60,11 @@
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)

# We remove the /admin redirect
# if running tests in order to make writing tests easier.
if settings.IS_RUNNING_TEST:
urlpatterns = delete_urls(
urlpatterns,
delete_name='wagtailadmin_redirect'
)
29 changes: 29 additions & 0 deletions src/moore/urls_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import absolute_import, unicode_literals

from django.urls.resolvers import URLResolver


def insert_urls_before(urlpatterns: list, before_name: str, url: URLResolver):
for index, pattern in enumerate(urlpatterns):
# Insert before index if `name` or `app_name` matches
if hasattr(pattern, 'name'):
if pattern.name == before_name:
urlpatterns.insert(index, url)
break
elif hasattr(pattern, 'app_name'):
if pattern.app_name == before_name:
urlpatterns.insert(index, url)
break

return urlpatterns


def delete_urls(urlpatterns: list, delete_name: str):
for index, pattern in enumerate(urlpatterns):
if hasattr(pattern, 'name'):
if pattern.name == delete_name:
# Insert before index
urlpatterns.pop(index)
break

return urlpatterns

0 comments on commit 816ce91

Please sign in to comment.