list.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. from django.core.urlresolvers import reverse
  2. from django.db.models import Q, F
  3. from django.forms import ValidationError
  4. from django.shortcuts import redirect
  5. from django.template import RequestContext
  6. from django.utils import timezone
  7. from django.utils.translation import ugettext as _
  8. from misago.acl.utils import ACLError403, ACLError404
  9. from misago.forms import FormLayout, FormFields
  10. from misago.forums.models import Forum
  11. from misago.messages import Message
  12. from misago.readstracker.trackers import ForumsTracker, ThreadsTracker
  13. from misago.threads.forms import MoveThreadsForm, MergeThreadsForm
  14. from misago.threads.models import Thread, Post
  15. from misago.threads.views.base import BaseView
  16. from misago.threads.views.mixins import ThreadsFormMixin
  17. from misago.views import error403, error404
  18. from misago.utils import make_pagination, slugify
  19. class ThreadsView(BaseView, ThreadsFormMixin):
  20. def fetch_forum(self, forum):
  21. self.forum = Forum.objects.get(pk=forum, type='forum')
  22. self.proxy = Forum.objects.parents_aware_forum(self.forum)
  23. self.request.acl.forums.allow_forum_view(self.forum)
  24. self.parents = Forum.objects.forum_parents(self.forum.pk)
  25. if self.forum.lft + 1 != self.forum.rght:
  26. self.forum.subforums = Forum.objects.treelist(self.request.acl.forums, self.forum, tracker=ForumsTracker(self.request.user))
  27. self.tracker = ThreadsTracker(self.request.user, self.forum)
  28. def fetch_threads(self, page):
  29. self.count = self.request.acl.threads.filter_threads(self.request, self.forum, Thread.objects.filter(forum=self.forum).filter(weight__lt=2)).count()
  30. self.pagination = make_pagination(page, self.count, self.request.settings.threads_per_page)
  31. self.threads = []
  32. queryset_anno = Thread.objects.filter(Q(forum=Forum.objects.token_to_pk('annoucements')) | (Q(forum=self.forum) & Q(weight=2)))
  33. queryset_threads = self.request.acl.threads.filter_threads(self.request, self.forum, Thread.objects.filter(forum=self.forum).filter(weight__lt=2)).order_by('-weight', '-last')
  34. if self.request.settings.avatars_on_threads_list:
  35. queryset_anno = queryset_anno.prefetch_related('start_poster', 'last_post')
  36. queryset_threads = queryset_threads.prefetch_related('start_poster', 'last_poster')
  37. for thread in queryset_anno:
  38. self.threads.append(thread)
  39. for thread in queryset_threads:
  40. self.threads.append(thread)
  41. if self.request.settings.threads_per_page < self.count:
  42. self.threads = self.threads[self.pagination['start']:self.pagination['stop']]
  43. for thread in self.threads:
  44. thread.is_read = self.tracker.is_read(thread)
  45. def get_thread_actions(self):
  46. acl = self.request.acl.threads.get_role(self.forum)
  47. actions = []
  48. try:
  49. if acl['can_approve']:
  50. actions.append(('accept', _('Accept threads')))
  51. if acl['can_pin_threads'] == 2:
  52. actions.append(('annouce', _('Change to annoucements')))
  53. if acl['can_pin_threads'] > 0:
  54. actions.append(('sticky', _('Change to sticky threads')))
  55. if acl['can_pin_threads'] > 0:
  56. actions.append(('normal', _('Change to standard thread')))
  57. if acl['can_move_threads_posts']:
  58. actions.append(('move', _('Move threads')))
  59. actions.append(('merge', _('Merge threads')))
  60. if acl['can_close_threads']:
  61. actions.append(('open', _('Open threads')))
  62. actions.append(('close', _('Close threads')))
  63. if acl['can_delete_threads']:
  64. actions.append(('undelete', _('Undelete threads')))
  65. if acl['can_delete_threads']:
  66. actions.append(('soft', _('Soft delete threads')))
  67. if acl['can_delete_threads'] == 2:
  68. actions.append(('hard', _('Hard delete threads')))
  69. except KeyError:
  70. pass
  71. return actions
  72. def action_accept(self, ids):
  73. accepted = 0
  74. posts = 0
  75. users = []
  76. for thread in self.threads:
  77. if thread.pk in ids and thread.moderated:
  78. accepted += 1
  79. # Sync thread and post
  80. thread.moderated = False
  81. thread.replies_moderated -= 1
  82. thread.save(force_update=True)
  83. thread.start_post.moderated = False
  84. thread.start_post.save(force_update=True)
  85. # Sync user
  86. if thread.last_post.user:
  87. thread.start_post.user.threads += 1
  88. thread.start_post.user.posts += 1
  89. users.append(thread.start_post.user)
  90. thread.last_post.set_checkpoint(self.request, 'accepted')
  91. # Sync forum
  92. self.forum.threads += 1
  93. self.forum.threads_delta += 1
  94. self.forum.posts += thread.replies + 1
  95. posts += thread.replies + 1
  96. self.forum.posts_delta += thread.replies + 1
  97. if not self.forum.last_thread_date or self.forum.last_thread_date < thread.last:
  98. self.forum.last_thread = thread
  99. self.forum.last_thread_name = thread.name
  100. self.forum.last_thread_slug = thread.slug
  101. self.forum.last_thread_date = thread.last
  102. self.forum.last_poster = thread.last_poster
  103. self.forum.last_poster_name = thread.last_poster_name
  104. self.forum.last_poster_slug = thread.last_poster_slug
  105. self.forum.last_poster_style = thread.last_poster_style
  106. if accepted:
  107. self.request.monitor['threads'] = int(self.request.monitor['threads']) + accepted
  108. self.request.monitor['posts'] = posts
  109. self.forum.save(force_update=True)
  110. for user in users:
  111. user.save(force_update=True)
  112. self.request.messages.set_flash(Message(_('Selected threads have been marked as reviewed and made visible to other members.')), 'success', 'threads')
  113. def action_annouce(self, ids):
  114. acl = self.request.acl.threads.get_role(self.forum)
  115. annouced = []
  116. for thread in self.threads:
  117. if thread.pk in ids and thread.weight < 2:
  118. annouced.append(thread.pk)
  119. if annouced:
  120. Thread.objects.filter(id__in=annouced).update(weight=2)
  121. self.request.messages.set_flash(Message(_('Selected threads have been turned into annoucements.')), 'success', 'threads')
  122. def action_sticky(self, ids):
  123. sticky = []
  124. for thread in self.threads:
  125. if thread.pk in ids and thread.weight != 1 and (acl['can_pin_threads'] == 2 or thread.weight < 2):
  126. sticky.append(thread.pk)
  127. if sticky:
  128. Thread.objects.filter(id__in=sticky).update(weight=1)
  129. self.request.messages.set_flash(Message(_('Selected threads have been sticked to the top of list.')), 'success', 'threads')
  130. def action_normal(self, ids):
  131. normalised = []
  132. for thread in self.threads:
  133. if thread.pk in ids and thread.weight > 0:
  134. normalised.append(thread.pk)
  135. if normalised:
  136. Thread.objects.filter(id__in=normalised).update(weight=0)
  137. self.request.messages.set_flash(Message(_('Selected threads weight has been removed.')), 'success', 'threads')
  138. def action_open(self, ids):
  139. opened = []
  140. for thread in self.threads:
  141. if thread.pk in ids and thread.closed:
  142. opened.append(thread.pk)
  143. thread.last_post.set_checkpoint(self.request, 'opened')
  144. if opened:
  145. Thread.objects.filter(id__in=opened).update(closed=False)
  146. self.request.messages.set_flash(Message(_('Selected threads have been opened.')), 'success', 'threads')
  147. def action_move(self, ids):
  148. threads = []
  149. for thread in self.threads:
  150. if thread.pk in ids:
  151. threads.append(thread)
  152. if self.request.POST.get('origin') == 'move_form':
  153. form = MoveThreadsForm(self.request.POST,request=self.request,forum=self.forum)
  154. if form.is_valid():
  155. new_forum = form.cleaned_data['new_forum']
  156. for thread in threads:
  157. thread.forum = new_forum
  158. thread.post_set.update(forum=new_forum)
  159. thread.change_set.update(forum=new_forum)
  160. thread.checkpoint_set.update(forum=new_forum)
  161. thread.save(force_update=True)
  162. new_forum.sync()
  163. new_forum.save(force_update=True)
  164. self.forum.sync()
  165. self.forum.save(force_update=True)
  166. self.request.messages.set_flash(Message(_('Selected threads have been moved to "%(forum)s".') % {'forum': new_forum.name}), 'success', 'threads')
  167. return None
  168. self.message = Message(form.non_field_errors()[0], 'error')
  169. else:
  170. form = MoveThreadsForm(request=self.request,forum=self.forum)
  171. return self.request.theme.render_to_response('threads/move.html',
  172. {
  173. 'message': self.message,
  174. 'forum': self.forum,
  175. 'parents': self.parents,
  176. 'threads': threads,
  177. 'form': FormLayout(form),
  178. },
  179. context_instance=RequestContext(self.request));
  180. def action_merge(self, ids):
  181. if len(ids) < 2:
  182. raise ValidationError(_("You have to pick two or more threads to merge."))
  183. threads = []
  184. for thread in self.threads:
  185. if thread.pk in ids:
  186. threads.append(thread)
  187. if self.request.POST.get('origin') == 'merge_form':
  188. form = MergeThreadsForm(self.request.POST,request=self.request,threads=threads)
  189. if form.is_valid():
  190. new_thread = Thread.objects.create(
  191. forum=self.forum,
  192. name=form.cleaned_data['thread_name'],
  193. slug=slugify(form.cleaned_data['thread_name']),
  194. start=timezone.now(),
  195. last=timezone.now()
  196. )
  197. last_merge = 0
  198. last_thread = None
  199. merged = []
  200. for i in range(0, len(threads)):
  201. thread = form.merge_order[i]
  202. merged.append(thread.pk)
  203. if last_thread and last_thread.last > thread.start:
  204. last_merge += thread.merges + 1
  205. thread.post_set.update(thread=new_thread,merge=F('merge') + last_merge)
  206. thread.change_set.update(thread=new_thread)
  207. thread.checkpoint_set.update(thread=new_thread)
  208. last_thread = thread
  209. Thread.objects.filter(id__in=merged).delete()
  210. new_thread.sync()
  211. new_thread.save(force_update=True)
  212. self.forum.sync()
  213. self.forum.save(force_update=True)
  214. self.request.messages.set_flash(Message(_('Selected threads have been merged into new one.')), 'success', 'threads')
  215. return None
  216. self.message = Message(form.non_field_errors()[0], 'error')
  217. else:
  218. form = MergeThreadsForm(request=self.request,threads=threads)
  219. return self.request.theme.render_to_response('threads/merge.html',
  220. {
  221. 'message': self.message,
  222. 'forum': self.forum,
  223. 'parents': self.parents,
  224. 'threads': threads,
  225. 'form': FormLayout(form),
  226. },
  227. context_instance=RequestContext(self.request));
  228. def action_close(self, ids):
  229. closed = []
  230. for thread in self.threads:
  231. if thread.pk in ids and not thread.closed:
  232. closed.append(thread.pk)
  233. thread.last_post.set_checkpoint(self.request, 'closed')
  234. if closed:
  235. Thread.objects.filter(id__in=closed).update(closed=True)
  236. self.request.messages.set_flash(Message(_('Selected threads have been closed.')), 'success', 'threads')
  237. def action_undelete(self, ids):
  238. undeleted = []
  239. posts = 0
  240. for thread in self.threads:
  241. if thread.pk in ids and thread.deleted:
  242. undeleted.append(thread.pk)
  243. posts += thread.replies + 1
  244. thread.start_post.deleted = False
  245. thread.start_post.save(force_update=True)
  246. thread.last_post.set_checkpoint(self.request, 'undeleted')
  247. if undeleted:
  248. self.request.monitor['threads'] = int(self.request.monitor['threads']) + len(undeleted)
  249. self.request.monitor['posts'] = int(self.request.monitor['posts']) + posts
  250. self.forum.threads += len(undeleted)
  251. self.forum.posts += posts
  252. self.forum.save(force_update=True)
  253. Thread.objects.filter(id__in=undeleted).update(deleted=False)
  254. self.request.messages.set_flash(Message(_('Selected threads have been undeleted.')), 'success', 'threads')
  255. def action_soft(self, ids):
  256. deleted = []
  257. posts = 0
  258. for thread in self.threads:
  259. if thread.pk in ids and not thread.deleted:
  260. deleted.append(thread.pk)
  261. posts += thread.replies + 1
  262. thread.start_post.deleted = True
  263. thread.start_post.save(force_update=True)
  264. thread.last_post.set_checkpoint(self.request, 'deleted')
  265. if deleted:
  266. self.request.monitor['threads'] = int(self.request.monitor['threads']) - len(deleted)
  267. self.request.monitor['posts'] = int(self.request.monitor['posts']) - posts
  268. self.forum.sync()
  269. self.forum.save(force_update=True)
  270. Thread.objects.filter(id__in=deleted).update(deleted=True)
  271. self.request.messages.set_flash(Message(_('Selected threads have been softly deleted.')), 'success', 'threads')
  272. def action_hard(self, ids):
  273. deleted = []
  274. posts = 0
  275. for thread in self.threads:
  276. if thread.pk in ids:
  277. deleted.append(thread.pk)
  278. posts += thread.replies + 1
  279. thread.delete()
  280. if deleted:
  281. self.request.monitor['threads'] = int(self.request.monitor['threads']) - len(deleted)
  282. self.request.monitor['posts'] = int(self.request.monitor['posts']) - posts
  283. self.forum.sync()
  284. self.forum.save(force_update=True)
  285. self.request.messages.set_flash(Message(_('Selected threads have been deleted.')), 'success', 'threads')
  286. def __call__(self, request, slug=None, forum=None, page=0):
  287. self.request = request
  288. self.pagination = None
  289. self.parents = None
  290. self.message = request.messages.get_message('threads')
  291. try:
  292. self.fetch_forum(forum)
  293. self.fetch_threads(page)
  294. self.make_form()
  295. if self.form:
  296. response = self.handle_form()
  297. if response:
  298. return response
  299. except Forum.DoesNotExist:
  300. return error404(request)
  301. except ACLError403 as e:
  302. return error403(request, e.message)
  303. except ACLError404 as e:
  304. return error404(request, e.message)
  305. # Merge proxy into forum
  306. self.forum.closed = self.proxy.closed
  307. return request.theme.render_to_response('threads/list.html',
  308. {
  309. 'message': self.message,
  310. 'forum': self.forum,
  311. 'parents': self.parents,
  312. 'count': self.count,
  313. 'list_form': FormFields(self.form).fields if self.form else None,
  314. 'threads': self.threads,
  315. 'pagination': self.pagination,
  316. },
  317. context_instance=RequestContext(request));