forms.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django import forms
  2. from django.utils import timezone
  3. from django.utils.translation import ungettext_lazy, ugettext_lazy as _
  4. from misago.forms import Form
  5. class QuickSearchForm(Form):
  6. search_query = forms.CharField(max_length=255)
  7. def clean_search_query(self):
  8. data = self.cleaned_data['search_query']
  9. if len(data) < 3:
  10. raise forms.ValidationError(_("Search query should be at least 3 characters long."))
  11. self.mode = None
  12. if data[0:6].lower() == 'forum:':
  13. forum_name = data[6:].strip()
  14. if len(forum_name) < 2:
  15. raise forms.ValidationError(_("In order to jump to forum, You have to enter full forum name or first few characters of it."))
  16. self.mode = 'forum'
  17. self.target = forum_name
  18. if data[0:5].lower() == 'user:':
  19. username = data[5:].strip()
  20. if len(username) < 2:
  21. raise forms.ValidationError(_("In order to jump to user profile, You have to enter full user name or first few characters of it."))
  22. self.mode = 'user'
  23. self.target = username
  24. return data
  25. def clean(self):
  26. cleaned_data = super(QuickSearchForm, self).clean()
  27. if self.request.user.is_authenticated():
  28. self.check_flood_user()
  29. if self.request.user.is_anonymous():
  30. self.check_flood_guest()
  31. return cleaned_data
  32. def check_flood_user(self):
  33. if self.request.user.last_search:
  34. diff = timezone.now() - self.request.user.last_search
  35. diff = diff.seconds + (diff.days * 86400)
  36. wait_for = self.request.acl.search.search_cooldown() - diff
  37. if wait_for > 0:
  38. if wait_for < 5:
  39. raise forms.ValidationError(_("You can't perform one search so quickly after another. Please wait a moment and try again."))
  40. else:
  41. raise forms.ValidationError(ungettext_lazy(
  42. "You can't perform one search so quickly after another. Please wait %(seconds)d second and try again.",
  43. "You can't perform one search so quickly after another. Please wait %(seconds)d seconds and try again.",
  44. wait_for) % {
  45. 'seconds': wait_for,
  46. })
  47. def check_flood_guest(self):
  48. if not self.request.session.matched:
  49. raise forms.ValidationError(_("Search requires enabled cookies in order to work."))
  50. if self.request.session.get('last_search'):
  51. diff = timezone.now() - self.request.session.get('last_search')
  52. diff = diff.seconds + (diff.days * 86400)
  53. wait_for = self.request.acl.search.search_cooldown() - diff
  54. if wait_for > 0:
  55. if wait_for < 5:
  56. raise forms.ValidationError(_("You can't perform one search so quickly after another. Please wait a moment and try again."))
  57. else:
  58. raise forms.ValidationError(ungettext_lazy(
  59. "You can't perform one search so quickly after another. Please wait %(seconds)d second and try again.",
  60. "You can't perform one search so quickly after another. Please wait %(seconds)d seconds and try again.",
  61. wait_for) % {
  62. 'seconds': wait_for,
  63. })