merge.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from django.core.exceptions import PermissionDenied
  2. from django.db import transaction
  3. from django.http import Http404
  4. from django.utils.translation import gettext as _
  5. from django.utils.translation import ungettext
  6. from rest_framework.response import Response
  7. from misago.acl import add_acl
  8. from misago.categories.models import THREADS_ROOT_NAME, Category
  9. from misago.categories.permissions import can_browse_category, can_see_category
  10. from ...events import record_event
  11. from ...models import Thread
  12. from ...moderation import threads as moderation
  13. from ...permissions import can_see_thread
  14. from ...serializers import MergeThreadsSerializer, ThreadsListSerializer
  15. from ...threadtypes import trees_map
  16. from ...utils import add_categories_to_threads, get_thread_id_from_url
  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. @transaction.atomic
  22. def thread_merge_endpoint(request, thread, viewmodel):
  23. if not thread.acl['can_merge']:
  24. raise PermissionDenied(_("You don't have permission to merge this thread with others."))
  25. other_thread_id = get_thread_id_from_url(request, request.data.get('thread_url', None))
  26. if not other_thread_id:
  27. return Response({'detail': _("This is not a valid thread link.")}, status=400)
  28. if other_thread_id == thread.pk:
  29. return Response({'detail': _("You can't merge thread with itself.")}, status=400)
  30. try:
  31. other_thread = viewmodel(request, other_thread_id).thread
  32. except PermissionDenied as e:
  33. return Response({
  34. 'detail': e.args[0]
  35. }, status=400)
  36. except Http404:
  37. return Response({
  38. 'detail': _("The thread you have entered link to doesn't exist or you don't have permission to see it.")
  39. }, status=400)
  40. if not other_thread.acl['can_merge']:
  41. return Response({
  42. 'detail': _("You don't have permission to merge this thread with current one.")
  43. }, status=400)
  44. moderation.merge_thread(request, other_thread, thread)
  45. other_thread.synchronize()
  46. other_thread.save()
  47. other_thread.category.synchronize()
  48. other_thread.category.save()
  49. if thread.category != other_thread.category:
  50. thread.category.synchronize()
  51. thread.category.save()
  52. return Response({
  53. 'id': other_thread.pk,
  54. 'title': other_thread.title,
  55. 'url': other_thread.get_absolute_url()
  56. })
  57. def threads_merge_endpoint(request):
  58. try:
  59. threads = clean_threads_for_merge(request)
  60. except MergeError as e:
  61. return Response({'detail': e.msg}, status=403)
  62. invalid_threads = []
  63. for thread in threads:
  64. if not thread.acl['can_merge']:
  65. invalid_threads.append({
  66. 'id': thread.pk,
  67. 'title': thread.title,
  68. 'errors': [
  69. _("You don't have permission to merge this thread with others.")
  70. ]
  71. })
  72. if invalid_threads:
  73. return Response(invalid_threads, status=403)
  74. serializer = MergeThreadsSerializer(context=request.user, data=request.data)
  75. if serializer.is_valid():
  76. new_thread = merge_threads(request, serializer.validated_data, threads)
  77. return Response(ThreadsListSerializer(new_thread).data)
  78. else:
  79. return Response(serializer.errors, status=400)
  80. def clean_threads_for_merge(request):
  81. try:
  82. threads_ids = list(map(int, request.data.get('threads', [])))
  83. except (ValueError, TypeError):
  84. raise MergeError(_("One or more thread ids received were invalid."))
  85. if len(threads_ids) < 2:
  86. raise MergeError(_("You have to select at least two threads to merge."))
  87. elif len(threads_ids) > MERGE_LIMIT:
  88. message = ungettext(
  89. "No more than %(limit)s thread can be merged at single time.",
  90. "No more than %(limit)s threads can be merged at single time.",
  91. MERGE_LIMIT)
  92. raise MergeError(message % {'limit': MERGE_LIMIT})
  93. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  94. threads_queryset = Thread.objects.filter(
  95. id__in=threads_ids,
  96. category__tree_id=threads_tree_id,
  97. ).select_related('category').order_by('-id')
  98. threads = []
  99. for thread in threads_queryset:
  100. add_acl(request.user, thread)
  101. if can_see_thread(request.user, thread):
  102. threads.append(thread)
  103. if len(threads) != len(threads_ids):
  104. raise MergeError(_("One or more threads to merge could not be found."))
  105. return threads
  106. @transaction.atomic
  107. def merge_threads(request, validated_data, threads):
  108. new_thread = Thread(
  109. category=validated_data['category'],
  110. weight=validated_data.get('weight', 0),
  111. is_closed=validated_data.get('is_closed', False),
  112. started_on=threads[0].started_on,
  113. last_post_on=threads[0].last_post_on,
  114. )
  115. new_thread.set_title(validated_data['title'])
  116. new_thread.save()
  117. categories = []
  118. for thread in threads:
  119. categories.append(thread.category)
  120. new_thread.merge(thread)
  121. thread.delete()
  122. record_event(request, new_thread, 'merged', {
  123. 'merged_thread': thread.title,
  124. }, commit=False)
  125. new_thread.synchronize()
  126. new_thread.save()
  127. if new_thread.category not in categories:
  128. categories.append(new_thread.category)
  129. for category in categories:
  130. category.synchronize()
  131. category.save()
  132. # set extra attrs on thread for UI
  133. new_thread.is_read = False
  134. new_thread.subscription = None
  135. # add top category to thread
  136. if validated_data.get('top_category'):
  137. categories = list(Category.objects.all_categories().filter(
  138. id__in=request.user.acl['visible_categories']
  139. ))
  140. add_categories_to_threads(validated_data['top_category'], categories, [new_thread])
  141. else:
  142. new_thread.top_category = None
  143. new_thread.save()
  144. add_acl(request.user, new_thread)
  145. return new_thread