forms.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.forums.models import Forum
  6. from misago.utils.validators import validate_sluggable
  7. class CategoryForm(Form):
  8. parent = False
  9. perms = False
  10. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  11. _("Category name must be sluggable."),
  12. _("Category name is too long.")
  13. )])
  14. description = forms.CharField(widget=forms.Textarea, required=False)
  15. closed = forms.BooleanField(widget=YesNoSwitch, required=False)
  16. style = forms.CharField(max_length=255, required=False)
  17. template = forms.ChoiceField(choices=(
  18. ('row', _('One forum per row')),
  19. ('half', _('Two forums per row')),
  20. ('quarter', _('Four forums per row')),
  21. ))
  22. show_details = forms.BooleanField(widget=YesNoSwitch, required=False)
  23. layout = (
  24. (
  25. _("Basic Options"),
  26. (
  27. ('parent', {'label': _("Category Parent")}),
  28. ('perms', {'label': _("Copy Permissions from")}),
  29. ('name', {'label': _("Category Name")}),
  30. ('description', {'label': _("Category Description")}),
  31. ('closed', {'label': _("Closed Category")}),
  32. ),
  33. ),
  34. (
  35. _("Display Options"),
  36. (
  37. ('template', {'label': _("Category Layout"), 'help_text': _('Controls how this category is displayed on forums lists.')}),
  38. ('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.')}),
  39. ('style', {'label': _("Category Style"), 'help_text': _('You can add custom CSS classess to this category, to change way it looks on board index.')}),
  40. ),
  41. ),
  42. )
  43. def finalize_form(self):
  44. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(include_self=True), level_indicator=u'- - ')
  45. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  46. class ForumForm(Form):
  47. parent = False
  48. perms = False
  49. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  50. _("Forum name must be sluggable."),
  51. _("Forum name is too long.")
  52. )])
  53. description = forms.CharField(widget=forms.Textarea, required=False)
  54. closed = forms.BooleanField(widget=YesNoSwitch, required=False)
  55. style = forms.CharField(max_length=255, required=False)
  56. prune_start = forms.IntegerField(min_value=0, initial=0)
  57. prune_last = forms.IntegerField(min_value=0, initial=0)
  58. template = forms.ChoiceField(choices=(
  59. ('row', _('One forum per row')),
  60. ('half', _('Two forums per row')),
  61. ('quarter', _('Four forums per row')),
  62. ))
  63. show_details = forms.BooleanField(widget=YesNoSwitch, required=False)
  64. layout = (
  65. (
  66. _("Basic Options"),
  67. (
  68. ('parent', {'label': _("Forum Parent")}),
  69. ('perms', {'label': _("Copy Permissions from")}),
  70. ('name', {'label': _("Forum Name")}),
  71. ('description', {'label': _("Forum Description")}),
  72. ('closed', {'label': _("Closed Forum")}),
  73. ),
  74. ),
  75. (
  76. _("Prune Forum"),
  77. (
  78. ('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.')}),
  79. ('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.')}),
  80. ),
  81. ),
  82. (
  83. _("Display Options"),
  84. (
  85. ('template', {'label': _("Subforums Layout"), 'help_text': _('Controls how this forum displays subforums list.')}),
  86. ('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.")}),
  87. ('style', {'label': _("Forum Style"), 'help_text': _('You can add custom CSS classess to this forum to change way it looks on forums lists.')}),
  88. ),
  89. ),
  90. )
  91. def finalize_form(self):
  92. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), level_indicator=u'- - ')
  93. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  94. class RedirectForm(Form):
  95. parent = False
  96. perms = False
  97. name = forms.CharField(max_length=255, validators=[validate_sluggable(
  98. _("Redirect name must be sluggable."),
  99. _("Redirect name is too long.")
  100. )])
  101. description = forms.CharField(widget=forms.Textarea, required=False)
  102. redirect = forms.URLField(max_length=255)
  103. style = forms.CharField(max_length=255, required=False)
  104. layout = (
  105. (
  106. _("Basic Options"),
  107. (
  108. ('parent', {'label': _("Redirect Parent")}),
  109. ('perms', {'label': _("Copy Permissions from")}),
  110. ('name', {'label': _("Redirect Name")}),
  111. ('redirect', {'label': _("Redirect URL")}),
  112. ('description', {'label': _("Redirect Description")}),
  113. ),
  114. ),
  115. (
  116. _("Display Options"),
  117. (
  118. ('style', {'label': _("Redirect Style"), 'help_text': _('You can add custom CSS classess to this redirect to change way it looks on forums lists.')}),
  119. ),
  120. ),
  121. )
  122. def finalize_form(self):
  123. self.fields['parent'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), level_indicator=u'- - ')
  124. self.fields['perms'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), level_indicator=u'- - ', required=False, empty_label=_("Don't copy permissions"))
  125. class DeleteForm(Form):
  126. layout = (
  127. (
  128. _("Delete Options"),
  129. (
  130. ('contents', {'label': _("Move threads to")}),
  131. ('subforums', {'label': _("Move subforums to")}),
  132. ),
  133. ),
  134. )
  135. def __init__(self, *args, **kwargs):
  136. self.forum = kwargs.pop('forum')
  137. super(DeleteForm, self).__init__(*args, **kwargs)
  138. def finalize_form(self):
  139. self.fields['contents'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), required=False, empty_label=_("Remove with forum"), level_indicator=u'- - ')
  140. self.fields['subforums'] = TreeNodeChoiceField(queryset=Forum.tree.get(token='root').get_descendants(), required=False, empty_label=_("Remove with forum"), level_indicator=u'- - ')
  141. def clean_contents(self):
  142. data = self.cleaned_data['contents']
  143. if data:
  144. if data.type == 'category':
  145. raise forms.ValidationError(_("Categories cannot contain threads."))
  146. if data.type == 'redirect':
  147. raise forms.ValidationError(_("Redirects cannot contain threads."))
  148. return data
  149. def clean(self):
  150. cleaned_data = super(DeleteForm, self).clean()
  151. 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']:
  152. raise forms.ValidationError(_("Destination you want to move this forum's threads to will be deleted with this forum."))
  153. return cleaned_data