floodprotection.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.utils import timezone
  4. from django.utils.translation import ugettext as _
  5. from misago.threads.posting import PostingMiddleware, PostingInterrupt
  6. MIN_POSTING_PAUSE = 3
  7. class FloodProtectionMiddleware(PostingMiddleware):
  8. def interrupt_posting(self, form):
  9. now = timezone.now()
  10. if self.user.last_posted_on:
  11. previous_post = now - self.user.last_posted_on
  12. if previous_post.total_seconds() < MIN_POSTING_PAUSE:
  13. raise PostingInterrupt(_("You can't post message so "
  14. "quickly after previous one."))
  15. self.user.last_posted_on = timezone.now()
  16. self.user.update_fields.append('last_posted_on')
  17. if settings.MISAGO_HOURLY_POST_LIMIT:
  18. cutoff = now - timedelta(hours=24)
  19. count_qs = self.user.post_set.filter(posted_on__gte=cutoff)
  20. posts_count = count_qs.count()
  21. if posts_count > settings.MISAGO_HOURLY_POST_LIMIT:
  22. raise PostingInterrupt(_("Your account has excceed "
  23. "hourly post limit."))
  24. if settings.MISAGO_DIALY_POST_LIMIT:
  25. cutoff = now - timedelta(hours=1)
  26. count_qs = self.user.post_set.filter(posted_on__gte=cutoff)
  27. posts_count = count_qs.count()
  28. if posts_count > settings.MISAGO_DIALY_POST_LIMIT:
  29. raise PostingInterrupt(_("Your account has excceed "
  30. "dialy post limit."))