from functools import wraps from django.utils import timezone from apps.core.models import Config, ConfigValueChoices from utils.exceptions import ServiceUnavailable def service_availability(key:str): def with_params(view_func): @wraps(view_func) def wrapper(self, request, *args, **kwargs): print(key) print(f"Executing {view_func.__name__} action") service_is_available = Config.get_value_of(f"SERVICE_IS_AVAILABLE_{key.upper()}", "True", ConfigValueChoices.BOOLEAN) config = Config.objects.get(key=f"SERVICE_IS_AVAILABLE_{key.upper()}") if not service_is_available: raise ServiceUnavailable( detail={ # "code": 'service_unavailable_at_now', # "timestamp": timezone.now().isoformat(), "message": config.description or "این سرویس در حال حاضر در دسترس نیست" }, ) return view_func(self, request, *args, **kwargs) return wrapper return with_params