merge.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from django.core.exceptions import PermissionDenied
  2. from django.db import transaction
  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.models import THREADS_ROOT_NAME, Category
  8. from misago.categories.permissions import can_browse_category, can_see_category
  9. from ...events import record_event
  10. from ...models import Thread
  11. from ...permissions import can_see_thread
  12. from ...serializers import MergeThreadsSerializer, ThreadsListSerializer
  13. from ...threadtypes import trees_map
  14. from ...utils import add_categories_to_threads
  15. MERGE_LIMIT = 20 # no more than 20 threads can be merged in single action
  16. class MergeError(Exception):
  17. def __init__(self, msg):
  18. self.msg = msg
  19. def threads_merge_endpoint(request):
  20. try:
  21. threads = clean_threads_for_merge(request)
  22. except MergeError as e:
  23. return Response({'detail': e.msg}, status=403)
  24. invalid_threads = []
  25. for thread in threads:
  26. if not thread.acl['can_merge']:
  27. invalid_threads.append({
  28. 'id': thread.pk,
  29. 'title': thread.title,
  30. 'errors': [
  31. _("You don't have permission to merge this thread with others.")
  32. ]
  33. })
  34. if invalid_threads:
  35. return Response(invalid_threads, status=403)
  36. serializer = MergeThreadsSerializer(context=request.user, data=request.data)
  37. if serializer.is_valid():
  38. new_thread = merge_threads(request, serializer.validated_data, threads)
  39. return Response(ThreadsListSerializer(new_thread).data)
  40. else:
  41. return Response(serializer.errors, status=400)
  42. def clean_threads_for_merge(request):
  43. try:
  44. threads_ids = map(int, request.data.get('threads', []))
  45. except (ValueError, TypeError):
  46. raise MergeError(_("One or more thread ids received were invalid."))
  47. if len(threads_ids) < 2:
  48. raise MergeError(_("You have to select at least two threads to merge."))
  49. elif len(threads_ids) > MERGE_LIMIT:
  50. message = ungettext(
  51. "No more than %(limit)s thread can be merged at single time.",
  52. "No more than %(limit)s threads can be merged at single time.",
  53. MERGE_LIMIT)
  54. raise MergeError(message % {'limit': MERGE_LIMIT})
  55. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  56. threads_queryset = Thread.objects.filter(
  57. id__in=threads_ids,
  58. category__tree_id=threads_tree_id,
  59. ).select_related('category').order_by('-id')
  60. threads = []
  61. for thread in threads_queryset:
  62. add_acl(request.user, thread)
  63. if can_see_thread(request.user, thread):
  64. threads.append(thread)
  65. if len(threads) != len(threads_ids):
  66. raise MergeError(_("One or more threads to merge could not be found."))
  67. return threads
  68. @transaction.atomic
  69. def merge_threads(request, validated_data, threads):
  70. new_thread = Thread(
  71. category=validated_data['category'],
  72. weight=validated_data.get('weight', 0),
  73. is_closed=validated_data.get('is_closed', False),
  74. started_on=threads[0].started_on,
  75. last_post_on=threads[0].last_post_on,
  76. )
  77. new_thread.set_title(validated_data['title'])
  78. new_thread.save()
  79. categories = []
  80. for thread in threads:
  81. categories.append(thread.category)
  82. new_thread.merge(thread)
  83. thread.delete()
  84. record_event(request, new_thread, 'merged', {
  85. 'merged_thread': thread.title,
  86. }, commit=False)
  87. new_thread.synchronize()
  88. new_thread.save()
  89. if new_thread.category not in categories:
  90. categories.append(new_thread.category)
  91. for category in categories:
  92. category.synchronize()
  93. category.save()
  94. # set extra attrs on thread for UI
  95. new_thread.is_read = False
  96. new_thread.subscription = None
  97. # add top category to thread
  98. if validated_data.get('top_category'):
  99. categories = list(Category.objects.all_categories().filter(
  100. id__in=request.user.acl['visible_categories']
  101. ))
  102. add_categories_to_threads(validated_data['top_category'], categories, [new_thread])
  103. else:
  104. new_thread.top_category = None
  105. new_thread.save()
  106. add_acl(request.user, new_thread)
  107. return new_thread