forms.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. from django.utils.translation import ugettext_lazy as _
  2. from django import forms
  3. from mptt.forms import TreeNodeChoiceField
  4. from misago.forms import Form, YesNoSwitch
  5. from misago.models import Forum
  6. from misago.validators import validate_sluggable
  7. class CleanAttrsMixin(object):
  8. def clean_attrs(self):
  9. clean = []
  10. data = self.cleaned_data['attrs'].strip().split()
  11. for i in data:
  12. i = i.strip()
  13. if not i in clean:
  14. clean.append(i)
  15. return ' '.join(clean)
  16. class NewNodeForm(Form, CleanAttrsMixin):
  17. parent = False
  18. perms = False
  19. role = forms.ChoiceField(choices=(
  20. ('category', _("Category")),
  21. ('forum', _("Forum")),
  22. ('redirect', _("Redirection")),
  23. ))
  24. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  25. _("Category name must contain alphanumeric characters."),
  26. _("Category name is too long.")
  27. )])
  28. redirect = forms.URLField(max_length=255, required=False)
  29. description = forms.CharField(widget=forms.Textarea, required=False)
  30. closed = forms.BooleanField(widget=YesNoSwitch, required=False)
  31. attrs = forms.CharField(max_length=255, required=False)
  32. show_details = forms.BooleanField(widget=YesNoSwitch, required=False, initial=True)
  33. style = forms.CharField(max_length=255, required=False)
  34. layout = (
  35. (
  36. _("Basic Options"),
  37. (
  38. ('parent', {'label': _("Node Parent")}),
  39. ('perms', {'label': _("Copy Permissions from")}),
  40. ('role', {'label': _("Node Type"), 'help_text': _("Each Node has specific role in forums tree. This role cannot be changed after node is created.")}),
  41. ('name', {'label': _("Node Name")}),
  42. ('description', {'label': _("Node Description")}),
  43. ('redirect', {'label': _("Redirect URL"), 'help_text': _("Redirection nodes require you to specify URL they will redirect users to upon click.")}),
  44. ('closed', {'label': _("Closed Node")}),
  45. ),
  46. ),
  47. (
  48. _("Display Options"),
  49. (
  50. ('attrs', {'label': _("Node Attributes"), 'help_text': _('Custom templates can check nodes for predefined attributes that will change way they are rendered.')}),
  51. ('show_details', {'label': _("Show Subforums Details"), 'help_text': _('Allows you to prevent this node subforums from displaying statistics, last post data, etc. ect. on forums lists.')}),
  52. ('style', {'label': _("Node Style"), 'help_text': _('You can add custom CSS classess to this node, to change way it looks on board index.')}),
  53. ),
  54. ),
  55. )
  56. def finalize_form(self):
  57. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(include_self=True), level_indicator=u'- - ')
  58. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  59. def clean(self):
  60. cleaned_data = super(NewNodeForm, self).clean()
  61. node_role = cleaned_data['role']
  62. if node_role != 'category' and cleaned_data['parent'].special == 'root':
  63. raise forms.ValidationError(_("Only categories can use Root Category as their parent."))
  64. if node_role == 'redirect' and not cleaned_data['redirect']:
  65. raise forms.ValidationError(_("You have to define redirection URL"))
  66. return cleaned_data
  67. class CategoryForm(Form, CleanAttrsMixin):
  68. parent = False
  69. perms = False
  70. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  71. _("Category name must contain alphanumeric characters."),
  72. _("Category name is too long.")
  73. )])
  74. description = forms.CharField(widget=forms.Textarea, required=False)
  75. closed = forms.BooleanField(widget=YesNoSwitch, required=False)
  76. style = forms.CharField(max_length=255, required=False)
  77. attrs = forms.CharField(max_length=255, required=False)
  78. show_details = forms.BooleanField(widget=YesNoSwitch, required=False, initial=True)
  79. layout = (
  80. (
  81. _("Basic Options"),
  82. (
  83. ('parent', {'label': _("Category Parent")}),
  84. ('perms', {'label': _("Copy Permissions from")}),
  85. ('name', {'label': _("Category Name")}),
  86. ('description', {'label': _("Category Description")}),
  87. ('closed', {'label': _("Closed Category")}),
  88. ),
  89. ),
  90. (
  91. _("Display Options"),
  92. (
  93. ('attrs', {'label': _("Category Attributes"), 'help_text': _('Custom templates can check categories for predefined attributes that will change way they are rendered.')}),
  94. ('show_details', {'label': _("Show Subforums Details"), 'help_text': _('Allows you to prevent this category subforums from displaying statistics, last post data, etc. ect. on forums lists.')}),
  95. ('style', {'label': _("Category Style"), 'help_text': _('You can add custom CSS classess to this category, to change way it looks on board index.')}),
  96. ),
  97. ),
  98. )
  99. def finalize_form(self):
  100. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(include_self=True), level_indicator=u'- - ')
  101. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  102. class ForumForm(Form, CleanAttrsMixin):
  103. parent = False
  104. perms = False
  105. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  106. _("Forum name must contain alphanumeric characters."),
  107. _("Forum name is too long.")
  108. )])
  109. description = forms.CharField(widget=forms.Textarea, required=False)
  110. closed = forms.BooleanField(widget=YesNoSwitch, required=False)
  111. style = forms.CharField(max_length=255, required=False)
  112. prune_start = forms.IntegerField(min_value=0, initial=0)
  113. prune_last = forms.IntegerField(min_value=0, initial=0)
  114. attrs = forms.CharField(max_length=255, required=False)
  115. show_details = forms.BooleanField(widget=YesNoSwitch, required=False, initial=True)
  116. layout = (
  117. (
  118. _("Basic Options"),
  119. (
  120. ('parent', {'label': _("Forum Parent")}),
  121. ('perms', {'label': _("Copy Permissions from")}),
  122. ('name', {'label': _("Forum Name")}),
  123. ('description', {'label': _("Forum Description")}),
  124. ('closed', {'label': _("Closed Forum")}),
  125. ),
  126. ),
  127. (
  128. _("Prune Forum"),
  129. (
  130. ('prune_start', {'label': _("Delete threads with first post older than"), 'help_text': _('Enter number of days since thread start after which thread will be deleted or zero to don\'t delete threads.')}),
  131. ('prune_last', {'label': _("Delete threads with last post older than"), 'help_text': _('Enter number of days since since last reply in thread after which thread will be deleted or zero to don\'t delete threads.')}),
  132. ),
  133. ),
  134. (
  135. _("Display Options"),
  136. (
  137. ('attrs', {'label': _("Forum Attributes"), 'help_text': _('Custom templates can check forums for predefined attributes that will change way subforums lists are rendered.')}),
  138. ('show_details', {'label': _("Show Subforums Details"), 'help_text': _("Allows you to prevent this forum's subforums from displaying statistics, last post data, etc. ect. on subforums list.")}),
  139. ('style', {'label': _("Forum Style"), 'help_text': _('You can add custom CSS classess to this forum to change way it looks on forums lists.')}),
  140. ),
  141. ),
  142. )
  143. def finalize_form(self):
  144. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ')
  145. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  146. class RedirectForm(Form, CleanAttrsMixin):
  147. parent = False
  148. perms = False
  149. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  150. _("Redirect name must contain alphanumeric characters."),
  151. _("Redirect name is too long.")
  152. )])
  153. description = forms.CharField(widget=forms.Textarea, required=False)
  154. redirect = forms.URLField(max_length=255)
  155. style = forms.CharField(max_length=255, required=False)
  156. layout = (
  157. (
  158. _("Basic Options"),
  159. (
  160. ('parent', {'label': _("Redirect Parent")}),
  161. ('perms', {'label': _("Copy Permissions from")}),
  162. ('name', {'label': _("Redirect Name")}),
  163. ('redirect', {'label': _("Redirect URL")}),
  164. ('description', {'label': _("Redirect Description")}),
  165. ),
  166. ),
  167. (
  168. _("Display Options"),
  169. (
  170. ('attrs', {'label': _("Forum Attributes"), 'help_text': _('Custom templates can check forums for predefined attributes that will change way subforums lists are rendered.')}),
  171. ('style', {'label': _("Redirect Style"), 'help_text': _('You can add custom CSS classess to this redirect to change way it looks on forums lists.')}),
  172. ),
  173. ),
  174. )
  175. def finalize_form(self):
  176. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ')
  177. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  178. class DeleteForm(Form):
  179. layout = (
  180. (
  181. _("Delete Options"),
  182. (
  183. ('contents', {'label': _("Move threads to")}),
  184. ('subforums', {'label': _("Move subforums to")}),
  185. ),
  186. ),
  187. )
  188. def __init__(self, *args, **kwargs):
  189. self.forum = kwargs.pop('forum')
  190. super(DeleteForm, self).__init__(*args, **kwargs)
  191. def finalize_form(self):
  192. self.fields['contents'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), required=False, empty_label=_("Remove with forum"), level_indicator=u'- - ')
  193. self.fields['subforums'] = TreeNodeChoiceField(queryset=Forum.objects.get(special='root').get_descendants(), required=False, empty_label=_("Remove with forum"), level_indicator=u'- - ')
  194. def clean_contents(self):
  195. data = self.cleaned_data['contents']
  196. if data:
  197. if data.type == 'category':
  198. raise forms.ValidationError(_("Categories cannot contain threads."))
  199. if data.type == 'redirect':
  200. raise forms.ValidationError(_("Redirects cannot contain threads."))
  201. return data
  202. def clean(self):
  203. cleaned_data = super(DeleteForm, self).clean()
  204. if self.forum.type == 'forum' and cleaned_data['contents'] and cleaned_data['contents'].lft > self.forum.lft and cleaned_data['contents'].rght < self.forum.rght and not cleaned_data['subforums']:
  205. raise forms.ValidationError(_("Destination you want to move this forum's threads to will be deleted with this forum."))
  206. return cleaned_data