Browse Source

Refactor admin namespace logic into middleware

- Moved the `get_admin_namespace` function from `admin_access.py` to a new `config.middleware` module for better organization and reusability.
- Updated `AdminAccessMiddleware` to utilize the centralized `get_admin_namespace` function, enhancing code maintainability.
- Improved documentation for the `get_admin_namespace` function to clarify its usage and purpose.
master
mortezaei 3 months ago
parent
commit
8ffeb504bb
  1. 15
      apps/account/middleware/admin_access.py
  2. 22
      config/middleware/__init__.py

15
apps/account/middleware/admin_access.py

@ -5,20 +5,7 @@ from django.shortcuts import redirect
from django.urls import reverse
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
def get_admin_namespace(request):
"""
Determine the admin namespace based on the request domain.
Returns the appropriate admin namespace for use in reverse() calls.
"""
host = request.get_host()
# Check if the request is from Dovoodi domain
if 'dovodi' in host or 'dovoodi' in host:
return 'dovoodi_admin'
else:
return 'imam_javad_admin'
from config.middleware import get_admin_namespace
class AdminAccessMiddleware:

22
config/middleware/__init__.py

@ -0,0 +1,22 @@
"""
Middleware utilities and helpers
"""
def get_admin_namespace(request):
"""
Determine the admin namespace based on the request domain.
Returns the appropriate admin namespace for use in reverse() calls.
Usage:
from config.middleware import get_admin_namespace
admin_ns = get_admin_namespace(request)
url = reverse(f'{admin_ns}:model_changelist')
"""
host = request.get_host()
# Check if the request is from Dovoodi domain
if 'dovodi' in host or 'dovoodi' in host:
return 'dovoodi_admin'
else:
return 'imam_javad_admin'
Loading…
Cancel
Save