views.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.core.urlresolvers import reverse
  2. from django.db.models import Q, F
  3. from django.http import Http404
  4. from django.shortcuts import redirect
  5. from django.template import RequestContext
  6. from django.utils.translation import ugettext as _
  7. from misago.apps.errors import error403
  8. from misago.conf import settings
  9. from misago.decorators import block_guest
  10. from misago.messages import Message
  11. from misago.models import Forum, WatchedThread
  12. from misago.shortcuts import render_to_response
  13. from misago.utils.pagination import make_pagination
  14. @block_guest
  15. def watched_threads(request, page=0, new=False):
  16. # Find mode and fetch threads
  17. readable_forums = Forum.objects.readable_forums(request.acl, True)
  18. starter_readable_forums = Forum.objects.starter_readable_forums(request.acl)
  19. if not readable_forums and not readable_forums:
  20. return error403(request, _("%(username), you cannot read any forums.") % {'username': request.user.username})
  21. private_threads_pk = Forum.objects.special_pk('private_threads')
  22. if not settings.enable_private_threads and private_threads_pk in readable_forums:
  23. readable_forums.remove(private_threads_pk)
  24. queryset = WatchedThread.objects.filter(user=request.user).filter(thread__moderated=False).filter(thread__deleted=False).select_related('thread')
  25. if starter_readable_forums and readable_forums:
  26. queryset = queryset.filter(Q(forum_id__in=readable_forums) | Q(forum_id__in=starter_readable_forums, starter_id=request.user.pk))
  27. elif starter_readable_forums:
  28. queryset = queryset.filter(starter_id__in=request.user.pk).filter(forum_id__in=starter_readable_forums)
  29. else:
  30. queryset = queryset.filter(forum_id__in=readable_forums)
  31. if settings.avatars_on_threads_list:
  32. queryset = queryset.prefetch_related('thread__last_poster')
  33. if new:
  34. queryset = queryset.filter(last_read__lt=F('thread__last'))
  35. count = queryset.count()
  36. try:
  37. pagination = make_pagination(page, count, settings.threads_per_page)
  38. except Http404:
  39. if new:
  40. return redirect(reverse('watched_threads_new'))
  41. return redirect(reverse('watched_threads'))
  42. queryset = queryset.order_by('-thread__last')
  43. if settings.threads_per_page < count:
  44. queryset = queryset[pagination['start']:pagination['stop']]
  45. queryset.prefetch_related('thread__forum', 'thread__start_poster', 'thread__last_poster')
  46. threads = []
  47. for thread in queryset:
  48. thread.thread.send_email = thread.email
  49. thread.thread.is_read = thread.thread.last <= thread.last_read
  50. threads.append(thread.thread)
  51. # Display page
  52. return render_to_response('watched.html',
  53. {
  54. 'items_total': count,
  55. 'pagination': pagination,
  56. 'new': new,
  57. 'threads': threads,
  58. 'message': request.messages.get_message('threads'),
  59. },
  60. context_instance=RequestContext(request))