signinattemptmodel.py 1.3 KB

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