threads.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from rest_framework import viewsets
  2. from rest_framework.decorators import detail_route, list_route
  3. from rest_framework.response import Response
  4. from django.core.exceptions import PermissionDenied
  5. from django.db import transaction
  6. from django.utils.translation import ugettext as _
  7. from misago.categories import PRIVATE_THREADS_ROOT_NAME, THREADS_ROOT_NAME
  8. from misago.core.shortcuts import get_int_or_404
  9. from misago.threads.models import Post, Thread
  10. from misago.threads.moderation import threads as moderation
  11. from misago.threads.permissions import allow_use_private_threads
  12. from misago.threads.viewmodels import ForumThread, PrivateThread
  13. from .postingendpoint import PostingEndpoint
  14. from .threadendpoints.editor import thread_start_editor
  15. from .threadendpoints.list import private_threads_list_endpoint, threads_list_endpoint
  16. from .threadendpoints.merge import thread_merge_endpoint, threads_merge_endpoint
  17. from .threadendpoints.patch import thread_patch_endpoint
  18. from .threadendpoints.read import read_private_threads, read_threads
  19. class ViewSet(viewsets.ViewSet):
  20. thread = None
  21. def get_thread(self, request, pk, read_aware=True, subscription_aware=True):
  22. return self.thread(
  23. request,
  24. get_int_or_404(pk),
  25. None,
  26. read_aware,
  27. subscription_aware,
  28. )
  29. def retrieve(self, request, pk):
  30. thread = self.get_thread(request, pk)
  31. return Response(thread.get_frontend_context())
  32. @transaction.atomic
  33. def partial_update(self, request, pk):
  34. request.user.lock()
  35. thread = self.get_thread(request, pk).unwrap()
  36. return thread_patch_endpoint(request, thread)
  37. @transaction.atomic
  38. def destroy(self, request, pk):
  39. request.user.lock()
  40. thread = self.get_thread(request, pk)
  41. if thread.acl.get('can_hide') == 2:
  42. moderation.delete_thread(request.user, thread)
  43. return Response({'detail': 'ok'})
  44. else:
  45. raise PermissionDenied(_("You don't have permission to delete this thread."))
  46. class ThreadViewSet(ViewSet):
  47. thread = ForumThread
  48. def list(self, request):
  49. return threads_list_endpoint(request)
  50. @transaction.atomic
  51. def create(self, request):
  52. # Initialize empty instances for new thread
  53. thread = Thread()
  54. post = Post(thread=thread)
  55. # Put them through posting pipeline
  56. posting = PostingEndpoint(
  57. request,
  58. PostingEndpoint.START,
  59. tree_name=THREADS_ROOT_NAME,
  60. thread=thread,
  61. post=post,
  62. )
  63. if posting.is_valid():
  64. posting.save()
  65. return Response({
  66. 'id': thread.pk,
  67. 'title': thread.title,
  68. 'url': thread.get_absolute_url(),
  69. })
  70. else:
  71. return Response(posting.errors, status=400)
  72. @detail_route(methods=['post'], url_path='merge')
  73. @transaction.atomic
  74. def thread_merge(self, request, pk):
  75. thread = self.get_thread(request, pk).unwrap()
  76. return thread_merge_endpoint(request, thread, self.thread)
  77. @list_route(methods=['post'], url_path='merge')
  78. @transaction.atomic
  79. def threads_merge(self, request):
  80. return threads_merge_endpoint(request)
  81. @list_route(methods=['get'])
  82. def editor(self, request):
  83. return thread_start_editor(request)
  84. @list_route(methods=['post'])
  85. @transaction.atomic
  86. def read(self, request):
  87. read_threads(request.user, request.GET.get('category'))
  88. return Response({'detail': 'ok'})
  89. class PrivateThreadViewSet(ViewSet):
  90. thread = PrivateThread
  91. def list(self, request):
  92. return private_threads_list_endpoint(request)
  93. @transaction.atomic
  94. def create(self, request):
  95. allow_use_private_threads(request.user)
  96. if not request.user.acl_cache['can_start_private_threads']:
  97. raise PermissionDenied(_("You can't start private threads."))
  98. request.user.lock()
  99. # Initialize empty instances for new thread
  100. thread = Thread()
  101. post = Post(thread=thread)
  102. # Put them through posting pipeline
  103. posting = PostingEndpoint(
  104. request,
  105. PostingEndpoint.START,
  106. tree_name=PRIVATE_THREADS_ROOT_NAME,
  107. thread=thread,
  108. post=post,
  109. )
  110. if posting.is_valid():
  111. posting.save()
  112. return Response({
  113. 'id': thread.pk,
  114. 'title': thread.title,
  115. 'url': thread.get_absolute_url(),
  116. })
  117. else:
  118. return Response(posting.errors, status=400)
  119. @list_route(methods=['post'])
  120. @transaction.atomic
  121. def read(self, request):
  122. allow_use_private_threads(request.user)
  123. read_private_threads(request.user)
  124. return Response({'detail': 'ok'})