diff --git a/Watcher/Watcher/accounts/admin.py b/Watcher/Watcher/accounts/admin.py index cf8c911..49a8dbb 100644 --- a/Watcher/Watcher/accounts/admin.py +++ b/Watcher/Watcher/accounts/admin.py @@ -11,7 +11,8 @@ from django.utils import timezone from datetime import timedelta from knox.models import AuthToken - +from django.db.models.signals import post_delete +from django.dispatch import receiver """ Log Entries Snippet @@ -289,6 +290,15 @@ def key_details(self, obj): admin.site.register(APIKey, APIKeyAdmin) +@receiver(post_delete, sender=APIKey) +def delete_authtoken_when_apikey_deleted(sender, instance, **kwargs): + try: + if instance.auth_token: + instance.auth_token.delete() + except AuthToken.DoesNotExist: + pass + + class AuthTokenAdmin(admin.ModelAdmin): list_display = ('user', 'digest', 'created', 'expiry') readonly_fields = ('user', 'digest', 'created', 'expiry') diff --git a/Watcher/Watcher/accounts/models.py b/Watcher/Watcher/accounts/models.py index e27fea8..a1eeaf9 100644 --- a/Watcher/Watcher/accounts/models.py +++ b/Watcher/Watcher/accounts/models.py @@ -2,8 +2,7 @@ from django_auth_ldap.backend import populate_user from django.contrib.auth.models import User from knox.models import AuthToken -from django.db.models.signals import post_delete -from django.dispatch import receiver + class APIKey(models.Model): """ @@ -19,14 +18,6 @@ class Meta: verbose_name_plural = "API Keys" app_label = 'auth' -@receiver(post_delete, sender=APIKey) -def delete_authtoken_when_apikey_deleted(sender, instance, **kwargs): - try: - if instance.auth_token: - instance.auth_token.delete() - except AuthToken.DoesNotExist: - pass - def make_inactive(sender, user, **kwargs): if not User.objects.filter(username=user.username):