django-axes/axes/models.py

84 lines
2.5 KiB
Python
Raw Normal View History

from django.db import models
2018-07-17 13:14:17 +00:00
from django.utils.translation import gettext_lazy as _
2016-06-24 14:42:18 +00:00
class AccessBase(models.Model):
user_agent = models.CharField(_("User Agent"), max_length=255, db_index=True)
ip_address = models.GenericIPAddressField(_("IP Address"), null=True, db_index=True)
username = models.CharField(_("Username"), max_length=255, null=True, db_index=True)
http_accept = models.CharField(_("HTTP Accept"), max_length=1025)
path_info = models.CharField(_("Path"), max_length=255)
attempt_time = models.DateTimeField(_("Attempt Time"), auto_now_add=True)
class Meta:
app_label = "axes"
abstract = True
ordering = ["-attempt_time"]
class AccessFailureLog(AccessBase):
2022-05-16 07:31:46 +00:00
locked_out = models.BooleanField(
_("Access lock out"), null=False, blank=True, default=False
)
def __str__(self):
2022-05-16 07:31:46 +00:00
locked_out_str = " locked out" if self.locked_out else ""
return f"Failed access: user {self.username}{locked_out_str} on {self.attempt_time} from {self.ip_address}"
class Meta:
verbose_name = _("access failure")
verbose_name_plural = _("access failures")
class AccessAttempt(AccessBase):
get_data = models.TextField(_("GET Data"))
post_data = models.TextField(_("POST Data"))
failures_since_start = models.PositiveIntegerField(_("Failed Logins"))
def __str__(self):
return f"Attempted Access: {self.attempt_time}"
class Meta:
verbose_name = _("access attempt")
verbose_name_plural = _("access attempts")
2021-09-13 15:35:56 +00:00
unique_together = [["username", "ip_address", "user_agent"]]
2025-06-07 13:13:14 +00:00
class AccessAttemptExpiration(models.Model):
access_attempt = models.OneToOneField(
AccessAttempt,
2025-06-08 08:45:17 +00:00
primary_key=True,
2025-06-07 13:13:14 +00:00
on_delete=models.CASCADE,
related_name="expiration",
verbose_name=_("Access Attempt"),
)
expires_at = models.DateTimeField(
_("Expires At"),
help_text=_("The time when access attempt expires and is no longer valid."),
)
class Meta:
verbose_name = _("access attempt expiration")
verbose_name_plural = _("access attempt expirations")
2026-02-11 19:54:13 +00:00
class AccessLog(AccessBase):
logout_time = models.DateTimeField(_("Logout Time"), null=True, blank=True)
2026-02-11 19:54:13 +00:00
session_hash = models.CharField(
_("Session key hash (sha256)"), default="", blank=True, max_length=64
)
def __str__(self):
return f"Access Log for {self.username} @ {self.attempt_time}"
class Meta:
verbose_name = _("access log")
verbose_name_plural = _("access logs")