list.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from itertools import chain
  2. from django.core.urlresolvers import reverse
  3. from django.http import Http404
  4. from django.shortcuts import redirect
  5. from django.utils.translation import ugettext as _
  6. from misago.apps.threadtype.list import ThreadsListBaseView, ThreadsListModeration
  7. from misago.conf import settings
  8. from misago.models import Forum, Thread
  9. from misago.readstrackers import ThreadsTracker
  10. from misago.utils.pagination import make_pagination
  11. from misago.apps.threads.mixins import TypeMixin
  12. class ThreadsListView(ThreadsListBaseView, ThreadsListModeration, TypeMixin):
  13. def fetch_forum(self):
  14. self.forum = Forum.objects.get(pk=self.kwargs.get('forum'), type='forum')
  15. def threads_queryset(self):
  16. announcements = self.request.acl.threads.filter_threads(self.request, self.forum, self.forum.thread_set).filter(weight=2).order_by('-pk')
  17. threads = self.request.acl.threads.filter_threads(self.request, self.forum, self.forum.thread_set).filter(weight__lt=2).order_by('-weight', '-last')
  18. # Dont display threads by ignored users (unless they are important)
  19. if self.request.user.is_authenticated():
  20. ignored_users = self.request.user.ignored_users()
  21. if ignored_users:
  22. threads = threads.extra(where=["`threads_thread`.`start_poster_id` IS NULL OR `threads_thread`.`start_poster_id` NOT IN (%s)" % ','.join([str(i) for i in ignored_users])])
  23. # Add in first and last poster
  24. if settings.avatars_on_threads_list:
  25. announcements = announcements.prefetch_related('start_poster', 'last_poster')
  26. threads = threads.prefetch_related('start_poster', 'last_poster')
  27. return announcements, threads
  28. def fetch_threads(self):
  29. qs_announcements, qs_threads = self.threads_queryset()
  30. self.count = qs_threads.count()
  31. try:
  32. self.pagination = make_pagination(self.kwargs.get('page', 0), self.count, settings.threads_per_page)
  33. except Http404:
  34. return self.threads_list_redirect()
  35. tracker_forum = ThreadsTracker(self.request, self.forum)
  36. for thread in list(chain(qs_announcements, qs_threads[self.pagination['start']:self.pagination['stop']])):
  37. thread.is_read = tracker_forum.is_read(thread)
  38. self.threads.append(thread)
  39. def threads_actions(self):
  40. acl = self.request.acl.threads.get_role(self.forum)
  41. actions = []
  42. try:
  43. if acl['can_approve']:
  44. actions.append(('accept', _('Accept threads')))
  45. if acl['can_pin_threads'] == 2:
  46. actions.append(('annouce', _('Change to announcements')))
  47. if acl['can_pin_threads'] > 0:
  48. actions.append(('sticky', _('Change to sticky threads')))
  49. if acl['can_pin_threads'] > 0:
  50. actions.append(('normal', _('Change to standard thread')))
  51. if acl['can_move_threads_posts']:
  52. actions.append(('move', _('Move threads')))
  53. actions.append(('merge', _('Merge threads')))
  54. if acl['can_close_threads']:
  55. actions.append(('open', _('Open threads')))
  56. actions.append(('close', _('Close threads')))
  57. if acl['can_delete_threads']:
  58. actions.append(('undelete', _('Restore threads')))
  59. actions.append(('soft', _('Hide threads')))
  60. if acl['can_delete_threads'] == 2:
  61. actions.append(('hard', _('Delete threads')))
  62. except KeyError:
  63. pass
  64. return actions