From 87f9b3a4b95b358fb0e543614e19d441463ee597 Mon Sep 17 00:00:00 2001 From: Chneemann Date: Thu, 27 Mar 2025 08:34:37 +0100 Subject: [PATCH] refactor: moved ExpiringToken to models.py --- auth_app/models.py | 14 +++++++++++++- auth_app/views.py | 17 ++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/auth_app/models.py b/auth_app/models.py index 71a8362..168c408 100644 --- a/auth_app/models.py +++ b/auth_app/models.py @@ -1,3 +1,15 @@ +from django.utils import timezone +from rest_framework.authtoken.models import Token from django.db import models +from join.settings import TOKEN_EXPIRATION_TIME -# Create your models here. +class ExpiringToken(Token): + expires_at = models.DateTimeField(null=True, blank=True) + + def is_expired(self): + return self.expires_at and self.expires_at < timezone.now() + + def save(self, *args, **kwargs): + if not self.expires_at: + self.expires_at = timezone.now() + TOKEN_EXPIRATION_TIME + super().save(*args, **kwargs) \ No newline at end of file diff --git a/auth_app/views.py b/auth_app/views.py index fd40eaa..1624730 100644 --- a/auth_app/views.py +++ b/auth_app/views.py @@ -1,14 +1,12 @@ -from django.shortcuts import render, redirect from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status, authentication -from rest_framework.authtoken.models import Token from django.contrib.auth import authenticate from join.settings import TOKEN_EXPIRATION_TIME from .serializer import LoginSerializer from rest_framework.permissions import IsAuthenticated -from django.db import models from django.utils import timezone +from .models import ExpiringToken class LoginView(APIView): @@ -72,15 +70,4 @@ class AuthView(APIView): token.delete() return Response({"error": "Token expired, please log in again"}, status=status.HTTP_401_UNAUTHORIZED) - return Response({"user_id": request.user.id}, status=status.HTTP_200_OK) - -class ExpiringToken(Token): - expires_at = models.DateTimeField(null=True, blank=True) - - def is_expired(self): - return self.expires_at and self.expires_at < timezone.now() - - def save(self, *args, **kwargs): - if not self.expires_at: - self.expires_at = timezone.now() + TOKEN_EXPIRATION_TIME - super().save(*args, **kwargs) \ No newline at end of file + return Response({"user_id": request.user.id}, status=status.HTTP_200_OK) \ No newline at end of file