Skip to content

Commit

Permalink
Serve utn.se/admin at admin.utn.se (#798)
Browse files Browse the repository at this point in the history
* A Proof of concept solution to server django admin page at admin.domain.com Can be used to show wagtail admin (probably). Uses 'django-host' to server subdomain

* Changed to wagtail admin page

* Fixed configs so default is used

* fix login paths for admin, redirect /admin to admin subdomain

* fix comments

* support reversing admin paths

* fix tests

---------

Co-authored-by: [ludvigalden](https://github.com/ludvigalden) and [hato1883](https://github.com/hato1883)
  • Loading branch information
hato1883 authored Feb 1, 2024
1 parent fdadf3b commit a2d25f6
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
django_recaptcha==3.0.0
Django[argon2]==3.2.17
wagtail==2.16.2
# Allows subdomains
django-hosts==6.0

# External Libraries
requests==2.28.2
Expand Down
16 changes: 16 additions & 0 deletions src/admin/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from __future__ import absolute_import, unicode_literals

from django.conf.urls import include, url

from wagtail.admin import urls as wagtailadmin_urls

from moore.urls import urlpatterns as base_urlpatterns

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

# Insert `wagtailadmin_urls` at appropriate level
urlpatterns.insert(
len(urlpatterns) - 3,
url(r'', include(wagtailadmin_urls))
)
11 changes: 11 additions & 0 deletions src/admin/views.py
Original file line number Diff line number Diff line change
@@ -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}')
9 changes: 9 additions & 0 deletions src/moore/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
'captcha',
'jsonschemaform',
'django_select2', # Custom select2 widget
'django_hosts', # Subdomain for admin site

'django.contrib.admin', # Used for wagtail admin filters
'django.contrib.auth',
Expand All @@ -79,6 +80,8 @@
]

MIDDLEWARE = [
# Subdomain for admin site. Needed by django_hosts
'django_hosts.middleware.HostsRequestMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand All @@ -89,6 +92,8 @@
'django.middleware.locale.LocaleMiddleware',

'wagtail.contrib.redirects.middleware.RedirectMiddleware',
# Subdomain for admin site. Needed by django_hosts
'django_hosts.middleware.HostsResponseMiddleware',
]

DATABASES = {
Expand All @@ -103,6 +108,10 @@
}

ROOT_URLCONF = 'moore.urls'
# Needed for django hosts, enables us to publish wagtail admin on subdomain.
ROOT_HOSTCONF = 'moore.settings.hosts'
DEFAULT_HOST = 'default'


TEMPLATES = [
{
Expand Down
3 changes: 3 additions & 0 deletions src/moore/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
# trailing slash
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

Expand Down
8 changes: 8 additions & 0 deletions src/moore/settings/hosts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django_hosts import patterns, host
from django.conf import settings

host_patterns = patterns(
'',
host(r'', settings.ROOT_URLCONF, name='default'),
host(r'admin', 'admin.urls', name='admin'),
)
1 change: 1 addition & 0 deletions src/moore/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
BASE_URL = 'https://utn.se'

ALLOWED_HOSTS = ['.utn.se', '.utnarm.se']
PARENT_HOST = 'utn.se'

# Email settings
DEFAULT_FROM_EMAIL = '[email protected]'
Expand Down
27 changes: 22 additions & 5 deletions src/moore/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

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.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

urlpatterns = [

# Needs to be imported before wagtail urls
url(r'^api/', api_router.urls),

# Needs to be imported before wagtail admin
url(r'', include('involvement.urls')),
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<path>.*)$',
redirect_admin,
name='wagtailadmin_redirect'
),

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

url(r'^search/$', search_views.search, name='search'),
Expand All @@ -35,6 +40,10 @@

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:
Expand All @@ -51,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'
)
12 changes: 12 additions & 0 deletions src/moore/urls_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import absolute_import, unicode_literals


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 a2d25f6

Please sign in to comment.