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. http_accept = models.CharField(
  22. _('HTTP Accept'),
  23. max_length=1025,
  24. )
  25. path_info = models.CharField(
  26. _('Path'),
  27. max_length=255,
  28. )
  29. attempt_time = models.DateTimeField(
  30. _('Attempt Time'),
  31. auto_now_add=True,
  32. )
  33. class Meta:
  34. app_label = 'axes'
  35. abstract = True
  36. ordering = ['-attempt_time']
  37. class AccessAttempt(CommonAccess):
  38. get_data = models.TextField(
  39. _('GET Data'),
  40. )
  41. post_data = models.TextField(
  42. _('POST Data'),
  43. )
  44. failures_since_start = models.PositiveIntegerField(
  45. _('Failed Logins'),
  46. )
  47. @property
  48. def failures(self):
  49. return self.failures_since_start
  50. def __str__(self):
  51. return 'Attempted Access: %s' % self.attempt_time
  52. class Meta:
  53. verbose_name = _('access attempt')
  54. verbose_name_plural = _('access attempts')
  55. class AccessLog(CommonAccess):
  56. # Once a user logs in from an ip, that combination is trusted and not
  57. # locked out in case of a distributed attack
  58. trusted = models.BooleanField(
  59. default=False,
  60. db_index=True,
  61. )
  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')