merge.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. serializer.is_valid(raise_exception=True)
  24. # merge polls
  25. other_thread = serializer.validated_data['other_thread']
  26. poll = serializer.validated_data['poll']
  27. if poll:
  28. if hasattr(other_thread, 'poll') and poll != other_thread.poll:
  29. other_thread.poll.delete()
  30. poll.move(other_thread)
  31. else:
  32. if hasattr(thread, 'poll'):
  33. thread.poll.delete()
  34. if hasattr(other_thread, 'poll'):
  35. other_thread.poll.delete()
  36. # merge thread contents
  37. moderation.merge_thread(request, other_thread, thread)
  38. other_thread.synchronize()
  39. other_thread.save()
  40. other_thread.category.synchronize()
  41. other_thread.category.save()
  42. if thread.category != other_thread.category:
  43. thread.category.synchronize()
  44. thread.category.save()
  45. return Response({
  46. 'id': other_thread.pk,
  47. 'title': other_thread.title,
  48. 'url': other_thread.get_absolute_url(),
  49. })
  50. def threads_merge_endpoint(request):
  51. serializer = MergeThreadsSerializer(
  52. data=request.data,
  53. context={
  54. 'user': request.user
  55. },
  56. )
  57. serializer.is_valid(raise_exception=True)
  58. data = serializer.validated_data
  59. threads = data['threads']
  60. poll = data['poll']
  61. new_thread = Thread(
  62. category=data['category'],
  63. started_on=threads[0].started_on,
  64. last_post_on=threads[0].last_post_on,
  65. )
  66. new_thread.set_title(data['title'])
  67. new_thread.save()
  68. if poll:
  69. poll.move(new_thread)
  70. categories = []
  71. for thread in threads:
  72. categories.append(thread.category)
  73. new_thread.merge(thread)
  74. thread.delete()
  75. record_event(
  76. request,
  77. new_thread,
  78. 'merged',
  79. {
  80. 'merged_thread': thread.title
  81. },
  82. commit=False,
  83. )
  84. new_thread.synchronize()
  85. new_thread.save()
  86. if data.get('weight') == Thread.WEIGHT_GLOBAL:
  87. moderation.pin_thread_globally(request, new_thread)
  88. elif data.get('weight'):
  89. moderation.pin_thread_locally(request, new_thread)
  90. if data.get('is_hidden', False):
  91. moderation.hide_thread(request, new_thread)
  92. if data.get('is_closed', False):
  93. moderation.close_thread(request, new_thread)
  94. if new_thread.category not in categories:
  95. categories.append(new_thread.category)
  96. for category in categories:
  97. category.synchronize()
  98. category.save()
  99. # set extra attrs on thread for UI
  100. new_thread.is_read = False
  101. new_thread.subscription = None
  102. add_acl(request.user, new_thread)
  103. return Response(ThreadsListSerializer(new_thread).data)