Files
customer-portal/customer_portal/models/otp.py

37 lines
1.1 KiB
Python
Executable File

"""OTP model."""
from datetime import UTC, datetime
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from customer_portal.models import Base
class OTPCode(Base):
"""One-time password code."""
__tablename__ = "otp_codes"
id = Column(Integer, primary_key=True)
customer_id = Column(
Integer, ForeignKey("customers.id"), nullable=False, index=True
)
code = Column(String(6), nullable=False)
purpose = Column(String(20), nullable=False) # login, register, reset
expires_at = Column(DateTime, nullable=False)
used_at = Column(DateTime)
created_at = Column(DateTime, default=lambda: datetime.now(UTC))
# Relationship
customer = relationship("Customer", back_populates="otp_codes")
def __repr__(self) -> str:
return f"<OTPCode {self.id} for customer {self.customer_id}>"
@property
def is_valid(self) -> bool:
"""Check if OTP is still valid."""
now = datetime.now(UTC)
return self.used_at is None and self.expires_at > now