merge.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import gettext as _
  3. from rest_framework.exceptions import ValidationError
  4. from rest_framework.response import Response
  5. from misago.acl.objectacl import add_acl_to_obj
  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,
  13. MergeThreadsSerializer,
  14. ThreadsListSerializer,
  15. )
  16. def thread_merge_endpoint(request, thread, viewmodel):
  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. 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_acl, thread)
  94. except PermissionDenied as e:
  95. invalid_threads.append(
  96. {"id": thread.pk, "title": thread.title, "errors": [str(e)]}
  97. )
  98. if invalid_threads:
  99. return Response(invalid_threads, status=403)
  100. # handle merge conflict
  101. merge_conflict = MergeConflict(serializer.validated_data, threads)
  102. merge_conflict.is_valid(raise_exception=True)
  103. new_thread = merge_threads(
  104. request, serializer.validated_data, threads, merge_conflict
  105. )
  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, new_thread, "merged", {"merged_thread": thread.title}, commit=False
  134. )
  135. new_thread.synchronize()
  136. new_thread.save()
  137. if validated_data.get("weight") == Thread.WEIGHT_GLOBAL:
  138. moderation.pin_thread_globally(request, new_thread)
  139. elif validated_data.get("weight"):
  140. moderation.pin_thread_locally(request, new_thread)
  141. if validated_data.get("is_hidden", False):
  142. moderation.hide_thread(request, new_thread)
  143. if validated_data.get("is_closed", False):
  144. moderation.close_thread(request, new_thread)
  145. if new_thread.category not in categories:
  146. categories.append(new_thread.category)
  147. for category in categories:
  148. category.synchronize()
  149. category.save()
  150. # set extra attrs on thread for UI
  151. new_thread.is_read = False
  152. new_thread.subscription = None
  153. add_acl_to_obj(request.user_acl, new_thread)
  154. return new_thread