django scopes x django sites framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

from django.http import Http404
2 years ago
from django_scopes import scope
from scopedsites.models import Domain
class DomainScopeMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
2 years ago
d = Domain.get_from_request(request) # technically does hit the db layer, but should get caught in memcache
# TODO: performance testing
2 years ago
with scope(domain=d):
response = self.get_response(request)
return response
class DomainAutoCreateMiddleware:
def __init__(self, get_response):
self.get_response = get_response
2 years ago
self.cache = set()
def ensure_exists(self, r):
if (host := r.META.get("HTTP_HOST")) in self.cache:
return
try:
2 years ago
Domain.get_from_request(r)
self.cache.add(host)
except Http404:
2 years ago
Domain.objects.create(fqdn=r.META.get('HTTP_HOST'))
2 years ago
def __call__(self, request):
self.ensure_exists(request)
return self.get_response(request)