merge.py 4.1 KB

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