merge.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 import moderation
  8. from misago.threads.events import record_event
  9. from misago.threads.models import Thread
  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. serializer.is_valid(raise_exception=True)
  24. # merge conflict
  25. other_thread = serializer.validated_data['other_thread']
  26. best_answer = serializer.validated_data.get('best_answer')
  27. if 'best_answer' in serializer.merge_conflict and not best_answer:
  28. other_thread.clear_best_answer()
  29. if best_answer and best_answer != other_thread:
  30. other_thread.best_answer_id = thread.best_answer_id
  31. other_thread.best_answer_is_protected = thread.best_answer_is_protected
  32. other_thread.best_answer_marked_on = thread.best_answer_marked_on
  33. other_thread.best_answer_marked_by_id = thread.best_answer_marked_by_id
  34. other_thread.best_answer_marked_by_name = thread.best_answer_marked_by_name
  35. other_thread.best_answer_marked_by_slug = thread.best_answer_marked_by_slug
  36. poll = serializer.validated_data.get('poll')
  37. if 'poll' in serializer.merge_conflict:
  38. if poll and poll.thread_id != other_thread.id:
  39. other_thread.poll.delete()
  40. poll.move(other_thread)
  41. elif not poll:
  42. other_thread.poll.delete()
  43. elif poll:
  44. poll.move(other_thread)
  45. # merge thread contents
  46. moderation.merge_thread(request, other_thread, thread)
  47. other_thread.synchronize()
  48. other_thread.save()
  49. other_thread.category.synchronize()
  50. other_thread.category.save()
  51. if thread.category != other_thread.category:
  52. thread.category.synchronize()
  53. thread.category.save()
  54. return Response({
  55. 'id': other_thread.pk,
  56. 'title': other_thread.title,
  57. 'url': other_thread.get_absolute_url(),
  58. })
  59. def threads_merge_endpoint(request):
  60. serializer = MergeThreadsSerializer(
  61. data=request.data,
  62. context={
  63. 'user': request.user
  64. },
  65. )
  66. serializer.is_valid(raise_exception=True)
  67. threads = serializer.validated_data['threads']
  68. data = serializer.validated_data
  69. threads = data['threads']
  70. new_thread = Thread(
  71. category=data['category'],
  72. started_on=threads[0].started_on,
  73. last_post_on=threads[0].last_post_on,
  74. )
  75. new_thread.set_title(data['title'])
  76. new_thread.save()
  77. # handle merge conflict
  78. best_answer = data.get('best_answer')
  79. if best_answer:
  80. new_thread.best_answer_id = best_answer.best_answer_id
  81. new_thread.best_answer_is_protected = best_answer.best_answer_is_protected
  82. new_thread.best_answer_marked_on = best_answer.best_answer_marked_on
  83. new_thread.best_answer_marked_by_id = best_answer.best_answer_marked_by_id
  84. new_thread.best_answer_marked_by_name = best_answer.best_answer_marked_by_name
  85. new_thread.best_answer_marked_by_slug = best_answer.best_answer_marked_by_slug
  86. poll = data.get('poll')
  87. if poll:
  88. poll.move(new_thread)
  89. categories = []
  90. for thread in threads:
  91. categories.append(thread.category)
  92. new_thread.merge(thread)
  93. thread.delete()
  94. record_event(
  95. request,
  96. new_thread,
  97. 'merged',
  98. {
  99. 'merged_thread': thread.title
  100. },
  101. commit=False,
  102. )
  103. new_thread.synchronize()
  104. new_thread.save()
  105. if data.get('weight') == Thread.WEIGHT_GLOBAL:
  106. moderation.pin_thread_globally(request, new_thread)
  107. elif data.get('weight'):
  108. moderation.pin_thread_locally(request, new_thread)
  109. if data.get('is_hidden', False):
  110. moderation.hide_thread(request, new_thread)
  111. if data.get('is_closed', False):
  112. moderation.close_thread(request, new_thread)
  113. if new_thread.category not in categories:
  114. categories.append(new_thread.category)
  115. for category in categories:
  116. category.synchronize()
  117. category.save()
  118. # set extra attrs on thread for UI
  119. new_thread.is_read = False
  120. new_thread.subscription = None
  121. add_acl(request.user, new_thread)
  122. return Response(ThreadsListSerializer(new_thread).data)