| 1 |
import re |
| 2 |
import sys |
| 3 |
from types import ModuleType |
| 4 |
from typing import Any, Literal |
| 5 |
|
| 6 |
from django.conf import settings |
| 7 |
from django.urls import URLResolver |
| 8 |
|
| 9 |
from django_pgschemas.routing.info import DomainInfo, HeadersInfo, SessionInfo |
| 10 |
from django_pgschemas.schema import Schema, get_current_schema |
| 11 |
|
| 12 |
DYNAMIC_URLCONF_SUFFIX = "_dynamically_tenant_prefixed" |
| 13 |
|
| 14 |
|
| 15 |
class TenantPrefixPattern: |
| 16 |
converters: dict = {} |
| 17 |
|
| 18 |
@property |
| 19 |
def tenant_prefix(self) -> str: |
| 20 |
current_schema = get_current_schema() |
| 21 |
return ( |
| 22 |
f"{current_schema.routing.folder}/" |
| 23 |
if isinstance(current_schema.routing, DomainInfo) and current_schema.routing.folder |
| 24 |
else "" |
| 25 |
) |
| 26 |
|
| 27 |
@property |
| 28 |
def regex(self) -> re.Pattern: |
| 29 |
# This is only used by reverse() and cached in _reverse_dict. |
| 30 |
return re.compile(self.tenant_prefix) |
| 31 |
|
| 32 |
def match(self, path: str) -> tuple | None: |
| 33 |
tenant_prefix = self.tenant_prefix |
| 34 |
if path.startswith(tenant_prefix): |
| 35 |
return path[len(tenant_prefix) :], (), {} |
| 36 |
return None |
| 37 |
|
| 38 |
def check(self) -> list: |
| 39 |
return [] |
| 40 |
|
| 41 |
def describe(self) -> str: |
| 42 |
return f"'{self}'" |
| 43 |
|
| 44 |
def __str__(self) -> str: |
| 45 |
return self.tenant_prefix |
| 46 |
|
| 47 |
|
| 48 |
def get_dynamic_tenant_prefixed_urlconf(urlconf: str, dynamic_path: str) -> ModuleType: |
| 49 |
""" |
| 50 |
Generates a new urlconf module with all patterns prefixed with tenant. |
| 51 |
""" |
| 52 |
|
| 53 |
class LazyURLConfModule(ModuleType): |
| 54 |
def __getattr__(self, attr: str) -> Any: |
| 55 |
if attr == "urlpatterns": |
| 56 |
return [URLResolver(TenantPrefixPattern(), urlconf)] |
| 57 |
return self.__getattribute__(attr) |
| 58 |
|
| 59 |
return LazyURLConfModule(dynamic_path) |
| 60 |
|
| 61 |
|
| 62 |
def _get_urlconf_from_schema( |
| 63 |
schema: Schema, config_key: Literal["URLCONF", "WS_URLCONF"] |
| 64 |
) -> str | None: |
| 65 |
match schema.routing: |
| 66 |
case DomainInfo(domain, _): |
| 67 |
# Checking for static tenants |
| 68 |
if not schema.is_dynamic: |
| 69 |
for schema_name, data in settings.TENANTS.items(): |
| 70 |
if schema_name in ["public", "default"]: |
| 71 |
continue |
| 72 |
if domain in data.get("DOMAINS", []): |
| 73 |
return data.get(config_key) |
| 74 |
if domain in data.get("FALLBACK_DOMAINS", []): |
| 75 |
return data.get(config_key) |
| 76 |
return None |
| 77 |
|
| 78 |
# Checking for dynamic tenants |
| 79 |
urlconf = settings.TENANTS.get("default", {}).get(config_key) |
| 80 |
if urlconf is not None and schema.routing.folder: |
| 81 |
dynamic_path = urlconf + DYNAMIC_URLCONF_SUFFIX |
| 82 |
if not sys.modules.get(dynamic_path): |
| 83 |
sys.modules[dynamic_path] = get_dynamic_tenant_prefixed_urlconf( |
| 84 |
urlconf, dynamic_path |
| 85 |
) |
| 86 |
urlconf = dynamic_path |
| 87 |
|
| 88 |
return urlconf |
| 89 |
|
| 90 |
case SessionInfo(reference): |
| 91 |
# Checking for static tenants |
| 92 |
if not schema.is_dynamic: |
| 93 |
for schema_name, data in settings.TENANTS.items(): |
| 94 |
if schema_name in ["public", "default"]: |
| 95 |
continue |
| 96 |
if reference == data.get("SESSION_KEY"): |
| 97 |
return data.get(config_key) |
| 98 |
return None |
| 99 |
|
| 100 |
# Checking for dynamic tenants |
| 101 |
return settings.TENANTS.get("default", {}).get(config_key) |
| 102 |
|
| 103 |
case HeadersInfo(reference): |
| 104 |
# Checking for static tenants |
| 105 |
if not schema.is_dynamic: |
| 106 |
for schema_name, data in settings.TENANTS.items(): |
| 107 |
if schema_name in ["public", "default"]: |
| 108 |
continue |
| 109 |
if reference == data.get("HEADER"): |
| 110 |
return data.get(config_key) |
| 111 |
return None |
| 112 |
|
| 113 |
# Checking for dynamic tenants |
| 114 |
return settings.TENANTS.get("default", {}).get(config_key) |
| 115 |
|
| 116 |
case _: |
| 117 |
return None |
| 118 |
|
| 119 |
|
| 120 |
def get_urlconf_from_schema(schema: Schema) -> str | None: |
| 121 |
return _get_urlconf_from_schema(schema, "URLCONF") |
| 122 |
|
| 123 |
|
| 124 |
def get_ws_urlconf_from_schema(schema: Schema) -> str | None: |
| 125 |
return _get_urlconf_from_schema(schema, "WS_URLCONF") |