forms.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.utils import timezone
  2. from django.utils.translation import ungettext_lazy, ugettext_lazy as _
  3. import floppyforms as forms
  4. from misago.forms import Form, ForumMultipleChoiceField
  5. from misago.models import Forum
  6. class SearchFormBase(Form):
  7. search_query = forms.CharField(label=_("Search Phrases"), max_length=255)
  8. search_thread_titles = forms.BooleanField(label=_("Search Thread Titles"), required=False)
  9. search_thread = forms.CharField(label=_("Thread Name or Link"),
  10. help_text=_("Limit search to specified thread by entering it's name or link here."),
  11. max_length=255,
  12. required=False)
  13. search_author = forms.CharField(label=_("Author Name"),
  14. help_text=_("Limit search to specified user by entering his or her name here."),
  15. max_length=255,
  16. required=False)
  17. def clean_search_query(self):
  18. data = self.cleaned_data['search_query']
  19. if len(data) < 3:
  20. raise forms.ValidationError(_("Search query should be at least 3 characters long."))
  21. return data
  22. def clean(self):
  23. cleaned_data = super(SearchFormBase, self).clean()
  24. if self.request.user.is_authenticated():
  25. self.check_flood_user()
  26. if self.request.user.is_anonymous():
  27. self.check_flood_guest()
  28. return cleaned_data
  29. def check_flood_user(self):
  30. if self.request.user.last_search:
  31. diff = timezone.now() - self.request.user.last_search
  32. diff = diff.seconds + (diff.days * 86400)
  33. wait_for = self.request.acl.search.search_cooldown() - diff
  34. if wait_for > 0:
  35. if wait_for < 5:
  36. raise forms.ValidationError(_("You can't perform one search so quickly after another. Please wait a moment and try again."))
  37. else:
  38. raise forms.ValidationError(ungettext_lazy(
  39. "You can't perform one search so quickly after another. Please wait %(seconds)d second and try again.",
  40. "You can't perform one search so quickly after another. Please wait %(seconds)d seconds and try again.",
  41. wait_for) % {
  42. 'seconds': wait_for,
  43. })
  44. def check_flood_guest(self):
  45. if not self.request.session.matched:
  46. raise forms.ValidationError(_("Search requires enabled cookies in order to work."))
  47. if self.request.session.get('last_search'):
  48. diff = timezone.now() - self.request.session.get('last_search')
  49. diff = diff.seconds + (diff.days * 86400)
  50. wait_for = self.request.acl.search.search_cooldown() - diff
  51. if wait_for > 0:
  52. if wait_for < 5:
  53. raise forms.ValidationError(_("You can't perform one search so quickly after another. Please wait a moment and try again."))
  54. else:
  55. raise forms.ValidationError(ungettext_lazy(
  56. "You can't perform one search so quickly after another. Please wait %(seconds)d second and try again.",
  57. "You can't perform one search so quickly after another. Please wait %(seconds)d seconds and try again.",
  58. wait_for) % {
  59. 'seconds': wait_for,
  60. })
  61. class QuickSearchForm(SearchFormBase):
  62. pass
  63. class AdvancedSearchForm(SearchFormBase):
  64. search_before = forms.DateField(label=_("Posted Before"),
  65. help_text=_("Exclude posts made before specified date from search. Use YYYY-MM-DD format, for example 2013-11-23."),
  66. required=False)
  67. search_after = forms.DateField(label=_("Posted After"),
  68. help_text=_("Exclude posts made after specified date from search. Use YYYY-MM-DD format, for example 2013-11-23."),
  69. required=False)
  70. class ForumsSearchForm(AdvancedSearchForm):
  71. def finalize_form(self):
  72. self.add_field('search_forums', ForumMultipleChoiceField(label=_("Search Forums"),
  73. help_text=_("If you want, you can limit search to specified forums."),
  74. queryset=Forum.objects.get(special='root').get_descendants().filter(pk__in=self.request.acl.forums.acl['can_browse']),
  75. required=False, empty_label=None, widget=forms.SelectMultiple))
  76. self.add_field('search_forums_childs', forms.BooleanField(label=_("Include Children Forums"), required=False))
  77. class PrivateThreadsSearchForm(AdvancedSearchForm):
  78. pass
  79. class ReportsSearchForm(AdvancedSearchForm):
  80. search_weight = forms.TypedMultipleChoiceField(label=_("Report Types"),
  81. help_text=_("Limit search to certain report types."),
  82. choices=(
  83. (2, _("Open")),
  84. (1, _("Resolved")),
  85. (0, _("Bogus")),
  86. ),
  87. coerce=int,
  88. widget=forms.CheckboxSelectMultiple,
  89. required=False)