moderation.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from urlparse import urlparse
  2. from django.core.urlresolvers import resolve
  3. from django.http import Http404
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.acl import add_acl
  6. from misago.core import forms
  7. from misago.forums.forms import ForumChoiceField
  8. from misago.forums.permissions import allow_see_forum, allow_browse_forum
  9. from misago.threads.models import Thread
  10. from misago.threads.permissions import allow_see_thread
  11. from misago.threads.validators import validate_title
  12. class MergeThreadsForm(forms.Form):
  13. merged_thread_title = forms.CharField(label=_("Merged thread title"),
  14. required=False)
  15. def clean(self):
  16. data = super(MergeThreadsForm, self).clean()
  17. merged_thread_title = data.get('merged_thread_title')
  18. if merged_thread_title:
  19. validate_title(merged_thread_title)
  20. else:
  21. message = _("You have to enter merged thread title.")
  22. raise forms.ValidationError(message)
  23. return data
  24. class MoveThreadsForm(forms.Form):
  25. new_forum = ForumChoiceField(label=_("Move threads to forum"),
  26. empty_label=None)
  27. def __init__(self, *args, **kwargs):
  28. self.forum = kwargs.pop('forum')
  29. acl = kwargs.pop('acl')
  30. super(MoveThreadsForm, self).__init__(*args, **kwargs)
  31. self.fields['new_forum'].set_acl(acl)
  32. def clean(self):
  33. data = super(MoveThreadsForm, self).clean()
  34. new_forum = data.get('new_forum')
  35. if new_forum:
  36. if new_forum.is_category:
  37. message = _("You can't move threads to category.")
  38. raise forms.ValidationError(message)
  39. if new_forum.is_redirect:
  40. message = _("You can't move threads to redirect.")
  41. raise forms.ValidationError(message)
  42. if new_forum.pk == self.forum.pk:
  43. message = _("New forum is same as current one.")
  44. raise forms.ValidationError(message)
  45. else:
  46. raise forms.ValidationError(_("You have to select forum."))
  47. return data
  48. class MoveThreadForm(MoveThreadsForm):
  49. new_forum = ForumChoiceField(label=_("Move thread to forum"),
  50. empty_label=None)
  51. class MovePostsForm(forms.Form):
  52. new_thread_url = forms.CharField(
  53. label=_("New thread link"),
  54. help_text=_("Paste link to thread you want selected posts moved to."))
  55. def __init__(self, *args, **kwargs):
  56. self.user = kwargs.pop('user')
  57. self.thread = kwargs.pop('thread')
  58. self.new_thread = None
  59. super(MovePostsForm, self).__init__(*args, **kwargs)
  60. def clean(self):
  61. data = super(MovePostsForm, self).clean()
  62. new_thread_url = data.get('new_thread_url')
  63. try:
  64. if not new_thread_url:
  65. raise Http404()
  66. resolution = resolve(urlparse(new_thread_url).path)
  67. if not 'thread_id' in resolution.kwargs:
  68. raise Http404()
  69. queryset = Thread.objects.select_related('forum')
  70. self.new_thread = queryset.get(id=resolution.kwargs['thread_id'])
  71. add_acl(self.user, self.new_thread.forum)
  72. add_acl(self.user, self.new_thread)
  73. allow_see_forum(self.user, self.new_thread.forum)
  74. allow_browse_forum(self.user, self.new_thread.forum)
  75. allow_see_thread(self.user, self.new_thread)
  76. except (Http404, Thread.DoesNotExist):
  77. message = _("You have to enter valid link to thread.")
  78. raise forms.ValidationError(message)
  79. if self.thread == self.new_thread:
  80. message = _("New thread is same as current one.")
  81. raise forms.ValidationError(message)
  82. if self.new_thread.forum.special_role:
  83. message = _("You can't move posts to special threads.")
  84. raise forms.ValidationError(message)
  85. return data
  86. class SplitThreadForm(forms.Form):
  87. forum = ForumChoiceField(label=_("New thread forum"),
  88. empty_label=None)
  89. thread_title = forms.CharField(label=_("New thread title"),
  90. required=False)
  91. def __init__(self, *args, **kwargs):
  92. acl = kwargs.pop('acl')
  93. super(SplitThreadForm, self).__init__(*args, **kwargs)
  94. self.fields['forum'].set_acl(acl)
  95. def clean(self):
  96. data = super(SplitThreadForm, self).clean()
  97. forum = data.get('forum')
  98. if forum:
  99. if forum.is_category:
  100. message = _("You can't start threads in category.")
  101. raise forms.ValidationError(message)
  102. if forum.is_redirect:
  103. message = _("You can't start threads in redirect.")
  104. raise forms.ValidationError(message)
  105. else:
  106. raise forms.ValidationError(_("You have to select forum."))
  107. thread_title = data.get('thread_title')
  108. if thread_title:
  109. validate_title(thread_title)
  110. else:
  111. message = _("You have to enter new thread title.")
  112. raise forms.ValidationError(message)
  113. return data