From 2c3e571a4c8c4c5beee451163a9c4abeb458e75e Mon Sep 17 00:00:00 2001 From: ludvigalden Date: Thu, 25 Jan 2024 10:57:34 +0100 Subject: [PATCH] fix login paths for admin, redirect /admin to admin subdomain --- src/admin/urls.py | 19 +++++++++++++++---- src/admin/views.py | 11 +++++++++++ src/moore/settings/dev.py | 2 ++ src/moore/settings/production.py | 1 + src/moore/urls.py | 9 +++++---- 5 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 src/admin/views.py diff --git a/src/admin/urls.py b/src/admin/urls.py index fc7146f6..f0ba73d0 100644 --- a/src/admin/urls.py +++ b/src/admin/urls.py @@ -1,7 +1,18 @@ +from __future__ import absolute_import, unicode_literals +from django.conf.urls import include, url + +from wagtail.core import urls as wagtail_urls from wagtail.admin import urls as wagtailadmin_urls -from django.urls import path, include +from moore.urls import urlpatterns as base_urlpatterns + +# Use the same `urlpatterns` as other domains as the base +urlpatterns = base_urlpatterns.copy() -urlpatterns = [ - path(r'', include(wagtailadmin_urls)), -] +# 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 diff --git a/src/admin/views.py b/src/admin/views.py new file mode 100644 index 00000000..94c6a408 --- /dev/null +++ b/src/admin/views.py @@ -0,0 +1,11 @@ +from django.conf import settings +from django_hosts.resolvers import reverse_host +from django.http import HttpResponseRedirect + + +def redirect_admin(request, path): + protocol = 'https' if request.is_secure() else 'http' + host = reverse_host(host='admin') + if getattr(settings, 'HOST_PORT', None): + host = f"{host}:{settings.HOST_PORT}" + return HttpResponseRedirect(f'{protocol}://{host}/{path}') diff --git a/src/moore/settings/dev.py b/src/moore/settings/dev.py index 7939ef09..b25625df 100644 --- a/src/moore/settings/dev.py +++ b/src/moore/settings/dev.py @@ -53,6 +53,8 @@ BASE_URL = 'http://localhost:8000' ALLOWED_HOSTS = ['admin.localhost', 'localhost'] +PARENT_HOST = 'localhost' +HOST_PORT = '8000' # Email # https://docs.djangoproject.com/en/1.10/ref/settings/#email-backend diff --git a/src/moore/settings/production.py b/src/moore/settings/production.py index c8c40ff9..bf5be084 100644 --- a/src/moore/settings/production.py +++ b/src/moore/settings/production.py @@ -40,6 +40,7 @@ BASE_URL = 'https://utn.se' ALLOWED_HOSTS = ['.utn.se', '.utnarm.se'] +PARENT_HOST = 'utn.se' # Email settings DEFAULT_FROM_EMAIL = 'info@utn.se' diff --git a/src/moore/urls.py b/src/moore/urls.py index f901be1f..13efd1fa 100644 --- a/src/moore/urls.py +++ b/src/moore/urls.py @@ -2,19 +2,19 @@ from django.conf import settings from django.conf.urls import include, url -from django.urls import path +from django.urls import path, re_path from search import views as search_views -from wagtail.admin import urls as wagtailadmin_urls +# from wagtail.admin import urls as wagtailadmin_urls from wagtail.core import urls as wagtail_urls from wagtail.documents import urls as wagtaildocs_urls from .api import api_router from members.views import member_check_api +from admin.views import redirect_admin urlpatterns = [ - # Needs to be imported before wagtail urls url(r'^api/', api_router.urls), @@ -23,7 +23,8 @@ url(r'', include('events.urls')), path('member_check_api/', member_check_api, name='member_check_api'), - url(r'^admin/', include(wagtailadmin_urls)), + re_path(r'^admin/(?P.*)$', redirect_admin), + url(r'^documents/', include(wagtaildocs_urls)), url(r'^search/$', search_views.search, name='search'),