threads.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from django.core.exceptions import PermissionDenied
  2. from django.db import transaction
  3. from django.utils.translation import gettext as _
  4. from rest_framework import viewsets
  5. from rest_framework.decorators import detail_route, list_route
  6. from rest_framework.response import Response
  7. from misago.categories.models import THREADS_ROOT_NAME
  8. from misago.core.shortcuts import get_int_or_404
  9. from ..models import Post, Thread
  10. from ..moderation import threads as moderation
  11. from ..viewmodels.thread import ForumThread
  12. from .postingendpoint import PostingEndpoint
  13. from .threadendpoints.editor import thread_start_editor
  14. from .threadendpoints.list import threads_list_endpoint
  15. from .threadendpoints.merge import thread_merge_endpoint, threads_merge_endpoint
  16. from .threadendpoints.patch import thread_patch_endpoint
  17. class ViewSet(viewsets.ViewSet):
  18. thread = None
  19. TREE_ID = None
  20. def get_thread(self, request, pk, read_aware=True, subscription_aware=True, select_for_update=False):
  21. return self.thread(
  22. request,
  23. get_int_or_404(pk),
  24. None,
  25. read_aware,
  26. subscription_aware,
  27. select_for_update
  28. )
  29. def get_thread_for_update(self, request, pk):
  30. return self.get_thread(
  31. request, pk,
  32. read_aware=False,
  33. subscription_aware=False,
  34. select_for_update=True
  35. )
  36. def list(self, request):
  37. return threads_list_endpoint(request)
  38. def retrieve(self, request, pk):
  39. thread = self.get_thread(request, pk)
  40. return Response(thread.get_frontend_context())
  41. @transaction.atomic
  42. def partial_update(self, request, pk):
  43. thread = self.get_thread_for_update(request, pk).model
  44. return thread_patch_endpoint(request, thread)
  45. @transaction.atomic
  46. def destroy(self, request, pk):
  47. thread = self.get_thread_for_update(request, pk).model
  48. if thread.acl.get('can_hide') == 2:
  49. moderation.delete_thread(request.user, thread)
  50. return Response({'detail': 'ok'})
  51. else:
  52. raise PermissionDenied(_("You don't have permission to delete this thread."))
  53. class ThreadViewSet(ViewSet):
  54. thread = ForumThread
  55. def create(self, request):
  56. # Initialize empty instances for new thread
  57. thread = Thread()
  58. post = Post(thread=thread)
  59. # Put them through posting pipeline
  60. posting = PostingEndpoint(
  61. request,
  62. PostingEndpoint.START,
  63. tree_name=THREADS_ROOT_NAME,
  64. thread=thread,
  65. post=post
  66. )
  67. if posting.is_valid():
  68. posting.save()
  69. return Response({
  70. 'id': thread.pk,
  71. 'title': thread.title,
  72. 'url': thread.get_absolute_url()
  73. })
  74. else:
  75. return Response(posting.errors, status=400)
  76. @detail_route(methods=['post'], url_path='merge')
  77. @transaction.atomic
  78. def thread_merge(self, request, pk):
  79. thread = self.get_thread_for_update(request, pk).model
  80. return thread_merge_endpoint(request, thread, self.thread)
  81. @list_route(methods=['post'], url_path='merge')
  82. @transaction.atomic
  83. def threads_merge(self, request):
  84. return threads_merge_endpoint(request)
  85. @list_route(methods=['get'])
  86. def editor(self, request):
  87. return thread_start_editor(request)