merge.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from django.core.exceptions import PermissionDenied
  2. from rest_framework.response import Response
  3. from ....acl.objectacl import add_acl_to_obj
  4. from ...events import record_event
  5. from ...mergeconflict import MergeConflict
  6. from ...models import Thread
  7. from ...moderation import threads as moderation
  8. from ...permissions import allow_merge_thread
  9. from ...serializers import (
  10. MergeThreadSerializer,
  11. MergeThreadsSerializer,
  12. ThreadsListSerializer,
  13. )
  14. def thread_merge_endpoint(
  15. request, thread, viewmodel
  16. ): # pylint: disable=too-many-branches
  17. allow_merge_thread(request.user_acl, thread)
  18. serializer = MergeThreadSerializer(
  19. data=request.data,
  20. context={"request": request, "thread": thread, "viewmodel": viewmodel},
  21. )
  22. if not serializer.is_valid():
  23. if "other_thread" in serializer.errors:
  24. errors = serializer.errors["other_thread"]
  25. elif "best_answer" in serializer.errors:
  26. errors = serializer.errors["best_answer"]
  27. elif "best_answers" in serializer.errors:
  28. return Response(
  29. {"best_answers": serializer.errors["best_answers"]}, status=400
  30. )
  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. {
  70. "id": other_thread.pk,
  71. "title": other_thread.title,
  72. "url": other_thread.get_absolute_url(),
  73. }
  74. )
  75. def threads_merge_endpoint(request):
  76. serializer = MergeThreadsSerializer(
  77. data=request.data,
  78. context={"settings": request.settings, "user_acl": request.user_acl},
  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. if "non_field_errors" in serializer.errors:
  85. errors = {"detail": serializer.errors["non_field_errors"][0]}
  86. return Response(errors, status=403)
  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_acl, thread)
  93. except PermissionDenied as e:
  94. invalid_threads.append(
  95. {"id": thread.pk, "title": thread.title, "errors": [str(e)]}
  96. )
  97. if invalid_threads:
  98. return Response(invalid_threads, status=403)
  99. # handle merge conflict
  100. merge_conflict = MergeConflict(serializer.validated_data, threads)
  101. merge_conflict.is_valid(raise_exception=True)
  102. new_thread = merge_threads(
  103. request, serializer.validated_data, threads, merge_conflict
  104. )
  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, new_thread, "merged", {"merged_thread": thread.title}, commit=False
  133. )
  134. new_thread.synchronize()
  135. new_thread.save()
  136. if validated_data.get("weight") == Thread.WEIGHT_GLOBAL:
  137. moderation.pin_thread_globally(request, new_thread)
  138. elif validated_data.get("weight"):
  139. moderation.pin_thread_locally(request, new_thread)
  140. if validated_data.get("is_hidden", False):
  141. moderation.hide_thread(request, new_thread)
  142. if validated_data.get("is_closed", False):
  143. moderation.close_thread(request, new_thread)
  144. if new_thread.category not in categories:
  145. categories.append(new_thread.category)
  146. for category in categories:
  147. category.synchronize()
  148. category.save()
  149. # set extra attrs on thread for UI
  150. new_thread.is_read = False
  151. new_thread.subscription = None
  152. add_acl_to_obj(request.user_acl, new_thread)
  153. return new_thread