-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubdomain_middleware.py
31 lines (30 loc) · 1.39 KB
/
subdomain_middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponseRedirect, Http404
from subdomain_settings import *
class SubdomainMiddleware:
def process_request(self, request):
"""
Intercepts request and processes subdomain.
Only works with a single subdomain (e.g. lol.jk.com, not a.lol.jk.com)
"""
request.subdomain = None
host = request.META.get('HTTP_HOST', '')
host_s = host.replace('www.', '').split('.')
if len(host_s) > 2:
request.subdomain = host_s[0]
if request.subdomain:
# A subdomain exists. First try and match subdomains not in the model
sub = request.subdomain
try:
#try to find a subdomain in this list (not a model subdomain)
request.urlconf = SUBDOMAIN_URLCONFS[sub]
except KeyError:
try:
#try and find a model associated with this subdomain
request.subdomain_object = MODEL_TO_SUBDOMAIN['model'].objects.get(**{MODEL_TO_SUBDOMAIN['filter_field'] : sub})
request.urlconf = MODEL_TO_SUBDOMAIN['url_conf']
except ObjectDoesNotExist:
if REDIRECT_IF_NO_MODEL:
return HttpResponseRedirect(REDIRECT_IF_NO_MODEL)
else:
raise Http404