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. def get_thread(self, request, pk, read_aware=True, subscription_aware=True, select_for_update=False):
  20. return self.thread(
  21. request,
  22. get_int_or_404(pk),
  23. None,
  24. read_aware,
  25. subscription_aware,
  26. select_for_update
  27. )
  28. def get_thread_for_update(self, request, pk):
  29. return self.get_thread(
  30. request, pk,
  31. read_aware=False,
  32. subscription_aware=False,
  33. select_for_update=True
  34. )
  35. def list(self, request):
  36. return threads_list_endpoint(request)
  37. def retrieve(self, request, pk):
  38. thread = self.get_thread(request, pk)
  39. return Response(thread.get_frontend_context())
  40. @transaction.atomic
  41. def partial_update(self, request, pk):
  42. thread = self.get_thread_for_update(request, pk).model
  43. return thread_patch_endpoint(request, thread)
  44. @transaction.atomic
  45. def destroy(self, request, pk):
  46. thread = self.get_thread_for_update(request, pk).model
  47. if thread.acl.get('can_hide') == 2:
  48. moderation.delete_thread(request.user, thread)
  49. return Response({'detail': 'ok'})
  50. else:
  51. raise PermissionDenied(_("You don't have permission to delete this thread."))
  52. class ThreadViewSet(ViewSet):
  53. thread = ForumThread
  54. @transaction.atomic
  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)