merge.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(request, thread, viewmodel):
  15. allow_merge_thread(request.user_acl, thread)
  16. serializer = MergeThreadSerializer(
  17. data=request.data,
  18. context={"request": request, "thread": thread, "viewmodel": viewmodel},
  19. )
  20. if not serializer.is_valid():
  21. if "other_thread" in serializer.errors:
  22. errors = serializer.errors["other_thread"]
  23. elif "best_answer" in serializer.errors:
  24. errors = serializer.errors["best_answer"]
  25. elif "best_answers" in serializer.errors:
  26. return Response(
  27. {"best_answers": serializer.errors["best_answers"]}, status=400
  28. )
  29. elif "poll" in serializer.errors:
  30. errors = serializer.errors["poll"]
  31. elif "polls" in serializer.errors:
  32. return Response({"polls": serializer.errors["polls"]}, status=400)
  33. else:
  34. errors = list(serializer.errors.values())[0]
  35. return Response({"detail": errors[0]}, status=400)
  36. # merge conflict
  37. other_thread = serializer.validated_data["other_thread"]
  38. best_answer = serializer.validated_data.get("best_answer")
  39. if "best_answer" in serializer.merge_conflict and not best_answer:
  40. other_thread.clear_best_answer()
  41. if best_answer and best_answer != other_thread:
  42. other_thread.best_answer_id = thread.best_answer_id
  43. other_thread.best_answer_is_protected = thread.best_answer_is_protected
  44. other_thread.best_answer_marked_on = thread.best_answer_marked_on
  45. other_thread.best_answer_marked_by_id = thread.best_answer_marked_by_id
  46. other_thread.best_answer_marked_by_name = thread.best_answer_marked_by_name
  47. other_thread.best_answer_marked_by_slug = thread.best_answer_marked_by_slug
  48. poll = serializer.validated_data.get("poll")
  49. if "poll" in serializer.merge_conflict:
  50. if poll and poll.thread_id != other_thread.id:
  51. other_thread.poll.delete()
  52. poll.move(other_thread)
  53. elif not poll:
  54. other_thread.poll.delete()
  55. elif poll:
  56. poll.move(other_thread)
  57. # merge thread contents
  58. moderation.merge_thread(request, other_thread, thread)
  59. other_thread.synchronize()
  60. other_thread.save()
  61. other_thread.category.synchronize()
  62. other_thread.category.save()
  63. if thread.category != other_thread.category:
  64. thread.category.synchronize()
  65. thread.category.save()
  66. return Response(
  67. {
  68. "id": other_thread.pk,
  69. "title": other_thread.title,
  70. "url": other_thread.get_absolute_url(),
  71. }
  72. )
  73. def threads_merge_endpoint(request):
  74. serializer = MergeThreadsSerializer(
  75. data=request.data,
  76. context={"settings": request.settings, "user_acl": request.user_acl},
  77. )
  78. if not serializer.is_valid():
  79. if "threads" in serializer.errors:
  80. errors = {"detail": serializer.errors["threads"][0]}
  81. return Response(errors, status=403)
  82. if "non_field_errors" in serializer.errors:
  83. errors = {"detail": serializer.errors["non_field_errors"][0]}
  84. return Response(errors, status=403)
  85. return Response(serializer.errors, status=400)
  86. threads = serializer.validated_data["threads"]
  87. invalid_threads = []
  88. for thread in threads:
  89. try:
  90. allow_merge_thread(request.user_acl, thread)
  91. except PermissionDenied as e:
  92. invalid_threads.append(
  93. {"id": thread.pk, "title": thread.title, "errors": [str(e)]}
  94. )
  95. if invalid_threads:
  96. return Response(invalid_threads, status=403)
  97. # handle merge conflict
  98. merge_conflict = MergeConflict(serializer.validated_data, threads)
  99. merge_conflict.is_valid(raise_exception=True)
  100. new_thread = merge_threads(
  101. request, serializer.validated_data, threads, merge_conflict
  102. )
  103. return Response(ThreadsListSerializer(new_thread).data)
  104. def merge_threads(request, validated_data, threads, merge_conflict):
  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. resolution = merge_conflict.get_resolution()
  113. best_answer = resolution.get("best_answer")
  114. if best_answer:
  115. new_thread.best_answer_id = best_answer.best_answer_id
  116. new_thread.best_answer_is_protected = best_answer.best_answer_is_protected
  117. new_thread.best_answer_marked_on = best_answer.best_answer_marked_on
  118. new_thread.best_answer_marked_by_id = best_answer.best_answer_marked_by_id
  119. new_thread.best_answer_marked_by_name = best_answer.best_answer_marked_by_name
  120. new_thread.best_answer_marked_by_slug = best_answer.best_answer_marked_by_slug
  121. poll = resolution.get("poll")
  122. if poll:
  123. poll.move(new_thread)
  124. categories = []
  125. for thread in threads:
  126. categories.append(thread.category)
  127. new_thread.merge(thread)
  128. thread.delete()
  129. record_event(
  130. request, new_thread, "merged", {"merged_thread": thread.title}, commit=False
  131. )
  132. new_thread.synchronize()
  133. new_thread.save()
  134. if validated_data.get("weight") == Thread.WEIGHT_GLOBAL:
  135. moderation.pin_thread_globally(request, new_thread)
  136. elif validated_data.get("weight"):
  137. moderation.pin_thread_locally(request, new_thread)
  138. if validated_data.get("is_hidden", False):
  139. moderation.hide_thread(request, new_thread)
  140. if validated_data.get("is_closed", False):
  141. moderation.close_thread(request, new_thread)
  142. if new_thread.category not in categories:
  143. categories.append(new_thread.category)
  144. for category in categories:
  145. category.synchronize()
  146. category.save()
  147. # set extra attrs on thread for UI
  148. new_thread.is_read = False
  149. new_thread.subscription = None
  150. add_acl_to_obj(request.user_acl, new_thread)
  151. return new_thread