Skip to content

models

django_spire.auth.sms.models

DAY_SECONDS = 86400 module-attribute

MINUTE_SECONDS = 60 module-attribute

AuthSms

Bases: HistoryModelMixin

user = models.ForeignKey('django_spire_auth_user.AuthUser', on_delete=models.CASCADE, related_name='auth_smss', related_query_name='auth_sms') class-attribute instance-attribute

phone_number = models.CharField(max_length=20, unique=True) class-attribute instance-attribute

is_verified = models.BooleanField(default=False) class-attribute instance-attribute

verified_datetime = models.DateTimeField(blank=True, null=True) class-attribute instance-attribute

code_hash = models.CharField(max_length=128, blank=True, default='', editable=False) class-attribute instance-attribute

code_purpose = models.CharField(max_length=4, choices=AuthSmsCodePurposeChoices.choices, default='', blank=True, editable=False) class-attribute instance-attribute

code_expiration_datetime = models.DateTimeField(blank=True, null=True, editable=False) class-attribute instance-attribute

code_attempt_count = models.IntegerField(default=0, editable=False) class-attribute instance-attribute

session_started_datetime = models.DateTimeField(blank=True, null=True, editable=False) class-attribute instance-attribute

session_last_activity_datetime = models.DateTimeField(blank=True, null=True, editable=False) class-attribute instance-attribute

objects = AuthSmsQuerySet.as_manager() class-attribute instance-attribute

services = AuthSmsService() class-attribute instance-attribute

session_is_active property

Meta

db_table = 'django_spire_auth_sms' class-attribute instance-attribute
verbose_name = 'Auth SMS' class-attribute instance-attribute
verbose_name_plural = 'Auth SMSs' class-attribute instance-attribute
ordering = ('phone_number',) class-attribute instance-attribute

__str__

Source code in django_spire/auth/sms/models.py
def __str__(self) -> str:
    return f'{self.phone_number} ({self.user})'

is_throttled

Source code in django_spire/auth/sms/models.py
def is_throttled(self) -> bool:
    minute_key, day_key = self._build_throttle_keys()

    minute_count = cache.get(minute_key, 0) or 0
    day_count = cache.get(day_key, 0) or 0

    minute_rate_max = settings.DJANGO_SPIRE_AUTH_SMS_THROTTLE_RATE_PER_MINUTE
    day_rate_max = settings.DJANGO_SPIRE_AUTH_SMS_THROTTLE_RATE_PER_DAY

    if minute_count > minute_rate_max:
        return True

    return day_count > day_rate_max

record_attempt

Source code in django_spire/auth/sms/models.py
def record_attempt(self) -> None:
    minute_key, day_key = self._build_throttle_keys()

    cache.add(minute_key, 0, MINUTE_SECONDS)
    cache.incr(minute_key)

    cache.add(day_key, 0, DAY_SECONDS)
    cache.incr(day_key)