models.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. abstract = True
  37. ordering = ['-attempt_time']
  38. class AccessAttempt(CommonAccess):
  39. get_data = models.TextField(
  40. verbose_name='GET Data',
  41. )
  42. post_data = models.TextField(
  43. verbose_name='POST Data',
  44. )
  45. failures_since_start = models.PositiveIntegerField(
  46. verbose_name='Failed Logins',
  47. )
  48. @property
  49. def failures(self):
  50. return self.failures_since_start
  51. def __unicode__(self):
  52. return six.u('Attempted Access: %s') % self.attempt_time
  53. class AccessLog(CommonAccess):
  54. logout_time = models.DateTimeField(
  55. null=True,
  56. blank=True,
  57. )
  58. def __unicode__(self):
  59. return six.u('Access Log for %s @ %s') % (self.username, self.attempt_time)