posting.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from django.utils.translation import ugettext_lazy as _, ungettext
  2. from misago.conf import settings
  3. from misago.core import forms
  4. from misago.markup import common_flavour
  5. from misago.threads.validators import validate_title
  6. class ReplyForm(forms.Form):
  7. is_main = True
  8. legend = _("Reply")
  9. template = "misago/posting/replyform.html"
  10. post = forms.CharField(label=_("Message body"), required=False)
  11. def __init__(self, post=None, request=None, *args, **kwargs):
  12. self.request = request
  13. self.post_instance = post
  14. self.parsing_result = {}
  15. super(ReplyForm, self).__init__(*args, **kwargs)
  16. def validate_post(self, post):
  17. if self.post_instance.original != post:
  18. self._validate_post(post)
  19. self.parse_post(post)
  20. def _validate_post(self, post):
  21. post_len = len(post)
  22. if not post_len:
  23. raise forms.ValidationError(_("Enter message."))
  24. if post_len < settings.post_length_min:
  25. message = ungettext(
  26. "Posted message should be at least %(limit)s character long.",
  27. "Posted message should be at least %(limit)s characters long.",
  28. settings.post_length_min)
  29. message = message % {'limit': settings.post_length_min}
  30. raise forms.ValidationError(message)
  31. if settings.post_length_max and post_len > settings.post_length_max:
  32. message = ungettext(
  33. "Posted message can't be longer than %(limit)s character.",
  34. "Posted message can't be longer than %(limit)s characters.",
  35. settings.post_length_max,)
  36. message = message % {'limit': settings.post_length_max}
  37. raise forms.ValidationError(message)
  38. def parse_post(self, post):
  39. self.parsing_result = common_flavour(
  40. self.request, self.post_instance.poster, post)
  41. self.post_instance.original = self.parsing_result['original_text']
  42. self.post_instance.parsed = self.parsing_result['parsed_text']
  43. def validate_data(self, data):
  44. self.validate_post(data.get('post', ''))
  45. def clean(self):
  46. data = super(ReplyForm, self).clean()
  47. self.validate_data(data)
  48. return data
  49. class ThreadForm(ReplyForm):
  50. legend = _("Thread ")
  51. title = forms.CharField(label=_("Thread title"), required=False)
  52. def __init__(self, thread=None, *args, **kwargs):
  53. self.thread_instance = thread
  54. super(ThreadForm, self).__init__(*args, **kwargs)
  55. def validate_data(self, data):
  56. errors = []
  57. if not data.get('title') and not data.get('post'):
  58. raise forms.ValidationError(_("Enter thread title and message."))
  59. try:
  60. validate_title(data.get('title', ''))
  61. except forms.ValidationError as e:
  62. errors.append(e)
  63. try:
  64. self.validate_post(data.get('post', ''))
  65. except forms.ValidationError as e:
  66. errors.append(e)
  67. if errors:
  68. raise forms.ValidationError(errors)
  69. class ThreadParticipantsForm(forms.Form):
  70. is_supporting = True
  71. location = 'reply_top'
  72. template = "misago/posting/threadparticipantsform.html"
  73. users = forms.CharField(label=_("Invite users to thread"))
  74. def __init__(self, thread=None, *args, **kwargs):
  75. self.thread_instance = thread
  76. super(ThreadParticipantsForm, self).__init__(*args, **kwargs)
  77. class ThreadLabelFormBase(forms.Form):
  78. is_supporting = True
  79. location = 'after_title'
  80. template = "misago/posting/threadlabelform.html"
  81. def ThreadLabelForm(*args, **kwargs):
  82. labels = kwargs.pop('labels')
  83. choices = [(0, _("No label"))]
  84. choices.extend([(label.pk, label.name ) for label in labels])
  85. field = forms.TypedChoiceField(
  86. label=_("Thread label"),
  87. coerce=int,
  88. choices=choices)
  89. FormType = type("ThreadLabelFormFinal",
  90. (ThreadLabelFormBase,),
  91. {'label': field})
  92. return FormType(*args, **kwargs)
  93. class ThreadPinForm(forms.Form):
  94. is_supporting = True
  95. location = 'lefthand'
  96. template = "misago/posting/threadpinform.html"
  97. is_pinned = forms.YesNoSwitch(
  98. label=_("Pin thread"),
  99. yes_label=_("Pinned thread"),
  100. no_label=_("Unpinned thread"))
  101. class ThreadCloseForm(forms.Form):
  102. is_supporting = True
  103. location = 'lefthand'
  104. template = "misago/posting/threadcloseform.html"
  105. is_closed = forms.YesNoSwitch(
  106. label=_("Close thread"),
  107. yes_label=_("Closed thread"),
  108. no_label=_("Open thread"))