forms.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. from django.db import models
  2. from django.utils.html import conditional_escape, mark_safe
  3. from django.utils.translation import ugettext_lazy as _
  4. from mptt.forms import * # noqa
  5. from misago.core import forms
  6. from misago.core.validators import validate_sluggable
  7. from misago.forums.models import Forum, ForumRole
  8. """
  9. Fields
  10. """
  11. class ForumListLazyQueryset(object):
  12. def __init__(self, parent, acl):
  13. self._cache = None
  14. self.parent = parent
  15. self.acl = acl
  16. def get_parent(self):
  17. if not self.parent:
  18. self.parent = Forum.objects.root_category()
  19. return self.parent
  20. def _all(self):
  21. queryset = self.get_parent().get_descendants()
  22. if self.acl:
  23. allowed_ids = [0]
  24. for forum_id, perms in self.acl.get('forums', {}).items():
  25. if perms.get('can_see') and perms.get('can_browse'):
  26. allowed_ids.append(forum_id)
  27. queryset = queryset.filter(id__in=allowed_ids)
  28. return queryset.all()
  29. def all(self):
  30. if not self._cache:
  31. self._cache = self._all()
  32. return self._cache
  33. def filter(self, *args, **kwargs):
  34. return self.all().filter(*args, **kwargs)
  35. def exclude(self, *args, **kwargs):
  36. return self.all().exclude(*args, **kwargs)
  37. def __len__(self):
  38. return len(self.all())
  39. class MisagoForumMixin(object):
  40. def get_queryset(self, acl=None):
  41. queryset = self.parent.get_descendants()
  42. if acl:
  43. allowed_ids = [0]
  44. for forum_id, perms in acl.get('forums', {}).items():
  45. if perms.get('can_see') and perms.get('can_browse'):
  46. allowed_ids.append(forum_id)
  47. queryset = queryset.filter(id__in=allowed_ids)
  48. return queryset
  49. def _get_level_indicator(self, obj):
  50. level = obj.level - self.queryset.get_parent().level - 1
  51. return mark_safe(conditional_escape('- - ') * level)
  52. class ForumChoiceField(MisagoForumMixin, TreeNodeChoiceField):
  53. def __init__(self, parent=None, acl=None, *args, **kwargs):
  54. kwargs['queryset'] = ForumListLazyQueryset(parent, acl)
  55. super(ForumChoiceField, self).__init__(*args, **kwargs)
  56. class ForumsMultipleChoiceField(MisagoForumMixin, TreeNodeMultipleChoiceField):
  57. def __init__(self, parent=None, acl=None, *args, **kwargs):
  58. kwargs['queryset'] = ForumListLazyQueryset(parent, acl)
  59. super(ForumsMultipleChoiceField, self).__init__(*args, **kwargs)
  60. """
  61. Forms
  62. """
  63. class ForumChoiceField(TreeNodeChoiceField):
  64. def __init__(self, *args, **kwargs):
  65. self.base_level = kwargs.pop('base_level', 1)
  66. kwargs['level_indicator'] = kwargs.get('level_indicator', '- - ')
  67. super(ForumChoiceField, self).__init__(*args, **kwargs)
  68. def _get_level_indicator(self, obj):
  69. level = getattr(obj, obj._mptt_meta.level_attr) - self.base_level
  70. if level > 0:
  71. return mark_safe(conditional_escape(self.level_indicator) * level)
  72. else:
  73. return ''
  74. FORUM_ROLES = (
  75. ('category', _('Category')),
  76. ('forum', _('Forum')),
  77. ('redirect', _('Redirect')),
  78. )
  79. class ForumFormBase(forms.ModelForm):
  80. role = forms.ChoiceField(label=_("Type"), choices=FORUM_ROLES)
  81. name = forms.CharField(
  82. label=_("Name"),
  83. validators=[validate_sluggable()])
  84. description = forms.CharField(
  85. label=_("Description"), max_length=2048, required=False,
  86. widget=forms.Textarea(attrs={'rows': 3}),
  87. help_text=_("Optional description explaining forum intented "
  88. "purpose."))
  89. redirect_url = forms.URLField(
  90. label=_("Redirect URL"),
  91. validators=[validate_sluggable()],
  92. help_text=_('If forum type is redirect, enter here its URL.'),
  93. required=False)
  94. css_class = forms.CharField(
  95. label=_("CSS class"), required=False,
  96. help_text=_("Optional CSS class used to customize this forum "
  97. "appearance from templates."))
  98. is_closed = forms.YesNoSwitch(
  99. label=_("Closed forum"), required=False,
  100. help_text=_("Only members with valid permissions can post in "
  101. "closed forums."))
  102. css_class = forms.CharField(
  103. label=_("CSS class"), required=False,
  104. help_text=_("Optional CSS class used to customize this forum "
  105. "appearance from templates."))
  106. prune_started_after = forms.IntegerField(
  107. label=_("Thread age"), min_value=0,
  108. help_text=_("Prune thread if number of days since its creation is "
  109. "greater than specified. Enter 0 to disable this "
  110. "pruning criteria."))
  111. prune_replied_after = forms.IntegerField(
  112. label=_("Last reply"), min_value=0,
  113. help_text=_("Prune thread if number of days since last reply is "
  114. "greater than specified. Enter 0 to disable this "
  115. "pruning criteria."))
  116. class Meta:
  117. model = Forum
  118. fields = [
  119. 'role',
  120. 'name',
  121. 'description',
  122. 'redirect_url',
  123. 'css_class',
  124. 'is_closed',
  125. 'prune_started_after',
  126. 'prune_replied_after',
  127. 'archive_pruned_in',
  128. ]
  129. def clean_copy_permissions(self):
  130. data = self.cleaned_data['copy_permissions']
  131. if data and data.pk == self.instance.pk:
  132. message = _("Permissions cannot be copied from forum into itself.")
  133. raise forms.ValidationError(message)
  134. return data
  135. def clean_archive_pruned_in(self):
  136. data = self.cleaned_data['archive_pruned_in']
  137. if data and data.pk == self.instance.pk:
  138. message = _("Forum cannot act as archive for itself.")
  139. raise forms.ValidationError(message)
  140. return data
  141. def clean(self):
  142. data = super(ForumFormBase, self).clean()
  143. self.instance.set_name(data.get('name'))
  144. if data['role'] != 'category':
  145. if not data['new_parent'].level:
  146. message = _("Only categories can have no parent category.")
  147. raise forms.ValidationError(message)
  148. if data['role'] == 'redirect':
  149. if not data.get('redirect_url'):
  150. message = _("This forum is redirect, yet you haven't "
  151. "specified URL to which it should redirect "
  152. "after click.")
  153. raise forms.ValidationError(message)
  154. return data
  155. def ForumFormFactory(instance):
  156. parent_queryset = Forum.objects.all_forums(True).order_by('lft')
  157. if instance.pk:
  158. not_siblings = models.Q(lft__lt=instance.lft)
  159. not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
  160. parent_queryset = parent_queryset.filter(not_siblings)
  161. return type('ForumFormFinal', (ForumFormBase,), {
  162. 'new_parent': ForumChoiceField(
  163. label=_("Parent forum"),
  164. queryset=parent_queryset,
  165. initial=instance.parent,
  166. empty_label=None),
  167. 'copy_permissions': ForumChoiceField(
  168. label=_("Copy permissions"),
  169. help_text=_("You can replace this forum permissions with "
  170. "permissions copied from forum selected here."),
  171. queryset=Forum.objects.all_forums(),
  172. empty_label=_("Don't copy permissions"),
  173. base_level=1,
  174. required=False),
  175. 'archive_pruned_in': ForumChoiceField(
  176. label=_("Archive"),
  177. help_text=_("Instead of being deleted, pruned threads can be "
  178. "moved to designated forum."),
  179. queryset=Forum.objects.all_forums(),
  180. empty_label=_("Don't archive pruned threads"),
  181. base_level=1,
  182. required=False),
  183. })
  184. class DeleteForumFormBase(forms.ModelForm):
  185. class Meta:
  186. model = Forum
  187. fields = []
  188. def clean(self):
  189. data = super(DeleteForumFormBase, self).clean()
  190. if data.get('move_threads_to'):
  191. if data['move_threads_to'].pk == self.instance.pk:
  192. message = _("You are trying to move this forum threads to "
  193. "itself.")
  194. raise forms.ValidationError(message)
  195. if data['move_threads_to'].role == 'category':
  196. message = _("Threads can't be moved to category.")
  197. raise forms.ValidationError(message)
  198. if data['move_threads_to'].role == 'redirect':
  199. message = _("Threads can't be moved to redirect.")
  200. raise forms.ValidationError(message)
  201. moving_to_child = self.instance.has_child(data['move_threads_to'])
  202. if moving_to_child and not data.get('move_children_to'):
  203. message = _("You are trying to move this forum threads to a "
  204. "child forum that will be deleted together with "
  205. "this forum.")
  206. raise forms.ValidationError(message)
  207. if data.get('move_children_to'):
  208. if data['move_children_to'].special_role == 'root_category':
  209. for child in self.instance.get_children().iterator():
  210. if child.role != 'category':
  211. message = _("One or more child forums in forum are not "
  212. "categories and thus cannot be made root "
  213. "categories.")
  214. raise forms.ValidationError(message)
  215. return data
  216. def DeleteFormFactory(instance):
  217. content_queryset = Forum.objects.all_forums().order_by('lft')
  218. fields = {
  219. 'move_threads_to': ForumChoiceField(
  220. label=_("Move forum threads to"),
  221. queryset=content_queryset,
  222. initial=instance.parent,
  223. empty_label=_('Delete with forum'),
  224. required=False),
  225. }
  226. not_siblings = models.Q(lft__lt=instance.lft)
  227. not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
  228. children_queryset = Forum.objects.all_forums(True)
  229. children_queryset = children_queryset.filter(not_siblings).order_by('lft')
  230. if children_queryset.exists():
  231. fields['move_children_to'] = ForumChoiceField(
  232. label=_("Move child forums to"),
  233. queryset=children_queryset,
  234. empty_label=_('Delete with forum'),
  235. required=False)
  236. return type('DeleteForumFormFinal', (DeleteForumFormBase,), fields)
  237. class ForumRoleForm(forms.ModelForm):
  238. name = forms.CharField(label=_("Role name"))
  239. class Meta:
  240. model = ForumRole
  241. fields = ['name']
  242. def RoleForumACLFormFactory(forum, forum_roles, selected_role):
  243. attrs = {
  244. 'forum': forum,
  245. 'role': forms.ModelChoiceField(
  246. label=_("Role"),
  247. required=False,
  248. queryset=forum_roles,
  249. initial=selected_role,
  250. empty_label=_("No access"))
  251. }
  252. return type('RoleForumACLForm', (forms.Form,), attrs)
  253. def ForumRolesACLFormFactory(role, forum_roles, selected_role):
  254. attrs = {
  255. 'role': role,
  256. 'forum_role': forms.ModelChoiceField(
  257. label=_("Role"),
  258. required=False,
  259. queryset=forum_roles,
  260. initial=selected_role,
  261. empty_label=_("No access"))
  262. }
  263. return type('ForumRolesACLForm', (forms.Form,), attrs)