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. if not 'queryset' in kwargs:
  55. kwargs['queryset'] = ForumListLazyQueryset(parent, acl)
  56. super(ForumChoiceField, self).__init__(*args, **kwargs)
  57. class ForumsMultipleChoiceField(MisagoForumMixin, TreeNodeMultipleChoiceField):
  58. def __init__(self, parent=None, acl=None, *args, **kwargs):
  59. if not 'queryset' in kwargs:
  60. kwargs['queryset'] = ForumListLazyQueryset(parent, acl)
  61. super(ForumsMultipleChoiceField, self).__init__(*args, **kwargs)
  62. """
  63. Forms
  64. """
  65. FORUM_ROLES = (
  66. ('category', _('Category')),
  67. ('forum', _('Forum')),
  68. ('redirect', _('Redirect')),
  69. )
  70. class AdminForumChoiceField(TreeNodeChoiceField):
  71. def __init__(self, *args, **kwargs):
  72. self.base_level = kwargs.pop('base_level', 1)
  73. kwargs['level_indicator'] = kwargs.get('level_indicator', '- - ')
  74. super(AdminForumChoiceField, self).__init__(*args, **kwargs)
  75. def _get_level_indicator(self, obj):
  76. level = getattr(obj, obj._mptt_meta.level_attr) - self.base_level
  77. if level > 0:
  78. return mark_safe(conditional_escape(self.level_indicator) * level)
  79. else:
  80. return ''
  81. class ForumFormBase(forms.ModelForm):
  82. role = forms.ChoiceField(label=_("Type"), choices=FORUM_ROLES)
  83. name = forms.CharField(
  84. label=_("Name"),
  85. validators=[validate_sluggable()])
  86. description = forms.CharField(
  87. label=_("Description"), max_length=2048, required=False,
  88. widget=forms.Textarea(attrs={'rows': 3}),
  89. help_text=_("Optional description explaining forum intented "
  90. "purpose."))
  91. redirect_url = forms.URLField(
  92. label=_("Redirect URL"),
  93. validators=[validate_sluggable()],
  94. help_text=_('If forum type is redirect, enter here its URL.'),
  95. required=False)
  96. css_class = forms.CharField(
  97. label=_("CSS class"), required=False,
  98. help_text=_("Optional CSS class used to customize this forum "
  99. "appearance from templates."))
  100. is_closed = forms.YesNoSwitch(
  101. label=_("Closed forum"), required=False,
  102. help_text=_("Only members with valid permissions can post in "
  103. "closed forums."))
  104. css_class = forms.CharField(
  105. label=_("CSS class"), required=False,
  106. help_text=_("Optional CSS class used to customize this forum "
  107. "appearance from templates."))
  108. prune_started_after = forms.IntegerField(
  109. label=_("Thread age"), min_value=0,
  110. help_text=_("Prune thread if number of days since its creation is "
  111. "greater than specified. Enter 0 to disable this "
  112. "pruning criteria."))
  113. prune_replied_after = forms.IntegerField(
  114. label=_("Last reply"), min_value=0,
  115. help_text=_("Prune thread if number of days since last reply is "
  116. "greater than specified. Enter 0 to disable this "
  117. "pruning criteria."))
  118. class Meta:
  119. model = Forum
  120. fields = [
  121. 'role',
  122. 'name',
  123. 'description',
  124. 'redirect_url',
  125. 'css_class',
  126. 'is_closed',
  127. 'prune_started_after',
  128. 'prune_replied_after',
  129. 'archive_pruned_in',
  130. ]
  131. def clean_copy_permissions(self):
  132. data = self.cleaned_data['copy_permissions']
  133. if data and data.pk == self.instance.pk:
  134. message = _("Permissions cannot be copied from forum into itself.")
  135. raise forms.ValidationError(message)
  136. return data
  137. def clean_archive_pruned_in(self):
  138. data = self.cleaned_data['archive_pruned_in']
  139. if data and data.pk == self.instance.pk:
  140. message = _("Forum cannot act as archive for itself.")
  141. raise forms.ValidationError(message)
  142. return data
  143. def clean(self):
  144. data = super(ForumFormBase, self).clean()
  145. self.instance.set_name(data.get('name'))
  146. if data['role'] != 'category':
  147. if not data['new_parent'].level:
  148. message = _("Only categories can have no parent category.")
  149. raise forms.ValidationError(message)
  150. if data['role'] == 'redirect':
  151. if not data.get('redirect_url'):
  152. message = _("This forum is redirect, yet you haven't "
  153. "specified URL to which it should redirect "
  154. "after click.")
  155. raise forms.ValidationError(message)
  156. return data
  157. def ForumFormFactory(instance):
  158. parent_queryset = Forum.objects.all_forums(True).order_by('lft')
  159. if instance.pk:
  160. not_siblings = models.Q(lft__lt=instance.lft)
  161. not_siblings = not_siblings | models.Q(rght__gt=instance.rght)
  162. parent_queryset = parent_queryset.filter(not_siblings)
  163. return type('ForumFormFinal', (ForumFormBase,), {
  164. 'new_parent': AdminForumChoiceField(
  165. label=_("Parent forum"),
  166. queryset=parent_queryset,
  167. initial=instance.parent,
  168. empty_label=None),
  169. 'copy_permissions': AdminForumChoiceField(
  170. label=_("Copy permissions"),
  171. help_text=_("You can replace this forum permissions with "
  172. "permissions copied from forum selected here."),
  173. queryset=Forum.objects.all_forums(),
  174. empty_label=_("Don't copy permissions"),
  175. required=False),
  176. 'archive_pruned_in': AdminForumChoiceField(
  177. label=_("Archive"),
  178. help_text=_("Instead of being deleted, pruned threads can be "
  179. "moved to designated forum."),
  180. queryset=Forum.objects.all_forums(),
  181. empty_label=_("Don't archive pruned threads"),
  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 "
  212. "not categories and thus cannot be made "
  213. "root 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': AdminForumChoiceField(
  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'] = AdminForumChoiceField(
  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)