From 5e853aaf1916ddc8e730a8586e8310d840525942 Mon Sep 17 00:00:00 2001 From: Sepehr Akbarzadeh <sa.goldeneagle@gmail.com> Date: Thu, 19 Dec 2024 21:47:03 +0330 Subject: [PATCH] fix(sage_otp): enhance admin --- pyproject.toml | 2 +- sage_otp/admin.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 648a557..ee7ac4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "django-sage-otp" -version = "0.1.2" +version = "0.1.3" description = "A Django package for managing Otp" authors = ["Sepehr Akbarzadeh <sepehr@sageteam.org>","Radin Ghahremani <radin@sageteam.org>", ] readme = "README.md" diff --git a/sage_otp/admin.py b/sage_otp/admin.py index e4f1c89..c0191df 100644 --- a/sage_otp/admin.py +++ b/sage_otp/admin.py @@ -1,10 +1,15 @@ from django.contrib import admin -from django.utils.translation import gettext_lazy as _ +from django.conf import settings from django.utils.timezone import now +from django.utils.translation import gettext_lazy as _ from sage_otp.models import OTP +from sage_otp.helpers.choices import OTPState from datetime import timedelta +OTP_LIFETIME = getattr(settings, "OTP_LIFETIME", 120) + + @admin.register(OTP) class OTPAdmin(admin.ModelAdmin): """ @@ -80,7 +85,6 @@ def is_expired(self, obj): """ Check if the OTP has expired. """ - OTP_LIFETIME = 300 # Example: 5 minutes return (obj.created_at + timedelta(seconds=OTP_LIFETIME)) < now() @admin.display(description="Expires At") @@ -88,7 +92,6 @@ def expire_at(self, obj): """ Calculate and display the expiration time of the OTP. """ - OTP_LIFETIME = 300 # Example: 5 minutes return obj.created_at + timedelta(seconds=OTP_LIFETIME) @admin.action(description="Expire Active Tokens") @@ -98,8 +101,8 @@ def expire_active_tokens(self, request, queryset): """ updated_count = 0 for otp in queryset: - if otp.state == "active" and self.is_expired(otp): - otp.state = "expired" + if otp.state == OTPState.ACTIVE and self.is_expired(otp): + otp.state = OTPState.EXPIRED otp.save() updated_count += 1