forms.py 12 KB

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