mirror of
https://github.com/quantum5/qlinks.git
synced 2025-07-26 19:54:11 -04:00
Add utility class to throttle admin emails
This commit is contained in:
parent
1a60694112
commit
8694c0e924
0
qlinks/utils/__init__.py
Normal file
0
qlinks/utils/__init__.py
Normal file
21
qlinks/utils/admin_email.py
Normal file
21
qlinks/utils/admin_email.py
Normal 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)
|
Loading…
Reference in a new issue