Prechádzať zdrojové kódy

Add last_used to Token model and update when used

Pieter Lambrecht 3 rokov pred
rodič
commit
c04b4bbbfa

+ 5 - 0
netbox/netbox/api/authentication.py

@@ -1,4 +1,5 @@
 from django.conf import settings
 from django.conf import settings
+from django.utils import timezone
 from rest_framework import authentication, exceptions
 from rest_framework import authentication, exceptions
 from rest_framework.permissions import BasePermission, DjangoObjectPermissions, SAFE_METHODS
 from rest_framework.permissions import BasePermission, DjangoObjectPermissions, SAFE_METHODS
 
 
@@ -18,6 +19,10 @@ class TokenAuthentication(authentication.TokenAuthentication):
         except model.DoesNotExist:
         except model.DoesNotExist:
             raise exceptions.AuthenticationFailed("Invalid token")
             raise exceptions.AuthenticationFailed("Invalid token")
 
 
+        # Update last used.
+        token.last_used = timezone.now()
+        token.save()
+
         # Enforce the Token's expiration time, if one has been set.
         # Enforce the Token's expiration time, if one has been set.
         if token.is_expired:
         if token.is_expired:
             raise exceptions.AuthenticationFailed("Token expired")
             raise exceptions.AuthenticationFailed("Token expired")

+ 1 - 1
netbox/users/admin/__init__.py

@@ -58,7 +58,7 @@ class UserAdmin(UserAdmin_):
 class TokenAdmin(admin.ModelAdmin):
 class TokenAdmin(admin.ModelAdmin):
     form = forms.TokenAdminForm
     form = forms.TokenAdminForm
     list_display = [
     list_display = [
-        'key', 'user', 'created', 'expires', 'write_enabled', 'description'
+        'key', 'user', 'created', 'expires', 'last_used', 'write_enabled', 'description'
     ]
     ]
 
 
 
 

+ 18 - 0
netbox/users/migrations/0003_token_last_used.py

@@ -0,0 +1,18 @@
+# Generated by Django 4.0.4 on 2022-06-16 15:26
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('users', '0002_standardize_id_fields'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='token',
+            name='last_used',
+            field=models.DateTimeField(blank=True, null=True),
+        ),
+    ]

+ 4 - 0
netbox/users/models.py

@@ -203,6 +203,10 @@ class Token(models.Model):
         blank=True,
         blank=True,
         null=True
         null=True
     )
     )
+    last_used = models.DateTimeField(
+        blank=True,
+        null=True
+    )
     key = models.CharField(
     key = models.CharField(
         max_length=40,
         max_length=40,
         unique=True,
         unique=True,