chat/apps/core/decorators.py
Sayyid Hamid Mahdavi d596e319f0 init
2026-05-10 15:25:49 +03:30

25 lines
895 B
Python

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(config.description or "این سرویس در حال حاضر در دسترس نیست")
return view_func(self, request, *args, **kwargs)
return wrapper
return with_params