forms.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django import forms
  2. from django.utils.translation import ugettext_lazy as _
  3. from misago.apps.forumbase.mixins import ValidateThreadNameMixin
  4. from misago.forms import Form
  5. from misago.validators import validate_sluggable
  6. class PostingForm(Form):
  7. post = forms.CharField(widget=forms.Textarea)
  8. def __init__(self, data=None, file=None, request=None, forum=None, thread=None, *args, **kwargs):
  9. self.forum = forum
  10. self.thread = thread
  11. super(PostingForm, self).__init__(data, file, request=request, *args, **kwargs)
  12. def set_extra_fields(self):
  13. # Can we change threads states?
  14. if self.request.acl.threads.can_pin_threads(self.forum):
  15. thread_weight = []
  16. if (not self.thread or self.thread.weight < 2) and self.request.acl.threads.can_pin_threads(self.forum) == 2:
  17. thread_weight.append((2, _("Announcement")))
  18. if (not self.thread or self.thread.weight == 0) and self.request.acl.threads.can_pin_threads(self.forum):
  19. thread_weight.append((1, _("Sticky")))
  20. if (not self.thread or self.thread.weight != 0):
  21. thread_weight.append((0, _("Standard")))
  22. if thread_weight:
  23. self.layout[0][1].append(('thread_weight', {'label': _("Thread Importance")}))
  24. self.fields['thread_weight'] = forms.TypedChoiceField(widget=forms.RadioSelect, choices=thread_weight, coerce=int, initial=0)
  25. # Can we lock threads?
  26. if self.request.acl.threads.can_close(self.forum):
  27. self.fields['close_thread'] = forms.BooleanField(required=False)
  28. if self.thread and self.thread.closed:
  29. self.layout[0][1].append(('close_thread', {'label': 'NOEZ', 'inline': _("Open Thread")}))
  30. else:
  31. self.layout[0][1].append(('close_thread', {'label': 'BALLS', 'inline': _("Close Thread")}))
  32. class NewThreadForm(PostingForm, ValidateThreadNameMixin):
  33. def finalize_form(self):
  34. self.layout = [
  35. [
  36. None,
  37. [
  38. ('thread_name', {'label': _("Thread Name")}),
  39. ('post', {'label': _("Post Content")}),
  40. ]
  41. ]
  42. ]
  43. self.fields['thread_name'] = forms.CharField(max_length=self.request.settings['thread_name_max'],
  44. validators=[validate_sluggable(_("Thread name must contain at least one alpha-numeric character."),
  45. _("Thread name is too long. Try shorter name."))])
  46. self.set_extra_fields()