merge.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.six import text_type
  5. from django.utils.translation import ugettext as _
  6. from misago.acl import add_acl
  7. from misago.threads.events import record_event
  8. from misago.threads.mergeconflict import MergeConflict
  9. from misago.threads.models import Thread
  10. from misago.threads.moderation import threads as moderation
  11. from misago.threads.permissions import allow_merge_thread
  12. from misago.threads.serializers import (
  13. MergeThreadSerializer, MergeThreadsSerializer, ThreadsListSerializer)
  14. def thread_merge_endpoint(request, thread, viewmodel):
  15. allow_merge_thread(request.user, thread)
  16. serializer = MergeThreadSerializer(
  17. data=request.data,
  18. context={
  19. 'request': request,
  20. 'thread': thread,
  21. 'viewmodel': viewmodel,
  22. },
  23. )
  24. if not serializer.is_valid():
  25. if 'other_thread' in serializer.errors:
  26. errors = serializer.errors['other_thread']
  27. elif 'best_answer' in serializer.errors:
  28. errors = serializer.errors['best_answer']
  29. elif 'best_answers' in serializer.errors:
  30. return Response({'best_answers': serializer.errors['best_answers']}, status=400)
  31. elif 'poll' in serializer.errors:
  32. errors = serializer.errors['poll']
  33. elif 'polls' in serializer.errors:
  34. return Response({'polls': serializer.errors['polls']}, status=400)
  35. else:
  36. errors = list(serializer.errors.values())[0]
  37. return Response({'detail': errors[0]}, status=400)
  38. # merge conflict
  39. other_thread = serializer.validated_data['other_thread']
  40. best_answer = serializer.validated_data.get('best_answer')
  41. if 'best_answer' in serializer.merge_conflict and not best_answer:
  42. other_thread.clear_best_answer()
  43. if best_answer and best_answer != other_thread:
  44. other_thread.best_answer_id = thread.best_answer_id
  45. other_thread.best_answer_is_protected = thread.best_answer_is_protected
  46. other_thread.best_answer_marked_on = thread.best_answer_marked_on
  47. other_thread.best_answer_marked_by_id = thread.best_answer_marked_by_id
  48. other_thread.best_answer_marked_by_name = thread.best_answer_marked_by_name
  49. other_thread.best_answer_marked_by_slug = thread.best_answer_marked_by_slug
  50. poll = serializer.validated_data.get('poll')
  51. if 'poll' in serializer.merge_conflict:
  52. if poll and poll.thread_id != other_thread.id:
  53. other_thread.poll.delete()
  54. poll.move(other_thread)
  55. elif not poll:
  56. other_thread.poll.delete()
  57. elif poll:
  58. poll.move(other_thread)
  59. # merge thread contents
  60. moderation.merge_thread(request, other_thread, thread)
  61. other_thread.synchronize()
  62. other_thread.save()
  63. other_thread.category.synchronize()
  64. other_thread.category.save()
  65. if thread.category != other_thread.category:
  66. thread.category.synchronize()
  67. thread.category.save()
  68. return Response({
  69. 'id': other_thread.pk,
  70. 'title': other_thread.title,
  71. 'url': other_thread.get_absolute_url(),
  72. })
  73. def threads_merge_endpoint(request):
  74. serializer = MergeThreadsSerializer(
  75. data=request.data,
  76. context={
  77. 'user': request.user
  78. },
  79. )
  80. if not serializer.is_valid():
  81. if 'threads' in serializer.errors:
  82. errors = {'detail': serializer.errors['threads'][0]}
  83. return Response(errors, status=403)
  84. elif 'non_field_errors' in serializer.errors:
  85. errors = {'detail': serializer.errors['non_field_errors'][0]}
  86. return Response(errors, status=403)
  87. else:
  88. return Response(serializer.errors, status=400)
  89. threads = serializer.validated_data['threads']
  90. invalid_threads = []
  91. for thread in threads:
  92. try:
  93. allow_merge_thread(request.user, thread)
  94. except PermissionDenied as e:
  95. invalid_threads.append({
  96. 'id': thread.pk,
  97. 'title': thread.title,
  98. 'errors': [text_type(e)]
  99. })
  100. if invalid_threads:
  101. return Response(invalid_threads, status=403)
  102. # handle merge conflict
  103. merge_conflict = MergeConflict(serializer.validated_data, threads)
  104. merge_conflict.is_valid(raise_exception=True)
  105. new_thread = merge_threads(request, serializer.validated_data, threads, merge_conflict)
  106. return Response(ThreadsListSerializer(new_thread).data)
  107. def merge_threads(request, validated_data, threads, merge_conflict):
  108. new_thread = Thread(
  109. category=validated_data['category'],
  110. started_on=threads[0].started_on,
  111. last_post_on=threads[0].last_post_on,
  112. )
  113. new_thread.set_title(validated_data['title'])
  114. new_thread.save()
  115. resolution = merge_conflict.get_resolution()
  116. best_answer = resolution.get('best_answer')
  117. if best_answer:
  118. new_thread.best_answer_id = best_answer.best_answer_id
  119. new_thread.best_answer_is_protected = best_answer.best_answer_is_protected
  120. new_thread.best_answer_marked_on = best_answer.best_answer_marked_on
  121. new_thread.best_answer_marked_by_id = best_answer.best_answer_marked_by_id
  122. new_thread.best_answer_marked_by_name = best_answer.best_answer_marked_by_name
  123. new_thread.best_answer_marked_by_slug = best_answer.best_answer_marked_by_slug
  124. poll = resolution.get('poll')
  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