Initial subdomain implementation

This commit is contained in:
cuom1999 2023-01-23 20:36:44 -06:00
parent dea24f7f71
commit 1628e63084
17 changed files with 194 additions and 46 deletions

View file

@ -2,6 +2,10 @@ from django.conf import settings
from django.http import HttpResponseRedirect
from django.urls import Resolver404, resolve, reverse
from django.utils.http import urlquote
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ObjectDoesNotExist
from judge.models import Organization
class ShortCircuitMiddleware:
@ -82,3 +86,30 @@ class DarkModeMiddleware(object):
reverse("toggle_darkmode") + "?next=" + urlquote(request.path)
)
return self.get_response(request)
class SubdomainMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
domain = request.get_host()
site = get_current_site(request).domain
subdomain = domain[: len(domain) - len(site)]
request.organization = None
if len(subdomain) > 1:
subdomain = subdomain[:-1]
try:
organization = Organization.objects.get(slug=subdomain)
if (
request.profile
and organization in request.profile.organizations.all()
):
request.organization = organization
elif not request.GET.get("next", None):
return HttpResponseRedirect(
reverse("auth_login") + "?next=" + urlquote(request.path)
)
except ObjectDoesNotExist:
pass
return self.get_response(request)