forms.py 3.5 KB

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