models.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import unicode_literals
  2. from django.db import models
  3. from django.utils.translation import gettext_lazy as _
  4. class CommonAccess(models.Model):
  5. user_agent = models.CharField(
  6. _('User Agent'),
  7. max_length=255,
  8. db_index=True,
  9. )
  10. ip_address = models.GenericIPAddressField(
  11. _('IP Address'),
  12. null=True,
  13. db_index=True,
  14. )
  15. username = models.CharField(
  16. _('Username'),
  17. max_length=255,
  18. null=True,
  19. db_index=True,
  20. )
  21. # Once a user logs in from an ip, that combination is trusted and not
  22. # locked out in case of a distributed attack
  23. trusted = models.BooleanField(
  24. default=False,
  25. db_index=True,
  26. )
  27. http_accept = models.CharField(
  28. _('HTTP Accept'),
  29. max_length=1025,
  30. )
  31. path_info = models.CharField(
  32. _('Path'),
  33. max_length=255,
  34. )
  35. attempt_time = models.DateTimeField(
  36. _('Attempt Time'),
  37. auto_now_add=True,
  38. )
  39. class Meta:
  40. app_label = 'axes'
  41. abstract = True
  42. ordering = ['-attempt_time']
  43. class AccessAttempt(CommonAccess):
  44. get_data = models.TextField(
  45. _('GET Data'),
  46. )
  47. post_data = models.TextField(
  48. _('POST Data'),
  49. )
  50. failures_since_start = models.PositiveIntegerField(
  51. _('Failed Logins'),
  52. )
  53. @property
  54. def failures(self):
  55. return self.failures_since_start
  56. def __str__(self):
  57. return 'Attempted Access: %s' % self.attempt_time
  58. class Meta:
  59. verbose_name = _('access attempt')
  60. verbose_name_plural = _('access attempts')
  61. class AccessLog(CommonAccess):
  62. logout_time = models.DateTimeField(
  63. _('Logout Time'),
  64. null=True,
  65. blank=True,
  66. )
  67. def __str__(self):
  68. return 'Access Log for %s @ %s' % (self.username, self.attempt_time)
  69. class Meta:
  70. verbose_name = _('access log')
  71. verbose_name_plural = _('access logs')