merge.py 5.6 KB

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