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.
57 lines
1.9 KiB
57 lines
1.9 KiB
"""
|
|
Domain-based URL Configuration Middleware
|
|
|
|
This middleware detects the request domain and routes to the appropriate
|
|
URLconf (URL configuration) for each site:
|
|
- Dovoodi domains → config.urls_dovoodi
|
|
- Imam Javad domains → config.urls_imamjavad
|
|
"""
|
|
|
|
|
|
from django.http.response import HttpResponseRedirectBase
|
|
from django.middleware.common import CommonMiddleware
|
|
from django.utils.http import escape_leading_slashes
|
|
|
|
|
|
class SiteMiddleware:
|
|
"""
|
|
Middleware to route requests to different URL configurations based on domain.
|
|
|
|
This allows each domain to have clean /admin/ URLs instead of path-based
|
|
differentiation (/imam-javad/admin vs /dovoodi/admin).
|
|
"""
|
|
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
|
|
def __call__(self, request):
|
|
# Force Dovoodi URL configuration for all domains
|
|
request.urlconf = 'config.urls_dovoodi'
|
|
return self.get_response(request)
|
|
|
|
|
|
class HttpResponsePermanentRedirect308(HttpResponseRedirectBase):
|
|
"""
|
|
Custom response class for HTTP 308 Permanent Redirect.
|
|
This preserves the original HTTP method (e.g. POST) and payload.
|
|
"""
|
|
status_code = 308
|
|
|
|
|
|
class CommonMiddleware308(CommonMiddleware):
|
|
"""
|
|
Custom CommonMiddleware that performs trailing slash redirects
|
|
using HTTP 308 instead of HTTP 301.
|
|
"""
|
|
response_redirect_class = HttpResponsePermanentRedirect308
|
|
|
|
def get_full_path_with_slash(self, request):
|
|
"""
|
|
Return the full path of the request with a trailing slash appended.
|
|
|
|
Overridden to bypass the settings.DEBUG RuntimeError check. Since we are
|
|
redirecting with an HTTP 308 status code, the client will preserve the HTTP
|
|
method and payload, meaning redirecting POST/PUT/PATCH requests is safe.
|
|
"""
|
|
new_path = request.get_full_path(force_append_slash=True)
|
|
return escape_leading_slashes(new_path)
|