threads.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.acl import add_acl
  8. from misago.categories.models import THREADS_ROOT_NAME, Category
  9. from misago.categories.permissions import allow_browse_category, allow_see_category
  10. from misago.core.shortcuts import get_int_or_404, get_object_or_404
  11. from misago.readtracker.categoriestracker import read_category
  12. from ..models import Post, Subscription, Thread
  13. from ..moderation import threads as moderation
  14. from ..permissions.threads import can_start_thread
  15. from ..subscriptions import make_subscription_aware
  16. from ..threadtypes import trees_map
  17. from ..viewmodels.thread import ForumThread
  18. from .postingendpoint import PostingEndpoint
  19. from .threadendpoints.editor import thread_start_editor
  20. from .threadendpoints.list import threads_list_endpoint
  21. from .threadendpoints.merge import thread_merge_endpoint, threads_merge_endpoint
  22. from .threadendpoints.patch import thread_patch_endpoint
  23. class ViewSet(viewsets.ViewSet):
  24. thread = None
  25. TREE_ID = None
  26. def get_thread(self, request, pk, read_aware=True, subscription_aware=True, select_for_update=False):
  27. return self.thread(
  28. request,
  29. get_int_or_404(pk),
  30. None,
  31. read_aware,
  32. subscription_aware,
  33. select_for_update
  34. )
  35. def get_thread_for_update(self, request, pk):
  36. return self.get_thread(
  37. request, pk,
  38. read_aware=False,
  39. subscription_aware=False,
  40. select_for_update=True
  41. )
  42. def list(self, request):
  43. return threads_list_endpoint(request)
  44. def retrieve(self, request, pk):
  45. thread = self.get_thread(request, pk)
  46. return Response(thread.get_frontend_context())
  47. @transaction.atomic
  48. def partial_update(self, request, pk):
  49. thread = self.get_thread_for_update(request, pk).thread
  50. return thread_patch_endpoint(request, thread)
  51. @transaction.atomic
  52. def destroy(self, request, pk):
  53. thread = self.get_thread_for_update(request, pk).thread
  54. if thread.acl.get('can_hide') == 2:
  55. moderation.delete_thread(request.user, thread)
  56. return Response({'detail': 'ok'})
  57. else:
  58. raise PermissionDenied(_("You don't have permission to delete this thread."))
  59. class ThreadViewSet(ViewSet):
  60. thread = ForumThread
  61. def create(self, request):
  62. # Initialize empty instances for new thread
  63. thread = Thread()
  64. post = Post(thread=thread)
  65. # Put them through posting pipeline
  66. posting = PostingEndpoint(
  67. request,
  68. PostingEndpoint.START,
  69. tree_name=THREADS_ROOT_NAME,
  70. thread=thread,
  71. post=post
  72. )
  73. if posting.is_valid():
  74. posting.save()
  75. return Response({
  76. 'id': thread.pk,
  77. 'title': thread.title,
  78. 'url': thread.get_absolute_url()
  79. })
  80. else:
  81. return Response(posting.errors, status=400)
  82. @detail_route(methods=['post'], url_path='merge')
  83. @transaction.atomic
  84. def thread_merge(self, request, pk):
  85. thread = self.get_thread_for_update(request, pk).thread
  86. return thread_merge_endpoint(request, thread, self.thread)
  87. @list_route(methods=['post'], url_path='merge')
  88. @transaction.atomic
  89. def threads_merge(self, request):
  90. return threads_merge_endpoint(request)
  91. @list_route(methods=['post'])
  92. def read(self, request):
  93. if request.query_params.get('category'):
  94. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  95. category_id = get_int_or_404(request.query_params.get('category'))
  96. category = get_object_or_404(Category,
  97. id=category_id,
  98. tree_id=threads_tree_id,
  99. )
  100. allow_see_category(request.user, category)
  101. allow_browse_category(request.user, category)
  102. else:
  103. category = Category.objects.root_category()
  104. read_category(request.user, category)
  105. return Response({'detail': 'ok'})
  106. @list_route(methods=['get'])
  107. def editor(self, request):
  108. return thread_start_editor(request)