list.py 5.5 KB

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