merge.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. serializer.is_valid(raise_exception=True)
  61. new_thread = merge_threads(request, serializer.validated_data)
  62. return Response(ThreadsListSerializer(new_thread).data)
  63. def merge_threads(request, validated_data):
  64. threads = validated_data['threads']
  65. poll = validated_data.get('poll')
  66. new_thread = Thread(
  67. category=validated_data['category'],
  68. started_on=threads[0].started_on,
  69. last_post_on=threads[0].last_post_on,
  70. )
  71. new_thread.set_title(validated_data['title'])
  72. new_thread.save()
  73. if poll:
  74. poll.move(new_thread)
  75. categories = []
  76. for thread in threads:
  77. categories.append(thread.category)
  78. new_thread.merge(thread)
  79. thread.delete()
  80. record_event(
  81. request,
  82. new_thread,
  83. 'merged',
  84. {
  85. 'merged_thread': thread.title,
  86. },
  87. commit=False,
  88. )
  89. new_thread.synchronize()
  90. new_thread.save()
  91. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  92. moderation.pin_thread_globally(request, new_thread)
  93. elif validated_data.get('weight'):
  94. moderation.pin_thread_locally(request, new_thread)
  95. if validated_data.get('is_hidden', False):
  96. moderation.hide_thread(request, new_thread)
  97. if validated_data.get('is_closed', False):
  98. moderation.close_thread(request, new_thread)
  99. if new_thread.category not in categories:
  100. categories.append(new_thread.category)
  101. for category in categories:
  102. category.synchronize()
  103. category.save()
  104. # set extra attrs on thread for UI
  105. new_thread.is_read = False
  106. new_thread.subscription = None
  107. add_acl(request.user, new_thread)
  108. return new_thread