mirror of
https://github.com/jazzband/django-axes.git
synced 2026-05-11 09:03:12 +00:00
Add signals for setting/deleting cache keys
This commit is contained in:
parent
2357a4616b
commit
5b791f65f4
1 changed files with 19 additions and 1 deletions
|
|
@ -2,8 +2,10 @@ from django.dispatch import receiver
|
|||
from django.dispatch import Signal
|
||||
from django.utils.timezone import now
|
||||
from django.contrib.auth.signals import user_logged_out
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.core.cache import cache
|
||||
|
||||
from axes.models import AccessLog
|
||||
from axes.models import AccessLog, AccessAttempt
|
||||
from axes.settings import DISABLE_ACCESS_LOG
|
||||
|
||||
|
||||
|
|
@ -26,3 +28,19 @@ if not DISABLE_ACCESS_LOG:
|
|||
access_log = access_logs[0]
|
||||
access_log.logout_time = now()
|
||||
access_log.save()
|
||||
|
||||
|
||||
@receiver(post_save, sender=AccessAttempt)
|
||||
def update_cache_after_save(instance, **kwargs):
|
||||
from axes.decorators import get_cache_timeout, get_cache_key
|
||||
cache_hash_key = get_cache_key(instance)
|
||||
if not cache.get(cache_hash_key):
|
||||
cache_timeout = get_cache_timeout()
|
||||
cache.set(cache_hash_key, instance.failures_since_start, cache_timeout)
|
||||
|
||||
|
||||
@receiver(post_delete, sender=AccessAttempt)
|
||||
def delete_cache_after_delete(instance, **kwargs):
|
||||
from axes.decorators import get_cache_key
|
||||
cache_hash_key = get_cache_key(instance)
|
||||
cache.delete(cache_hash_key)
|
||||
|
|
|
|||
Loading…
Reference in a new issue