merge.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.six import text_type
  4. from django.utils.translation import ugettext as _
  5. from misago.acl import add_acl
  6. from misago.threads.events import record_event
  7. from misago.threads.models import Thread
  8. from misago.threads.moderation import threads as moderation
  9. from misago.threads.permissions import allow_merge_thread
  10. from misago.threads.pollmergehandler import PollMergeHandler
  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. if not serializer.is_valid():
  24. return Response(serializer.errors, status=400)
  25. # interrupt merge with request for poll resolution?
  26. if serializer.validated_data.get('polls'):
  27. return Response({'polls': serializer.validated_data['polls']}, status=400)
  28. # merge polls
  29. other_thread = serializer.validated_data['other_thread']
  30. poll = serializer.validated_data.get('poll')
  31. if len(serializer.polls_handler.polls) == 1:
  32. poll.move(other_thread)
  33. elif serializer.polls_handler.is_merge_conflict():
  34. if poll and poll.thread_id != other_thread.id:
  35. other_thread.poll.delete()
  36. poll.move(other_thread)
  37. elif not poll:
  38. other_thread.poll.delete()
  39. # merge thread contents
  40. moderation.merge_thread(request, other_thread, thread)
  41. other_thread.synchronize()
  42. other_thread.save()
  43. other_thread.category.synchronize()
  44. other_thread.category.save()
  45. if thread.category != other_thread.category:
  46. thread.category.synchronize()
  47. thread.category.save()
  48. return Response({
  49. 'id': other_thread.pk,
  50. 'title': other_thread.title,
  51. 'url': other_thread.get_absolute_url(),
  52. })
  53. def threads_merge_endpoint(request):
  54. serializer = MergeThreadsSerializer(
  55. data=request.data,
  56. context={
  57. 'user': request.user
  58. },
  59. )
  60. if not serializer.is_valid():
  61. return Response(serializer.errors, status=400)
  62. threads = serializer.validated_data['threads']
  63. invalid_threads = []
  64. for thread in threads:
  65. try:
  66. allow_merge_thread(request.user, thread)
  67. except PermissionDenied as e:
  68. invalid_threads.append({
  69. 'id': thread.pk,
  70. 'title': thread.title,
  71. 'errors': [text_type(e)]
  72. })
  73. if invalid_threads:
  74. return Response(invalid_threads, status=403)
  75. polls_handler = PollMergeHandler(threads)
  76. if len(polls_handler.polls) == 1:
  77. poll = polls_handler.polls[0]
  78. elif polls_handler.is_merge_conflict():
  79. if 'poll' in request.data:
  80. polls_handler.set_resolution(request.data.get('poll'))
  81. if polls_handler.is_valid():
  82. poll = polls_handler.get_resolution()
  83. else:
  84. return Response({'detail': _("Invalid choice.")}, status=400)
  85. else:
  86. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  87. else:
  88. poll = None
  89. new_thread = merge_threads(request, serializer.validated_data, threads, poll)
  90. return Response(ThreadsListSerializer(new_thread).data)
  91. def merge_threads(request, validated_data, threads, poll):
  92. new_thread = Thread(
  93. category=validated_data['category'],
  94. started_on=threads[0].started_on,
  95. last_post_on=threads[0].last_post_on,
  96. )
  97. new_thread.set_title(validated_data['title'])
  98. new_thread.save()
  99. if poll:
  100. poll.move(new_thread)
  101. categories = []
  102. for thread in threads:
  103. categories.append(thread.category)
  104. new_thread.merge(thread)
  105. thread.delete()
  106. record_event(
  107. request,
  108. new_thread,
  109. 'merged',
  110. {
  111. 'merged_thread': thread.title,
  112. },
  113. commit=False,
  114. )
  115. new_thread.synchronize()
  116. new_thread.save()
  117. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  118. moderation.pin_thread_globally(request, new_thread)
  119. elif validated_data.get('weight'):
  120. moderation.pin_thread_locally(request, new_thread)
  121. if validated_data.get('is_hidden', False):
  122. moderation.hide_thread(request, new_thread)
  123. if validated_data.get('is_closed', False):
  124. moderation.close_thread(request, new_thread)
  125. if new_thread.category not in categories:
  126. categories.append(new_thread.category)
  127. for category in categories:
  128. category.synchronize()
  129. category.save()
  130. # set extra attrs on thread for UI
  131. new_thread.is_read = False
  132. new_thread.subscription = None
  133. add_acl(request.user, new_thread)
  134. return new_thread