signinattemptmodel.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from datetime import timedelta
  2. from django.db import models
  3. from django.utils import timezone
  4. class SignInAttemptsManager(models.Manager):
  5. """
  6. IP's that have exhausted their quota of sign-in attempts are automatically banned for set amount of time.
  7. That IP ban cuts bad IP address from signing into board by either making another sign-in attempts or
  8. registering "fresh" account.
  9. """
  10. def register_attempt(self, ip):
  11. attempt = SignInAttempt(ip=ip, date=timezone.now())
  12. attempt.save(force_insert=True)
  13. def is_jammed(self, settings, ip):
  14. # Limit is off, dont jam IPs?
  15. if settings['attempts_limit'] == 0:
  16. return False
  17. # Check jam
  18. if settings['jams_lifetime'] > 0:
  19. attempts = SignInAttempt.objects.filter(
  20. date__gt=timezone.now() - timedelta(minutes=settings['jams_lifetime']),
  21. ip=ip
  22. )
  23. else:
  24. attempts = SignInAttempt.objects.filter(ip=ip)
  25. return attempts.count() > settings['attempts_limit']
  26. class SignInAttempt(models.Model):
  27. ip = models.GenericIPAddressField()
  28. date = models.DateTimeField()
  29. objects = SignInAttemptsManager()
  30. class Meta:
  31. app_label = 'misago'