Add utility class to throttle admin emails

This commit is contained in:
Quantum 2025-07-22 00:44:29 -04:00
parent 1a60694112
commit 8694c0e924
2 changed files with 21 additions and 0 deletions

0
qlinks/utils/__init__.py Normal file
View file

View file

@ -0,0 +1,21 @@
import traceback
from django.conf import settings
from django.core.cache import cache
from django.utils.log import AdminEmailHandler
THROTTLE_VARIABLE = 'qlinks_error_email_throttle'
throttle_limit, throttle_duration = getattr(settings, 'QLINKS_EMAIL_THROTTLE', (2, 60))
class ThrottledEmailHandler(AdminEmailHandler):
def emit(self, record):
try:
cache.add(THROTTLE_VARIABLE, 0, throttle_duration)
count = cache.incr(THROTTLE_VARIABLE)
except Exception:
traceback.print_exc()
else:
if count > throttle_limit:
return
super().emit(record)