goto.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from math import ceil
  2. from django.core.exceptions import PermissionDenied
  3. from django.db.models import Q
  4. from django.shortcuts import get_object_or_404, redirect
  5. from django.utils.translation import gettext as _
  6. from django.views import View
  7. from misago.conf import settings
  8. from misago.readtracker.dates import get_cutoff_date
  9. from misago.threads.permissions import exclude_invisible_posts
  10. from misago.threads.viewmodels import ForumThread, PrivateThread
  11. class GotoView(View):
  12. thread = None
  13. def get(self, request, pk, slug, **kwargs):
  14. thread = self.get_thread(request, pk, slug).unwrap()
  15. self.test_permissions(request, thread)
  16. posts_queryset = exclude_invisible_posts(
  17. request.user_acl, thread.category, thread.post_set
  18. )
  19. target_post = self.get_target_post(
  20. request.user, thread, posts_queryset.order_by('id'), **kwargs
  21. )
  22. target_page = self.compute_post_page(target_post, posts_queryset)
  23. return self.get_redirect(thread, target_post, target_page)
  24. def get_thread(self, request, pk, slug):
  25. return self.thread(request, pk, slug)
  26. def test_permissions(self, request, thread):
  27. pass
  28. def get_target_post(self, user, thread, posts_queryset):
  29. raise NotImplementedError("goto views should define their own get_target_post method")
  30. def compute_post_page(self, target_post, posts_queryset):
  31. # filter out events, order queryset
  32. posts_queryset = posts_queryset.filter(is_event=False).order_by('id')
  33. thread_length = posts_queryset.count()
  34. # is target an event?
  35. if target_post.is_event:
  36. target_event = target_post
  37. previous_posts = posts_queryset.filter(id__lt=target_event.id)
  38. else:
  39. previous_posts = posts_queryset.filter(id__lte=target_post.id)
  40. post_position = previous_posts.count()
  41. per_page = settings.MISAGO_POSTS_PER_PAGE - 1
  42. orphans = settings.MISAGO_POSTS_TAIL
  43. if orphans:
  44. orphans += 1
  45. hits = max(1, thread_length - orphans)
  46. thread_pages = int(ceil(hits / float(per_page)))
  47. if post_position >= thread_pages * per_page:
  48. return thread_pages
  49. return int(ceil(float(post_position) / (per_page)))
  50. def get_redirect(self, thread, target_post, target_page):
  51. thread_url = thread.thread_type.get_thread_absolute_url(thread, target_page)
  52. return redirect('%s#post-%s' % (thread_url, target_post.pk))
  53. class ThreadGotoPostView(GotoView):
  54. thread = ForumThread
  55. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  56. return get_object_or_404(posts_queryset, pk=kwargs['post'])
  57. class ThreadGotoLastView(GotoView):
  58. thread = ForumThread
  59. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  60. return posts_queryset.order_by('id').last()
  61. class GetFirstUnreadPostMixin(object):
  62. def get_first_unread_post(self, user, posts_queryset):
  63. if user.is_authenticated:
  64. expired_posts = Q(posted_on__lt=get_cutoff_date(user))
  65. read_posts = Q(id__in=user.postread_set.values('post'))
  66. first_unread = posts_queryset.exclude(
  67. expired_posts | read_posts,
  68. ).order_by('id').first()
  69. if first_unread:
  70. return first_unread
  71. return posts_queryset.order_by('id').last()
  72. class ThreadGotoNewView(GotoView, GetFirstUnreadPostMixin):
  73. thread = ForumThread
  74. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  75. return self.get_first_unread_post(user, posts_queryset)
  76. class ThreadGotoBestAnswerView(GotoView):
  77. thread = ForumThread
  78. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  79. return thread.best_answer or thread.first_post
  80. class ThreadGotoUnapprovedView(GotoView):
  81. thread = ForumThread
  82. def test_permissions(self, request, thread):
  83. if not thread.acl['can_approve']:
  84. raise PermissionDenied(
  85. _(
  86. "You need permission to approve content to "
  87. "be able to go to first unapproved post."
  88. )
  89. )
  90. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  91. unapproved_post = posts_queryset.filter(
  92. is_unapproved=True,
  93. ).order_by('id').first()
  94. if unapproved_post:
  95. return unapproved_post
  96. else:
  97. return posts_queryset.order_by('id').last()
  98. class PrivateThreadGotoPostView(GotoView):
  99. thread = PrivateThread
  100. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  101. return get_object_or_404(posts_queryset, pk=kwargs['post'])
  102. class PrivateThreadGotoLastView(GotoView):
  103. thread = PrivateThread
  104. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  105. return posts_queryset.order_by('id').last()
  106. class PrivateThreadGotoNewView(GotoView, GetFirstUnreadPostMixin):
  107. thread = PrivateThread
  108. def get_target_post(self, user, thread, posts_queryset, **kwargs):
  109. return self.get_first_unread_post(user, posts_queryset)