models.py 1.5 KB

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