merge.py 8.0 KB

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