ipg/apps/core/decorators.py
Sayyid Hamid Mahdavi da7d25d29e config
2026-03-29 13:40:20 +03:30

31 lines
1.1 KiB
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(
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