Skip to content

Commit

Permalink
fix(sage_otp): enhance admin
Browse files Browse the repository at this point in the history
  • Loading branch information
sepehr-akbarzadeh committed Dec 19, 2024
1 parent e297f2a commit 5e853aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>","Radin Ghahremani <[email protected]>", ]
readme = "README.md"
Expand Down
13 changes: 8 additions & 5 deletions sage_otp/admin.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down Expand Up @@ -80,15 +85,13 @@ 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")
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")
Expand All @@ -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

Expand Down

0 comments on commit 5e853aa

Please sign in to comment.