moderation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from django.forms import ValidationError
  2. from django.template import RequestContext
  3. from django.utils.translation import ugettext as _
  4. from misago.forms import FormLayout
  5. from misago.messages import Message
  6. from misago.models import Forum, Thread, Post
  7. from misago.apps.forumbase.list.forms import MoveThreadsForm, MergeThreadsForm
  8. class ThreadsListModeration(object):
  9. def action_accept(self, ids):
  10. accepted = 0
  11. last_posts = []
  12. users = []
  13. for thread in self.threads:
  14. if thread.pk in ids and thread.moderated:
  15. accepted += 1
  16. # Sync thread and post
  17. thread.moderated = False
  18. thread.replies_moderated -= 1
  19. thread.save(force_update=True)
  20. thread.start_post.moderated = False
  21. thread.start_post.save(force_update=True)
  22. thread.last_post.set_checkpoint(self.request, 'accepted')
  23. last_posts.append(thread.last_post.pk)
  24. # Sync user
  25. if thread.last_post.user:
  26. thread.start_post.user.threads += 1
  27. thread.start_post.user.posts += 1
  28. users.append(thread.start_post.user)
  29. if accepted:
  30. Post.objects.filter(id__in=last_posts).update(checkpoints=True)
  31. self.request.monitor['threads'] = int(self.request.monitor['threads']) + accepted
  32. self.request.monitor['posts'] = int(self.request.monitor['posts']) + accepted
  33. self.forum.sync()
  34. self.forum.save(force_update=True)
  35. for user in users:
  36. user.save(force_update=True)
  37. self.request.messages.set_flash(Message(_('Selected threads have been marked as reviewed and made visible to other members.')), 'success', 'threads')
  38. def action_annouce(self, ids):
  39. acl = self.request.acl.threads.get_role(self.forum)
  40. annouced = []
  41. for thread in self.threads:
  42. if thread.pk in ids and thread.weight < 2:
  43. annouced.append(thread.pk)
  44. if annouced:
  45. Thread.objects.filter(id__in=annouced).update(weight=2)
  46. self.request.messages.set_flash(Message(_('Selected threads have been turned into announcements.')), 'success', 'threads')
  47. def action_sticky(self, ids):
  48. acl = self.request.acl.threads.get_role(self.forum)
  49. sticky = []
  50. for thread in self.threads:
  51. if thread.pk in ids and thread.weight != 1 and (acl['can_pin_threads'] == 2 or thread.weight < 2):
  52. sticky.append(thread.pk)
  53. if sticky:
  54. Thread.objects.filter(id__in=sticky).update(weight=1)
  55. self.request.messages.set_flash(Message(_('Selected threads have been sticked to the top of list.')), 'success', 'threads')
  56. def action_normal(self, ids):
  57. normalised = []
  58. for thread in self.threads:
  59. if thread.pk in ids and thread.weight > 0:
  60. normalised.append(thread.pk)
  61. if normalised:
  62. Thread.objects.filter(id__in=normalised).update(weight=0)
  63. self.request.messages.set_flash(Message(_('Selected threads weight has been removed.')), 'success', 'threads')
  64. def action_move(self, ids):
  65. threads = []
  66. for thread in self.threads:
  67. if thread.pk in ids:
  68. threads.append(thread)
  69. if self.request.POST.get('origin') == 'move_form':
  70. form = MoveThreadsForm(self.request.POST, request=self.request, forum=self.forum)
  71. if form.is_valid():
  72. new_forum = form.cleaned_data['new_forum']
  73. for thread in threads:
  74. thread.move_to(new_forum)
  75. thread.save(force_update=True)
  76. new_forum.sync()
  77. new_forum.save(force_update=True)
  78. self.forum.sync()
  79. self.forum.save(force_update=True)
  80. self.request.messages.set_flash(Message(_('Selected threads have been moved to "%(forum)s".') % {'forum': new_forum.name}), 'success', 'threads')
  81. return None
  82. self.message = Message(form.non_field_errors()[0], 'error')
  83. else:
  84. form = MoveThreadsForm(request=self.request, forum=self.forum)
  85. return self.request.theme.render_to_response(('%s/move_threads.html' % self.templates_prefix),
  86. {
  87. 'message': self.message,
  88. 'forum': self.forum,
  89. 'parents': self.parents,
  90. 'threads': threads,
  91. 'form': FormLayout(form),
  92. },
  93. context_instance=RequestContext(self.request));
  94. def action_merge(self, ids):
  95. if len(ids) < 2:
  96. raise ValidationError(_("You have to pick two or more threads to merge."))
  97. threads = []
  98. for thread in self.threads:
  99. if thread.pk in ids:
  100. threads.append(thread)
  101. if self.request.POST.get('origin') == 'merge_form':
  102. form = MergeThreadsForm(self.request.POST, request=self.request, threads=threads)
  103. if form.is_valid():
  104. new_thread = Thread.objects.create(
  105. forum=form.cleaned_data['new_forum'],
  106. name=form.cleaned_data['thread_name'],
  107. slug=slugify(form.cleaned_data['thread_name']),
  108. start=timezone.now(),
  109. last=timezone.now()
  110. )
  111. last_merge = 0
  112. last_thread = None
  113. merged = []
  114. for i in range(0, len(threads)):
  115. thread = form.merge_order[i]
  116. merged.append(thread.pk)
  117. if last_thread and last_thread.last > thread.start:
  118. last_merge += thread.merges + 1
  119. thread.merge_with(new_thread, last_merge=last_merge)
  120. last_thread = thread
  121. Thread.objects.filter(id__in=merged).delete()
  122. new_thread.sync()
  123. new_thread.save(force_update=True)
  124. self.forum.sync()
  125. self.forum.save(force_update=True)
  126. if form.cleaned_data['new_forum'].pk != self.forum.pk:
  127. form.cleaned_data['new_forum'].sync()
  128. form.cleaned_data['new_forum'].save(force_update=True)
  129. self.request.messages.set_flash(Message(_('Selected threads have been merged into new one.')), 'success', 'threads')
  130. return None
  131. self.message = Message(form.non_field_errors()[0], 'error')
  132. else:
  133. form = MergeThreadsForm(request=self.request, threads=threads)
  134. return self.request.theme.render_to_response(('%s/merge.html' % self.templates_prefix),
  135. {
  136. 'message': self.message,
  137. 'forum': self.forum,
  138. 'parents': self.parents,
  139. 'threads': threads,
  140. 'form': FormLayout(form),
  141. },
  142. context_instance=RequestContext(self.request));
  143. def action_open(self, ids):
  144. opened = []
  145. last_posts = []
  146. for thread in self.threads:
  147. if thread.pk in ids and thread.closed:
  148. opened.append(thread.pk)
  149. thread.last_post.set_checkpoint(self.request, 'opened')
  150. last_posts.append(thread.last_post.pk)
  151. if opened:
  152. Post.objects.filter(id__in=last_posts).update(checkpoints=True)
  153. Thread.objects.filter(id__in=opened).update(closed=False)
  154. self.request.messages.set_flash(Message(_('Selected threads have been opened.')), 'success', 'threads')
  155. def action_close(self, ids):
  156. closed = []
  157. last_posts = []
  158. for thread in self.threads:
  159. if thread.pk in ids and not thread.closed:
  160. closed.append(thread.pk)
  161. thread.last_post.set_checkpoint(self.request, 'closed')
  162. last_posts.append(thread.last_post.pk)
  163. if closed:
  164. Post.objects.filter(id__in=last_posts).update(checkpoints=True)
  165. Thread.objects.filter(id__in=closed).update(closed=True)
  166. self.request.messages.set_flash(Message(_('Selected threads have been closed.')), 'success', 'threads')
  167. def action_undelete(self, ids):
  168. undeleted = []
  169. last_posts = []
  170. posts = 0
  171. for thread in self.threads:
  172. if thread.pk in ids and thread.deleted:
  173. undeleted.append(thread.pk)
  174. posts += thread.replies + 1
  175. thread.start_post.deleted = False
  176. thread.start_post.save(force_update=True)
  177. thread.last_post.set_checkpoint(self.request, 'undeleted')
  178. if undeleted:
  179. self.request.monitor['threads'] = int(self.request.monitor['threads']) + len(undeleted)
  180. self.request.monitor['posts'] = int(self.request.monitor['posts']) + posts
  181. self.forum.sync()
  182. self.forum.save(force_update=True)
  183. Post.objects.filter(id__in=last_posts).update(checkpoints=True)
  184. Thread.objects.filter(id__in=undeleted).update(deleted=False)
  185. self.request.messages.set_flash(Message(_('Selected threads have been undeleted.')), 'success', 'threads')
  186. def action_soft(self, ids):
  187. deleted = []
  188. last_posts = []
  189. posts = 0
  190. for thread in self.threads:
  191. if thread.pk in ids and not thread.deleted:
  192. deleted.append(thread.pk)
  193. posts += thread.replies + 1
  194. thread.start_post.deleted = True
  195. thread.start_post.save(force_update=True)
  196. thread.last_post.set_checkpoint(self.request, 'deleted')
  197. last_posts.append(thread.last_post.pk)
  198. if deleted:
  199. self.request.monitor['threads'] = int(self.request.monitor['threads']) - len(deleted)
  200. self.request.monitor['posts'] = int(self.request.monitor['posts']) - posts
  201. self.forum.sync()
  202. self.forum.save(force_update=True)
  203. Post.objects.filter(id__in=last_posts).update(checkpoints=True)
  204. Thread.objects.filter(id__in=deleted).update(deleted=True)
  205. self.request.messages.set_flash(Message(_('Selected threads have been softly deleted.')), 'success', 'threads')
  206. def action_hard(self, ids):
  207. deleted = []
  208. posts = 0
  209. for thread in self.threads:
  210. if thread.pk in ids:
  211. deleted.append(thread.pk)
  212. posts += thread.replies + 1
  213. thread.delete()
  214. if deleted:
  215. self.request.monitor['threads'] = int(self.request.monitor['threads']) - len(deleted)
  216. self.request.monitor['posts'] = int(self.request.monitor['posts']) - posts
  217. self.forum.sync()
  218. self.forum.save(force_update=True)
  219. self.request.messages.set_flash(Message(_('Selected threads have been deleted.')), 'success', 'threads')