Backend Draft

This commit is contained in:
__init__
2026-02-23 20:31:53 +05:30
commit eec700af51
127 changed files with 2356 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
from django.http import JsonResponse
from tenants.models import Tenant
class TenantMiddleware:
def __init__(self, get_response):
self.get_response = get_response
self.exempt_paths = [
'/admin/',
'/api/schema/',
'/api/docs/',
'/api/auth/login/',
'/api/auth/register/',
'/api/auth/token/refresh/',
'/api/auth/profile/',
]
def __call__(self, request):
if any(request.path.startswith(path) for path in self.exempt_paths):
request.tenant = None
return self.get_response(request)
# 1. Check Header
tenant_id = request.headers.get('X-Tenant-ID')
if not tenant_id:
# 2. Check Subdomain (Optional, skipping for now, can implement later)
# host = request.get_host().split(':')[0]
# subdomain = host.split('.')[0]
pass
if tenant_id:
try:
request.tenant = Tenant.objects.get(id=tenant_id, is_active=True)
except Tenant.DoesNotExist:
return JsonResponse({"detail": "Invalid or inactive tenant ID supplied."}, status=403)
else:
# Normally we might enforce tenant_id, but we'll let permission classes handle it.
request.tenant = None
return self.get_response(request)