merge.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.category.synchronize()
  46. other_thread.category.save()
  47. if thread.category != other_thread.category:
  48. thread.category.synchronize()
  49. thread.category.save()
  50. return Response({
  51. 'id': other_thread.pk,
  52. 'title': other_thread.title,
  53. 'url': other_thread.get_absolute_url()
  54. })
  55. def threads_merge_endpoint(request):
  56. try:
  57. threads = clean_threads_for_merge(request)
  58. except MergeError as e:
  59. return Response({'detail': e.msg}, status=403)
  60. invalid_threads = []
  61. for thread in threads:
  62. if not thread.acl['can_merge']:
  63. invalid_threads.append({
  64. 'id': thread.pk,
  65. 'title': thread.title,
  66. 'errors': [
  67. _("You don't have permission to merge this thread with others.")
  68. ]
  69. })
  70. if invalid_threads:
  71. return Response(invalid_threads, status=403)
  72. serializer = MergeThreadsSerializer(context=request.user, data=request.data)
  73. if serializer.is_valid():
  74. new_thread = merge_threads(request, serializer.validated_data, threads)
  75. return Response(ThreadsListSerializer(new_thread).data)
  76. else:
  77. return Response(serializer.errors, status=400)
  78. def clean_threads_for_merge(request):
  79. try:
  80. threads_ids = list(map(int, request.data.get('threads', [])))
  81. except (ValueError, TypeError):
  82. raise MergeError(_("One or more thread ids received were invalid."))
  83. if len(threads_ids) < 2:
  84. raise MergeError(_("You have to select at least two threads to merge."))
  85. elif len(threads_ids) > MERGE_LIMIT:
  86. message = ungettext(
  87. "No more than %(limit)s thread can be merged at single time.",
  88. "No more than %(limit)s threads can be merged at single time.",
  89. MERGE_LIMIT)
  90. raise MergeError(message % {'limit': MERGE_LIMIT})
  91. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  92. threads_queryset = Thread.objects.filter(
  93. id__in=threads_ids,
  94. category__tree_id=threads_tree_id,
  95. ).select_related('category').order_by('-id')
  96. threads = []
  97. for thread in threads_queryset:
  98. add_acl(request.user, thread)
  99. if can_see_thread(request.user, thread):
  100. threads.append(thread)
  101. if len(threads) != len(threads_ids):
  102. raise MergeError(_("One or more threads to merge could not be found."))
  103. return threads
  104. @transaction.atomic
  105. def merge_threads(request, validated_data, threads):
  106. new_thread = Thread(
  107. category=validated_data['category'],
  108. weight=validated_data.get('weight', 0),
  109. is_closed=validated_data.get('is_closed', False),
  110. started_on=threads[0].started_on,
  111. last_post_on=threads[0].last_post_on,
  112. )
  113. new_thread.set_title(validated_data['title'])
  114. new_thread.save()
  115. categories = []
  116. for thread in threads:
  117. categories.append(thread.category)
  118. new_thread.merge(thread)
  119. thread.delete()
  120. record_event(request, new_thread, 'merged', {
  121. 'merged_thread': thread.title,
  122. }, commit=False)
  123. new_thread.synchronize()
  124. new_thread.save()
  125. if new_thread.category not in categories:
  126. categories.append(new_thread.category)
  127. for category in categories:
  128. category.synchronize()
  129. category.save()
  130. # set extra attrs on thread for UI
  131. new_thread.is_read = False
  132. new_thread.subscription = None
  133. # add top category to thread
  134. if validated_data.get('top_category'):
  135. categories = list(Category.objects.all_categories().filter(
  136. id__in=request.user.acl['visible_categories']
  137. ))
  138. add_categories_to_threads(validated_data['top_category'], categories, [new_thread])
  139. else:
  140. new_thread.top_category = None
  141. new_thread.save()
  142. add_acl(request.user, new_thread)
  143. return new_thread