merge.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from rest_framework.exceptions import ValidationError
  2. from rest_framework.response import Response
  3. from django.core.exceptions import PermissionDenied
  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 'best_answer' in serializer.errors:
  27. errors = serializer.errors['best_answer']
  28. elif 'best_answers' in serializer.errors:
  29. return Response({'best_answers': serializer.errors['best_answers']}, status=400)
  30. elif 'poll' in serializer.errors:
  31. errors = serializer.errors['poll']
  32. elif 'polls' in serializer.errors:
  33. return Response({'polls': serializer.errors['polls']}, 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. best_answer = serializer.validated_data.get('best_answer')
  40. if 'best_answer' in serializer.merge_conflict and not best_answer:
  41. other_thread.clear_best_answer()
  42. if best_answer and best_answer != other_thread:
  43. other_thread.best_answer_id = thread.best_answer_id
  44. other_thread.best_answer_is_protected = thread.best_answer_is_protected
  45. other_thread.best_answer_marked_on = thread.best_answer_marked_on
  46. other_thread.best_answer_marked_by_id = thread.best_answer_marked_by_id
  47. other_thread.best_answer_marked_by_name = thread.best_answer_marked_by_name
  48. other_thread.best_answer_marked_by_slug = thread.best_answer_marked_by_slug
  49. poll = serializer.validated_data.get('poll')
  50. if 'poll' in serializer.merge_conflict:
  51. if poll and poll.thread_id != other_thread.id:
  52. other_thread.poll.delete()
  53. poll.move(other_thread)
  54. elif not poll:
  55. other_thread.poll.delete()
  56. elif poll:
  57. poll.move(other_thread)
  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': [str(e)]
  98. })
  99. if invalid_threads:
  100. return Response(invalid_threads, status=403)
  101. # handle merge conflict
  102. merge_conflict = MergeConflict(serializer.validated_data, threads)
  103. merge_conflict.is_valid(raise_exception=True)
  104. new_thread = merge_threads(request, serializer.validated_data, threads, merge_conflict)
  105. return Response(ThreadsListSerializer(new_thread).data)
  106. def merge_threads(request, validated_data, threads, merge_conflict):
  107. new_thread = Thread(
  108. category=validated_data['category'],
  109. started_on=threads[0].started_on,
  110. last_post_on=threads[0].last_post_on,
  111. )
  112. new_thread.set_title(validated_data['title'])
  113. new_thread.save()
  114. resolution = merge_conflict.get_resolution()
  115. best_answer = resolution.get('best_answer')
  116. if best_answer:
  117. new_thread.best_answer_id = best_answer.best_answer_id
  118. new_thread.best_answer_is_protected = best_answer.best_answer_is_protected
  119. new_thread.best_answer_marked_on = best_answer.best_answer_marked_on
  120. new_thread.best_answer_marked_by_id = best_answer.best_answer_marked_by_id
  121. new_thread.best_answer_marked_by_name = best_answer.best_answer_marked_by_name
  122. new_thread.best_answer_marked_by_slug = best_answer.best_answer_marked_by_slug
  123. poll = resolution.get('poll')
  124. if poll:
  125. poll.move(new_thread)
  126. categories = []
  127. for thread in threads:
  128. categories.append(thread.category)
  129. new_thread.merge(thread)
  130. thread.delete()
  131. record_event(
  132. request,
  133. new_thread,
  134. 'merged',
  135. {
  136. 'merged_thread': thread.title,
  137. },
  138. commit=False,
  139. )
  140. new_thread.synchronize()
  141. new_thread.save()
  142. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  143. moderation.pin_thread_globally(request, new_thread)
  144. elif validated_data.get('weight'):
  145. moderation.pin_thread_locally(request, new_thread)
  146. if validated_data.get('is_hidden', False):
  147. moderation.hide_thread(request, new_thread)
  148. if validated_data.get('is_closed', False):
  149. moderation.close_thread(request, new_thread)
  150. if new_thread.category not in categories:
  151. categories.append(new_thread.category)
  152. for category in categories:
  153. category.synchronize()
  154. category.save()
  155. # set extra attrs on thread for UI
  156. new_thread.is_read = False
  157. new_thread.subscription = None
  158. add_acl(request.user, new_thread)
  159. return new_thread