merge.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.six import text_type
  4. from django.utils.translation import ugettext as _
  5. from misago.acl import add_acl
  6. from misago.threads.events import record_event
  7. from misago.threads.models import Thread
  8. from misago.threads.moderation import threads as moderation
  9. from misago.threads.permissions import allow_merge_thread
  10. from misago.threads.pollmergehandler import PollMergeHandler
  11. from misago.threads.serializers import (
  12. MergeThreadSerializer, MergeThreadsSerializer, ThreadsListSerializer)
  13. def thread_merge_endpoint(request, thread, viewmodel):
  14. allow_merge_thread(request.user, thread)
  15. serializer = MergeThreadSerializer(
  16. data=request.data,
  17. context={
  18. 'request': request,
  19. 'thread': thread,
  20. 'viewmodel': viewmodel,
  21. },
  22. )
  23. if not serializer.is_valid():
  24. if 'other_thread' in serializer.errors:
  25. errors = serializer.errors['other_thread']
  26. elif 'poll' in serializer.errors:
  27. errors = serializer.errors['poll']
  28. else:
  29. errors = list(serializer.errors.values())[0]
  30. return Response({'detail': errors[0]}, status=400)
  31. # interrupt merge with request for poll resolution?
  32. if serializer.validated_data.get('polls'):
  33. return Response({'polls': serializer.validated_data['polls']}, status=400)
  34. # merge polls
  35. other_thread = serializer.validated_data['other_thread']
  36. poll = serializer.validated_data.get('poll')
  37. if len(serializer.polls_handler.polls) == 1:
  38. poll.move(other_thread)
  39. elif serializer.polls_handler.is_merge_conflict():
  40. if poll and poll.thread_id != other_thread.id:
  41. other_thread.poll.delete()
  42. poll.move(other_thread)
  43. elif not poll:
  44. other_thread.poll.delete()
  45. # merge thread contents
  46. moderation.merge_thread(request, other_thread, thread)
  47. other_thread.synchronize()
  48. other_thread.save()
  49. other_thread.category.synchronize()
  50. other_thread.category.save()
  51. if thread.category != other_thread.category:
  52. thread.category.synchronize()
  53. thread.category.save()
  54. return Response({
  55. 'id': other_thread.pk,
  56. 'title': other_thread.title,
  57. 'url': other_thread.get_absolute_url(),
  58. })
  59. def threads_merge_endpoint(request):
  60. serializer = MergeThreadsSerializer(
  61. data=request.data,
  62. context={
  63. 'user': request.user
  64. },
  65. )
  66. if not serializer.is_valid():
  67. if 'threads' in serializer.errors:
  68. errors = {'detail': serializer.errors['threads'][0]}
  69. return Response(errors, status=403)
  70. elif 'non_field_errors' in serializer.errors:
  71. errors = {'detail': serializer.errors['non_field_errors'][0]}
  72. return Response(errors, status=403)
  73. else:
  74. return Response(serializer.errors, status=400)
  75. threads = serializer.validated_data['threads']
  76. invalid_threads = []
  77. for thread in threads:
  78. try:
  79. allow_merge_thread(request.user, thread)
  80. except PermissionDenied as e:
  81. invalid_threads.append({
  82. 'id': thread.pk,
  83. 'title': thread.title,
  84. 'errors': [text_type(e)]
  85. })
  86. if invalid_threads:
  87. return Response(invalid_threads, status=403)
  88. polls_handler = PollMergeHandler(threads)
  89. if len(polls_handler.polls) == 1:
  90. poll = polls_handler.polls[0]
  91. elif polls_handler.is_merge_conflict():
  92. if 'poll' in request.data:
  93. polls_handler.set_resolution(request.data.get('poll'))
  94. if polls_handler.is_valid():
  95. poll = polls_handler.get_resolution()
  96. else:
  97. return Response({'detail': _("Invalid choice.")}, status=400)
  98. else:
  99. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  100. else:
  101. poll = None
  102. new_thread = merge_threads(request, serializer.validated_data, threads, poll)
  103. return Response(ThreadsListSerializer(new_thread).data)
  104. def merge_threads(request, validated_data, threads, poll):
  105. new_thread = Thread(
  106. category=validated_data['category'],
  107. started_on=threads[0].started_on,
  108. last_post_on=threads[0].last_post_on,
  109. )
  110. new_thread.set_title(validated_data['title'])
  111. new_thread.save()
  112. if poll:
  113. poll.move(new_thread)
  114. categories = []
  115. for thread in threads:
  116. categories.append(thread.category)
  117. new_thread.merge(thread)
  118. thread.delete()
  119. record_event(
  120. request,
  121. new_thread,
  122. 'merged',
  123. {
  124. 'merged_thread': thread.title,
  125. },
  126. commit=False,
  127. )
  128. new_thread.synchronize()
  129. new_thread.save()
  130. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  131. moderation.pin_thread_globally(request, new_thread)
  132. elif validated_data.get('weight'):
  133. moderation.pin_thread_locally(request, new_thread)
  134. if validated_data.get('is_hidden', False):
  135. moderation.hide_thread(request, new_thread)
  136. if validated_data.get('is_closed', False):
  137. moderation.close_thread(request, new_thread)
  138. if new_thread.category not in categories:
  139. categories.append(new_thread.category)
  140. for category in categories:
  141. category.synchronize()
  142. category.save()
  143. # set extra attrs on thread for UI
  144. new_thread.is_read = False
  145. new_thread.subscription = None
  146. add_acl(request.user, new_thread)
  147. return new_thread