Skip to content

Commit

Permalink
use middleware instead of re_path
Browse files Browse the repository at this point in the history
  • Loading branch information
ludvigalden committed Mar 6, 2024
1 parent 2033a33 commit 2e1900f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
19 changes: 19 additions & 0 deletions src/admin/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.conf import settings
from django_hosts.resolvers import reverse_host
from django.http import HttpResponseRedirect, HttpResponse
from django.http import HttpRequest
from django.utils.deprecation import MiddlewareMixin


class RedirectAdminMiddleware(MiddlewareMixin):
def process_request(self, request: HttpRequest) -> HttpResponse:
if (request.path.startswith('/admin/') and
request.method == 'GET' and
request.headers.get('Accept', '').startswith('text/html')):
protocol = 'https' if request.is_secure() else 'http'
host = reverse_host('admin')
if getattr(settings, 'HOST_PORT', None):
host = f"{host}:{settings.HOST_PORT}"
path = request.path[7:] # Remove '/admin/'
return HttpResponseRedirect(f'{protocol}://{host}/{path}')
return None
16 changes: 0 additions & 16 deletions src/admin/views.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/moore/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
# Subdomain for admin site. Needed by django_hosts
'django_hosts.middleware.HostsResponseMiddleware',
# Redirect GET html at path "/admin/*" admin subdomain
'admin.middleware.RedirectAdminMiddleware',
]

DATABASES = {
Expand Down
9 changes: 1 addition & 8 deletions src/moore/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings
from django.conf.urls import include, url
from django.urls import path, re_path
from django.urls import path

from search import views as search_views
from wagtail.core import urls as wagtail_urls
Expand All @@ -13,7 +13,6 @@
from .urls_utils import delete_urls

from members.views import member_check_api
from admin.views import redirect_admin

urlpatterns = [
# Needs to be imported before wagtail urls
Expand All @@ -23,12 +22,6 @@
url(r'', include('events.urls')),
path('member_check_api/', member_check_api, name='member_check_api'),

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

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

url(r'^search/$', search_views.search, name='search'),
Expand Down

0 comments on commit 2e1900f

Please sign in to comment.