models.py 1.7 KB

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