floodprotection.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 . import PostingInterrupt, PostingMiddleware
  6. MIN_POSTING_PAUSE = 3
  7. class FloodProtectionMiddleware(PostingMiddleware):
  8. def interrupt_posting(self, serializer):
  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 quickly after previous one."))
  14. self.user.last_posted_on = timezone.now()
  15. self.user.update_fields.append('last_posted_on')
  16. if settings.MISAGO_HOURLY_POST_LIMIT:
  17. cutoff = now - timedelta(hours=24)
  18. count_qs = self.user.post_set.filter(posted_on__gte=cutoff)
  19. posts_count = count_qs.count()
  20. if posts_count > settings.MISAGO_HOURLY_POST_LIMIT:
  21. raise PostingInterrupt(_("Your account has excceed hourly post limit."))
  22. if settings.MISAGO_DIALY_POST_LIMIT:
  23. cutoff = now - timedelta(hours=1)
  24. count_qs = self.user.post_set.filter(posted_on__gte=cutoff)
  25. posts_count = count_qs.count()
  26. if posts_count > settings.MISAGO_DIALY_POST_LIMIT:
  27. raise PostingInterrupt(_("Your account has excceed dialy post limit."))