list.py 5.4 KB

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