forms.py 6.3 KB

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