threadposts.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import ugettext as _
  3. from rest_framework import viewsets
  4. from rest_framework.decorators import detail_route, list_route
  5. from rest_framework.response import Response
  6. from misago.core.shortcuts import get_int_or_404
  7. from ..permissions.threads import allow_edit_post, allow_reply_thread
  8. from ..viewmodels.post import ThreadPost
  9. from ..viewmodels.posts import ThreadPosts
  10. from ..viewmodels.thread import ForumThread
  11. class ViewSet(viewsets.ViewSet):
  12. thread = None
  13. posts = None
  14. def get_thread(self, request, pk):
  15. return self.thread(request, get_int_or_404(pk), read_aware=True, subscription_aware=True)
  16. def get_posts(self, request, thread, page):
  17. return self.posts(request, thread, page)
  18. def create(self, request, thread_pk):
  19. thread = self.thread(request, get_int_or_404(thread_pk))
  20. allow_reply_thread(request.user, thread.thread)
  21. def update(self, request, thread_pk, pk):
  22. thread = self.thread(request, get_int_or_404(thread_pk))
  23. post = ThreadPost(request, thread, get_int_or_404(pk)).post
  24. allow_edit_post(request.user, post)
  25. def list(self, request, thread_pk):
  26. page = get_int_or_404(request.query_params.get('page', 0))
  27. if page == 1:
  28. page = 0 # api allows explicit first page
  29. thread = self.get_thread(request, thread_pk)
  30. posts = self.get_posts(request, thread, page)
  31. data = thread.get_frontend_context()
  32. data['post_set'] = posts.get_frontend_context()
  33. return Response(data)
  34. @detail_route(methods=['get'], url_path='editor')
  35. def post_editor(self, request, thread_pk, pk):
  36. thread = self.thread(request, get_int_or_404(thread_pk))
  37. post = ThreadPost(request, thread, get_int_or_404(pk)).post
  38. allow_edit_post(request.user, post)
  39. return Response({
  40. 'id': post.pk,
  41. 'api': post.get_api_url(),
  42. 'post': post.original,
  43. 'can_protect': bool(thread.category.acl['can_protect_posts']),
  44. 'is_protected': post.is_protected,
  45. 'poster': post.poster_name
  46. })
  47. @list_route(methods=['get'], url_path='editor')
  48. def reply_editor(self, request, thread_pk):
  49. thread = self.thread(request, get_int_or_404(thread_pk))
  50. allow_reply_thread(request.user, thread.thread)
  51. if 'reply' in request.query_params:
  52. reply_to = ThreadPost(request, thread, get_int_or_404(request.query_params['reply'])).post
  53. if reply_to.is_hidden and not reply_to.acl['can_see_hidden']:
  54. raise PermissionDenied(_("You can't reply to hidden posts"))
  55. return Response({
  56. 'id': reply_to.pk,
  57. 'post': reply_to.original,
  58. 'poster': reply_to.poster_name
  59. })
  60. else:
  61. return Response({})
  62. class ThreadPostsViewSet(ViewSet):
  63. thread = ForumThread
  64. posts = ThreadPosts