goto.py 4.5 KB

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