list.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django.core.urlresolvers import reverse
  2. from django.shortcuts import redirect
  3. from django.template import RequestContext
  4. from django.utils.translation import ugettext as _
  5. from misago.acl.utils import ACLError403, ACLError404
  6. from misago.forms import FormLayout, FormFields
  7. from misago.forums.models import Forum
  8. from misago.messages import Message
  9. from misago.readstracker.trackers import ForumsTracker, ThreadsTracker
  10. from misago.threads.models import Thread, Post
  11. from misago.threads.views.base import BaseView
  12. from misago.threads.views.mixins import ThreadsFormMixin
  13. from misago.views import error403, error404
  14. from misago.utils import make_pagination
  15. class ThreadsView(BaseView, ThreadsFormMixin):
  16. def fetch_forum(self, forum):
  17. self.forum = Forum.objects.get(pk=forum, type='forum')
  18. self.request.acl.forums.allow_forum_view(self.forum)
  19. self.parents = self.forum.get_ancestors().filter(level__gt=1)
  20. if self.forum.lft + 1 != self.forum.rght:
  21. self.forum.subforums = Forum.objects.treelist(self.request.acl.forums, self.forum, tracker=ForumsTracker(self.request.user))
  22. self.tracker = ThreadsTracker(self.request.user, self.forum)
  23. def fetch_threads(self, page):
  24. self.count = self.request.acl.threads.filter_threads(self.request, self.forum, Thread.objects.filter(forum=self.forum)).count()
  25. self.threads = self.request.acl.threads.filter_threads(self.request, self.forum, Thread.objects.filter(forum=self.forum)).order_by('-weight', '-last')
  26. self.pagination = make_pagination(page, self.count, self.request.settings.threads_per_page)
  27. if self.request.settings.threads_per_page < self.count:
  28. self.threads = self.threads[self.pagination['start']:self.pagination['stop']]
  29. for thread in self.threads:
  30. thread.is_read = self.tracker.is_read(thread)
  31. def get_thread_actions(self):
  32. acl = self.request.acl.threads.get_role(self.forum)
  33. actions = []
  34. try:
  35. if acl['can_approve']:
  36. actions.append(('accept', _('Accept threads')))
  37. if acl['can_make_annoucements']:
  38. actions.append(('annouce', _('Change to annoucements')))
  39. if acl['can_pin_threads']:
  40. actions.append(('sticky', _('Change to sticky threads')))
  41. if acl['can_make_annoucements'] or acl['can_pin_threads']:
  42. actions.append(('normal', _('Change to standard thread')))
  43. if acl['can_move_threads_posts']:
  44. actions.append(('move', _('Move threads')))
  45. actions.append(('merge', _('Merge threads')))
  46. if acl['can_close_threads']:
  47. actions.append(('open', _('Open threads')))
  48. actions.append(('close', _('Close threads')))
  49. if acl['can_delete_threads']:
  50. actions.append(('undelete', _('Undelete threads')))
  51. if acl['can_delete_threads']:
  52. actions.append(('soft', _('Soft delete threads')))
  53. if acl['can_delete_threads'] == 2:
  54. actions.append(('hard', _('Hard delete threads')))
  55. except KeyError:
  56. pass
  57. return actions
  58. def action_accept(self, ids, threads):
  59. accepted = 0
  60. users = []
  61. for thread in self.threads.prefetch_related('last_post', 'last_post__user').all():
  62. if thread.pk in ids and thread.moderated:
  63. accepted += 1
  64. # Sync thread and post
  65. thread.moderated = False
  66. thread.replies_moderated -= 1
  67. thread.save(force_update=True)
  68. thread.last_post.moderated = False
  69. thread.last_post.save(force_update=True)
  70. # Sync user
  71. if thread.last_post.user:
  72. thread.last_post.user.threads += 1
  73. thread.last_post.user.posts += 1
  74. users.append(thread.last_post.user)
  75. # Sync forum
  76. self.forum.threads += 1
  77. self.forum.threads_delta += 1
  78. self.forum.posts += 1
  79. self.forum.posts_delta += 1
  80. if not self.forum.last_thread_date or self.forum.last_thread_date < thread.last:
  81. self.forum.last_thread = thread
  82. self.forum.last_thread_name = thread.name
  83. self.forum.last_thread_slug = thread.slug
  84. self.forum.last_thread_date = thread.last
  85. self.forum.last_poster = thread.last_poster
  86. self.forum.last_poster_name = thread.last_poster_name
  87. self.forum.last_poster_slug = thread.last_poster_slug
  88. self.forum.last_poster_style = thread.last_poster_style
  89. if accepted:
  90. self.request.monitor['threads'] = int(self.request.monitor['threads']) + accepted
  91. self.request.monitor['posts'] = int(self.request.monitor['posts']) + accepted
  92. self.forum.save(force_update=True)
  93. for user in users:
  94. user.save(force_update=True)
  95. self.request.messages.set_flash(Message(_('Selected threads have been marked as reviewed and made visible to other members.')), 'success', 'threads')
  96. def __call__(self, request, slug=None, forum=None, page=0):
  97. self.request = request
  98. self.pagination = None
  99. self.parents = None
  100. self.message = request.messages.get_message('threads')
  101. try:
  102. self.fetch_forum(forum)
  103. self.fetch_threads(page)
  104. self.make_form()
  105. if self.form:
  106. response = self.handle_form()
  107. if response:
  108. return response
  109. except Forum.DoesNotExist:
  110. return error404(request)
  111. except ACLError403 as e:
  112. return error403(request, e.message)
  113. except ACLError404 as e:
  114. return error404(request, e.message)
  115. return request.theme.render_to_response('threads/list.html',
  116. {
  117. 'message': self.message,
  118. 'forum': self.forum,
  119. 'parents': self.parents,
  120. 'count': self.count,
  121. 'list_form': FormFields(self.form).fields if self.form else None,
  122. 'threads': self.threads,
  123. 'pagination': self.pagination,
  124. },
  125. context_instance=RequestContext(request));