list.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from itertools import chain
  2. from django.core.urlresolvers import reverse
  3. from django.db.models import F
  4. from django.http import Http404
  5. from django.shortcuts import redirect
  6. from django.utils.translation import ugettext as _
  7. from misago.apps.threadtype.list import ThreadsListBaseView, ThreadsListModeration
  8. from misago.messages import Message
  9. from misago.models import Forum, Thread, Post
  10. from misago.readstrackers import ThreadsTracker
  11. from misago.utils.pagination import make_pagination
  12. from misago.apps.reports.mixins import TypeMixin
  13. class ThreadsListView(ThreadsListBaseView, ThreadsListModeration, TypeMixin):
  14. def fetch_forum(self):
  15. self.forum = Forum.objects.get(special='reports')
  16. def threads_queryset(self):
  17. announcements = self.forum.thread_set.filter(weight=2).prefetch_related('report_for').order_by('-pk')
  18. threads = self.forum.thread_set.filter(weight__lt=2).prefetch_related('report_for').order_by('-weight', '-last')
  19. # Add in first and last poster
  20. if self.request.settings.avatars_on_threads_list:
  21. announcements = announcements.prefetch_related('start_poster', 'last_poster')
  22. threads = threads.prefetch_related('start_poster', 'last_poster')
  23. return announcements, threads
  24. def fetch_threads(self):
  25. qs_announcements, qs_threads = self.threads_queryset()
  26. self.count = qs_threads.count()
  27. try:
  28. self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, self.request.settings.threads_per_page)
  29. except Http404:
  30. return self.threads_list_redirect()
  31. tracker_forum = ThreadsTracker(self.request, self.forum)
  32. unresolved_count = 0
  33. for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])):
  34. thread.original_weight = thread.weight
  35. if thread.weight == 2:
  36. unresolved_count += 1
  37. thread.is_read = tracker_forum.is_read(thread)
  38. thread.report_forum = None
  39. if thread.report_for_id:
  40. thread.report_forum = Forum.objects.forums_tree.get(thread.report_for.forum_id)
  41. self.threads.append(thread)
  42. if int(self.request.monitor['reported_posts']) != unresolved_count:
  43. self.request.monitor['reported_posts'] = unresolved_count
  44. def threads_actions(self):
  45. acl = self.request.acl.threads.get_role(self.forum)
  46. actions = []
  47. try:
  48. actions.append(('sticky', _('Change to resolved')))
  49. actions.append(('normal', _('Change to bogus')))
  50. if acl['can_delete_threads']:
  51. actions.append(('undelete', _('Restore reports')))
  52. actions.append(('soft', _('Hide reports')))
  53. if acl['can_delete_threads'] == 2:
  54. actions.append(('hard', _('Delete reports')))
  55. except KeyError:
  56. pass
  57. return actions
  58. def mass_resolve(self, ids):
  59. reported_posts = []
  60. reported_threads = []
  61. for thread in self.threads:
  62. if thread.pk in ids:
  63. if thread.original_weight != thread.weight:
  64. if thread.weight == 1:
  65. thread.last_post.set_checkpoint(self.request, 'resolved')
  66. if thread.weight == 0:
  67. thread.last_post.set_checkpoint(self.request, 'bogus')
  68. thread.last_post.save(force_update=True)
  69. if thread.original_weight == 2 and thread.report_for_id:
  70. reported_posts.append(thread.report_for.pk)
  71. reported_threads.append(thread.report_for.thread_id)
  72. if reported_threads:
  73. Thread.objects.filter(id__in=reported_threads).update(replies_reported=F('replies_reported') - 1)
  74. Post.objects.filter(id__in=reported_posts).update(reported=False)
  75. def action_sticky(self, ids):
  76. if self._action_sticky(ids):
  77. self.mass_resolve(ids)
  78. self.request.messages.set_flash(Message(_('Selected reports were set as resolved.')), 'success', 'threads')
  79. else:
  80. self.request.messages.set_flash(Message(_('No reports were set as resolved.')), 'info', 'threads')
  81. def action_normal(self, ids):
  82. if self._action_normal(ids):
  83. self.mass_resolve(ids)
  84. self.request.messages.set_flash(Message(_('Selected reports were set as bogus.')), 'success', 'threads')
  85. else:
  86. self.request.messages.set_flash(Message(_('No reports were set as bogus.')), 'info', 'threads')
  87. def action_undelete(self, ids):
  88. if self._action_undelete(ids):
  89. self.request.messages.set_flash(Message(_('Selected reports have been restored.')), 'success', 'threads')
  90. else:
  91. self.request.messages.set_flash(Message(_('No reports were restored.')), 'info', 'threads')
  92. def action_soft(self, ids):
  93. if self._action_soft(ids):
  94. self.mass_resolve(ids)
  95. self.request.messages.set_flash(Message(_('Selected reports have been hidden.')), 'success', 'threads')
  96. else:
  97. self.request.messages.set_flash(Message(_('No reports were hidden.')), 'info', 'threads')
  98. def action_hard(self, ids):
  99. if self._action_hard(ids):
  100. self.request.messages.set_flash(Message(_('Selected reports have been deleted.')), 'success', 'threads')
  101. else:
  102. self.request.messages.set_flash(Message(_('No reports were deleted.')), 'info', 'threads')