merge.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404
  4. from django.utils.six import text_type
  5. from django.utils.translation import ugettext as _
  6. from django.utils.translation import ungettext
  7. from misago.acl import add_acl
  8. from misago.categories import THREADS_ROOT_NAME
  9. from misago.core.utils import clean_ids_list
  10. from misago.threads.events import record_event
  11. from misago.threads.models import Thread
  12. from misago.threads.moderation import threads as moderation
  13. from misago.threads.permissions import allow_merge_thread, can_reply_thread, can_see_thread
  14. from misago.threads.serializers import NewThreadSerializer, ThreadsListSerializer
  15. from misago.threads.threadtypes import trees_map
  16. from misago.threads.utils import get_thread_id_from_url
  17. from .pollmergehandler import PollMergeHandler
  18. MERGE_LIMIT = 20 # no more than 20 threads can be merged in single action
  19. class MergeError(Exception):
  20. def __init__(self, msg):
  21. self.msg = msg
  22. def thread_merge_endpoint(request, thread, viewmodel):
  23. allow_merge_thread(request.user, thread)
  24. other_thread_id = get_thread_id_from_url(request, request.data.get('thread_url', None))
  25. if not other_thread_id:
  26. return Response({'detail': _("This is not a valid thread link.")}, status=400)
  27. if other_thread_id == thread.pk:
  28. return Response({'detail': _("You can't merge thread with itself.")}, status=400)
  29. try:
  30. other_thread = viewmodel(request, other_thread_id).unwrap()
  31. allow_merge_thread(request.user, other_thread, otherthread=True)
  32. if not can_reply_thread(request.user, other_thread):
  33. raise PermissionDenied(_("You can't merge this thread into thread you can't reply."))
  34. except PermissionDenied as e:
  35. return Response({'detail': e.args[0]}, status=400)
  36. except Http404:
  37. return Response(
  38. {
  39. 'detail': _(
  40. "The thread you have entered link to doesn't "
  41. "exist or you don't have permission to see it."
  42. )
  43. },
  44. status=400,
  45. )
  46. polls_handler = PollMergeHandler([thread, other_thread])
  47. if len(polls_handler.polls) == 1:
  48. poll = polls_handler.polls[0]
  49. poll.move(other_thread)
  50. elif polls_handler.is_merge_conflict():
  51. if 'poll' in request.data:
  52. polls_handler.set_resolution(request.data.get('poll'))
  53. if polls_handler.is_valid():
  54. poll = polls_handler.get_resolution()
  55. if poll and poll.thread_id != other_thread.id:
  56. other_thread.poll.delete()
  57. poll.move(other_thread)
  58. elif not poll:
  59. other_thread.poll.delete()
  60. else:
  61. return Response({'detail': _("Invalid choice.")}, status=400)
  62. else:
  63. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  64. moderation.merge_thread(request, other_thread, thread)
  65. other_thread.synchronize()
  66. other_thread.save()
  67. other_thread.category.synchronize()
  68. other_thread.category.save()
  69. if thread.category != other_thread.category:
  70. thread.category.synchronize()
  71. thread.category.save()
  72. return Response({
  73. 'id': other_thread.pk,
  74. 'title': other_thread.title,
  75. 'url': other_thread.get_absolute_url(),
  76. })
  77. def threads_merge_endpoint(request):
  78. try:
  79. threads = clean_threads_for_merge(request)
  80. except MergeError as e:
  81. return Response({'detail': e.msg}, status=403)
  82. invalid_threads = []
  83. for thread in threads:
  84. try:
  85. allow_merge_thread(request.user, thread)
  86. except PermissionDenied as e:
  87. invalid_threads.append({
  88. 'id': thread.pk,
  89. 'title': thread.title,
  90. 'errors': [text_type(e)]
  91. })
  92. if invalid_threads:
  93. return Response(invalid_threads, status=403)
  94. serializer = NewThreadSerializer(
  95. data=request.data,
  96. context={'user': request.user},
  97. )
  98. if serializer.is_valid():
  99. polls_handler = PollMergeHandler(threads)
  100. if len(polls_handler.polls) == 1:
  101. poll = polls_handler.polls[0]
  102. elif polls_handler.is_merge_conflict():
  103. if 'poll' in request.data:
  104. polls_handler.set_resolution(request.data.get('poll'))
  105. if polls_handler.is_valid():
  106. poll = polls_handler.get_resolution()
  107. else:
  108. return Response({'detail': _("Invalid choice.")}, status=400)
  109. else:
  110. return Response({'polls': polls_handler.get_available_resolutions()}, status=400)
  111. else:
  112. poll = None
  113. new_thread = merge_threads(request, serializer.validated_data, threads, poll)
  114. return Response(ThreadsListSerializer(new_thread).data)
  115. else:
  116. return Response(serializer.errors, status=400)
  117. def clean_threads_for_merge(request):
  118. threads_ids = clean_ids_list(
  119. request.data.get('threads', []),
  120. _("One or more thread ids received were invalid."),
  121. )
  122. if len(threads_ids) < 2:
  123. raise MergeError(_("You have to select at least two threads to merge."))
  124. elif len(threads_ids) > MERGE_LIMIT:
  125. message = ungettext(
  126. "No more than %(limit)s thread can be merged at single time.",
  127. "No more than %(limit)s threads can be merged at single time.",
  128. MERGE_LIMIT,
  129. )
  130. raise MergeError(message % {'limit': MERGE_LIMIT})
  131. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  132. threads_queryset = Thread.objects.filter(
  133. id__in=threads_ids,
  134. category__tree_id=threads_tree_id,
  135. ).select_related('category').order_by('-id')
  136. threads = []
  137. for thread in threads_queryset:
  138. add_acl(request.user, thread)
  139. if can_see_thread(request.user, thread):
  140. threads.append(thread)
  141. if len(threads) != len(threads_ids):
  142. raise MergeError(_("One or more threads to merge could not be found."))
  143. return threads
  144. def merge_threads(request, validated_data, threads, poll):
  145. new_thread = Thread(
  146. category=validated_data['category'],
  147. started_on=threads[0].started_on,
  148. last_post_on=threads[0].last_post_on,
  149. )
  150. new_thread.set_title(validated_data['title'])
  151. new_thread.save()
  152. if poll:
  153. poll.move(new_thread)
  154. categories = []
  155. for thread in threads:
  156. categories.append(thread.category)
  157. new_thread.merge(thread)
  158. thread.delete()
  159. record_event(
  160. request,
  161. new_thread,
  162. 'merged',
  163. {
  164. 'merged_thread': thread.title,
  165. },
  166. commit=False,
  167. )
  168. new_thread.synchronize()
  169. new_thread.save()
  170. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  171. moderation.pin_thread_globally(request, new_thread)
  172. elif validated_data.get('weight'):
  173. moderation.pin_thread_locally(request, new_thread)
  174. if validated_data.get('is_hidden', False):
  175. moderation.hide_thread(request, new_thread)
  176. if validated_data.get('is_closed', False):
  177. moderation.close_thread(request, new_thread)
  178. if new_thread.category not in categories:
  179. categories.append(new_thread.category)
  180. for category in categories:
  181. category.synchronize()
  182. category.save()
  183. # set extra attrs on thread for UI
  184. new_thread.is_read = False
  185. new_thread.subscription = None
  186. add_acl(request.user, new_thread)
  187. return new_thread