Change context behavior setting to enum

This commit is contained in:
Dylan Castillo 2024-01-15 11:11:31 +01:00 committed by Emil Stenström
parent 8971849922
commit 560b721523
5 changed files with 48 additions and 9 deletions

View file

@ -1,6 +1,13 @@
from enum import Enum
from django.conf import settings
class ContextBehavior(Enum):
GLOBAL = "global"
ISOLATED = "isolated"
class AppSettings:
def __init__(self):
self.settings = getattr(settings, "COMPONENTS", {})
@ -18,8 +25,20 @@ class AppSettings:
return self.settings.setdefault("template_cache_size", 128)
@property
def ISOLATE_COMPONENT_CONTEXT(self):
return self.settings.setdefault("isolate_component_context", False)
def CONTEXT_BEHAVIOR(self):
raw_value = self.settings.setdefault(
"context_behavior", ContextBehavior.GLOBAL.value
)
return self._validate_context_behavior(raw_value)
def _validate_context_behavior(self, raw_value):
try:
return ContextBehavior(raw_value)
except ValueError:
valid_values = [behavior.value for behavior in ContextBehavior]
raise ValueError(
f"Invalid context behavior: {raw_value}. Valid options are {valid_values}"
)
app_settings = AppSettings()