merge.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.mergeconflict import MergeConflict
  8. from misago.threads.models import Thread
  9. from misago.threads.moderation import threads as moderation
  10. from misago.threads.permissions import allow_merge_thread
  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. elif 'polls' in serializer.errors:
  29. return Response({'polls': serializer.errors['polls']}, status=400)
  30. elif 'best_answer' in serializer.errors:
  31. errors = serializer.errors['best_answer']
  32. elif 'best_answers' in serializer.errors:
  33. return Response({'best_answers': serializer.errors['best_answers']}, status=400)
  34. else:
  35. errors = list(serializer.errors.values())[0]
  36. return Response({'detail': errors[0]}, status=400)
  37. # merge conflict
  38. other_thread = serializer.validated_data['other_thread']
  39. poll = serializer.validated_data.get('poll')
  40. if 'poll' in serializer.merge_conflict:
  41. if poll and poll.thread_id != other_thread.id:
  42. other_thread.poll.delete()
  43. poll.move(other_thread)
  44. elif not poll:
  45. other_thread.poll.delete()
  46. elif poll:
  47. poll.move(other_thread)
  48. best_answer = serializer.validated_data.get('best_answer')
  49. if 'best_answer' in serializer.merge_conflict and not best_answer:
  50. other_thread.clear_best_answer()
  51. if best_answer and best_answer != other_thread:
  52. other_thread.best_answer_id = thread.best_answer_id
  53. other_thread.best_answer_is_protected = thread.best_answer_is_protected
  54. other_thread.best_answer_marked_on = thread.best_answer_marked_on
  55. other_thread.best_answer_marked_by_id = thread.best_answer_marked_by_id
  56. other_thread.best_answer_marked_by_name = thread.best_answer_marked_by_name
  57. other_thread.best_answer_marked_by_slug = thread.best_answer_marked_by_slug
  58. # merge thread contents
  59. moderation.merge_thread(request, other_thread, thread)
  60. other_thread.synchronize()
  61. other_thread.save()
  62. other_thread.category.synchronize()
  63. other_thread.category.save()
  64. if thread.category != other_thread.category:
  65. thread.category.synchronize()
  66. thread.category.save()
  67. return Response({
  68. 'id': other_thread.pk,
  69. 'title': other_thread.title,
  70. 'url': other_thread.get_absolute_url(),
  71. })
  72. def threads_merge_endpoint(request):
  73. serializer = MergeThreadsSerializer(
  74. data=request.data,
  75. context={
  76. 'user': request.user
  77. },
  78. )
  79. if not serializer.is_valid():
  80. if 'threads' in serializer.errors:
  81. errors = {'detail': serializer.errors['threads'][0]}
  82. return Response(errors, status=403)
  83. elif 'non_field_errors' in serializer.errors:
  84. errors = {'detail': serializer.errors['non_field_errors'][0]}
  85. return Response(errors, status=403)
  86. else:
  87. return Response(serializer.errors, status=400)
  88. threads = serializer.validated_data['threads']
  89. invalid_threads = []
  90. for thread in threads:
  91. try:
  92. allow_merge_thread(request.user, thread)
  93. except PermissionDenied as e:
  94. invalid_threads.append({
  95. 'id': thread.pk,
  96. 'title': thread.title,
  97. 'errors': [text_type(e)]
  98. })
  99. if invalid_threads:
  100. return Response(invalid_threads, status=403)
  101. polls_handler = PollMergeHandler(threads)
  102. if len(polls_handler.polls) == 1:
  103. poll = polls_handler.polls[0]
  104. elif polls_handler.is_merge_conflict():
  105. if 'poll' in request.data:
  106. polls_handler.set_resolution(request.data.get('poll'))
  107. if polls_handler.is_valid():
  108. poll = polls_handler.get_resolution()
  109. else:
  110. return Response({'detail': _("Invalid choice.")}, status=400)
  111. else:
  112. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  113. else:
  114. poll = None
  115. new_thread = merge_threads(request, serializer.validated_data, threads, poll)
  116. return Response(ThreadsListSerializer(new_thread).data)
  117. def merge_threads(request, validated_data, threads, poll):
  118. new_thread = Thread(
  119. category=validated_data['category'],
  120. started_on=threads[0].started_on,
  121. last_post_on=threads[0].last_post_on,
  122. )
  123. new_thread.set_title(validated_data['title'])
  124. new_thread.save()
  125. if poll:
  126. poll.move(new_thread)
  127. categories = []
  128. for thread in threads:
  129. categories.append(thread.category)
  130. new_thread.merge(thread)
  131. thread.delete()
  132. record_event(
  133. request,
  134. new_thread,
  135. 'merged',
  136. {
  137. 'merged_thread': thread.title,
  138. },
  139. commit=False,
  140. )
  141. new_thread.synchronize()
  142. new_thread.save()
  143. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  144. moderation.pin_thread_globally(request, new_thread)
  145. elif validated_data.get('weight'):
  146. moderation.pin_thread_locally(request, new_thread)
  147. if validated_data.get('is_hidden', False):
  148. moderation.hide_thread(request, new_thread)
  149. if validated_data.get('is_closed', False):
  150. moderation.close_thread(request, new_thread)
  151. if new_thread.category not in categories:
  152. categories.append(new_thread.category)
  153. for category in categories:
  154. category.synchronize()
  155. category.save()
  156. # set extra attrs on thread for UI
  157. new_thread.is_read = False
  158. new_thread.subscription = None
  159. add_acl(request.user, new_thread)
  160. return new_thread