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.conf import settings
  9. from misago.messages import Message
  10. from misago.models import Forum, Thread, Post
  11. from misago.readstrackers import ThreadsTracker
  12. from misago.utils.pagination import make_pagination
  13. from misago.apps.reports.mixins import TypeMixin
  14. class ThreadsListView(ThreadsListBaseView, ThreadsListModeration, TypeMixin):
  15. def fetch_forum(self):
  16. self.forum = Forum.objects.get(special='reports')
  17. def threads_queryset(self):
  18. announcements = self.forum.thread_set.filter(weight=2).prefetch_related('report_for').order_by('-pk')
  19. threads = self.forum.thread_set.filter(weight__lt=2).prefetch_related('report_for').order_by('-weight', '-last')
  20. # Add in first and last poster
  21. if settings.avatars_on_threads_list:
  22. announcements = announcements.prefetch_related('start_poster', 'last_poster')
  23. threads = threads.prefetch_related('start_poster', 'last_poster')
  24. return announcements, threads
  25. def fetch_threads(self):
  26. qs_announcements, qs_threads = self.threads_queryset()
  27. self.count = qs_threads.count()
  28. try:
  29. self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, settings.threads_per_page)
  30. except Http404:
  31. return self.threads_list_redirect()
  32. tracker_forum = ThreadsTracker(self.request, self.forum)
  33. unresolved_count = 0
  34. for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])):
  35. thread.original_weight = thread.weight
  36. if thread.weight == 2:
  37. unresolved_count += 1
  38. thread.is_read = tracker_forum.is_read(thread)
  39. thread.report_forum = None
  40. if thread.report_for_id:
  41. thread.report_forum = Forum.objects.forums_tree.get(thread.report_for.forum_id)
  42. self.threads.append(thread)
  43. if int(self.request.monitor['reported_posts']) != unresolved_count:
  44. self.request.monitor['reported_posts'] = unresolved_count
  45. def threads_actions(self):
  46. acl = self.request.acl.threads.get_role(self.forum)
  47. actions = []
  48. try:
  49. actions.append(('sticky', _('Change to resolved')))
  50. actions.append(('normal', _('Change to bogus')))
  51. if acl['can_delete_threads']:
  52. actions.append(('undelete', _('Restore reports')))
  53. actions.append(('soft', _('Hide reports')))
  54. if acl['can_delete_threads'] == 2:
  55. actions.append(('hard', _('Delete reports')))
  56. except KeyError:
  57. pass
  58. return actions
  59. def mass_resolve(self, ids):
  60. reported_posts = []
  61. reported_threads = []
  62. for thread in self.threads:
  63. if thread.pk in ids:
  64. if thread.original_weight != thread.weight:
  65. if thread.weight == 1:
  66. thread.set_checkpoint(self.request, 'resolved')
  67. if thread.weight == 0:
  68. thread.set_checkpoint(self.request, 'bogus')
  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')