merge.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. from django.core.exceptions import PermissionDenied, ValidationError
  2. from django.http import Http404
  3. from django.utils.translation import gettext as _, ungettext
  4. from rest_framework import serializers
  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_WEIGHT_DEFAULT, THREAD_WEIGHT_GLOBAL, Thread
  11. from ...moderation import threads as moderation
  12. from ...permissions import allow_start_thread, can_see_thread
  13. from ...serializers import ThreadsListSerializer
  14. from ...threadtypes import trees_map
  15. from ...validators import validate_title
  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. 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).thread
  31. except PermissionDenied as e:
  32. return Response({
  33. 'detail': e.args[0]
  34. }, status=400)
  35. except Http404:
  36. return Response({
  37. 'detail': _("The thread you have entered link to doesn't exist or you don't have permission to see it.")
  38. }, status=400)
  39. if not other_thread.acl['can_merge']:
  40. return Response({
  41. 'detail': _("You don't have permission to merge this thread with current one.")
  42. }, status=400)
  43. moderation.merge_thread(request, other_thread, thread)
  44. other_thread.synchronize()
  45. other_thread.save()
  46. other_thread.category.synchronize()
  47. other_thread.category.save()
  48. if thread.category != other_thread.category:
  49. thread.category.synchronize()
  50. thread.category.save()
  51. return Response({
  52. 'id': other_thread.pk,
  53. 'title': other_thread.title,
  54. 'url': other_thread.get_absolute_url()
  55. })
  56. def threads_merge_endpoint(request):
  57. try:
  58. threads = clean_threads_for_merge(request)
  59. except MergeError as e:
  60. return Response({'detail': e.msg}, status=403)
  61. invalid_threads = []
  62. for thread in threads:
  63. if not thread.acl['can_merge']:
  64. invalid_threads.append({
  65. 'id': thread.pk,
  66. 'title': thread.title,
  67. 'errors': [
  68. _("You don't have permission to merge this thread with others.")
  69. ]
  70. })
  71. if invalid_threads:
  72. return Response(invalid_threads, status=403)
  73. serializer = MergeThreadsSerializer(context=request.user, data=request.data)
  74. if serializer.is_valid():
  75. new_thread = merge_threads(request, serializer.validated_data, threads)
  76. return Response(ThreadsListSerializer(new_thread).data)
  77. else:
  78. return Response(serializer.errors, status=400)
  79. def clean_threads_for_merge(request):
  80. try:
  81. threads_ids = list(map(int, request.data.get('threads', [])))
  82. except (ValueError, TypeError):
  83. raise MergeError(_("One or more thread ids received were invalid."))
  84. if len(threads_ids) < 2:
  85. raise MergeError(_("You have to select at least two threads to merge."))
  86. elif len(threads_ids) > MERGE_LIMIT:
  87. message = ungettext(
  88. "No more than %(limit)s thread can be merged at single time.",
  89. "No more than %(limit)s threads can be merged at single time.",
  90. MERGE_LIMIT)
  91. raise MergeError(message % {'limit': MERGE_LIMIT})
  92. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  93. threads_queryset = Thread.objects.filter(
  94. id__in=threads_ids,
  95. category__tree_id=threads_tree_id,
  96. ).select_for_update().select_related('category').order_by('-id')
  97. threads = []
  98. for thread in threads_queryset:
  99. add_acl(request.user, thread)
  100. if can_see_thread(request.user, thread):
  101. threads.append(thread)
  102. if len(threads) != len(threads_ids):
  103. raise MergeError(_("One or more threads to merge could not be found."))
  104. return threads
  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
  144. def validate_category(user, category_id, allow_root=False):
  145. try:
  146. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  147. category = Category.objects.get(
  148. tree_id=threads_tree_id,
  149. id=category_id,
  150. )
  151. except Category.DoesNotExist:
  152. category = None
  153. # Skip ACL validation for root category?
  154. if allow_root and category and not category.level:
  155. return category
  156. if not category or not can_see_category(user, category):
  157. raise ValidationError(_("Requested category could not be found."))
  158. if not can_browse_category(user, category):
  159. raise ValidationError(_("You don't have permission to access this category."))
  160. return category
  161. class MergeThreadsSerializer(serializers.Serializer):
  162. title = serializers.CharField()
  163. category = serializers.IntegerField()
  164. top_category = serializers.IntegerField(required=False, allow_null=True)
  165. weight = serializers.IntegerField(
  166. required=False,
  167. allow_null=True,
  168. max_value=THREAD_WEIGHT_GLOBAL,
  169. min_value=THREAD_WEIGHT_DEFAULT,
  170. )
  171. is_closed = serializers.NullBooleanField(required=False)
  172. def validate_title(self, title):
  173. return validate_title(title)
  174. def validate_top_category(self, category_id):
  175. return validate_category(self.context, category_id, allow_root=True)
  176. def validate_category(self, category_id):
  177. self.category = validate_category(self.context, category_id)
  178. return self.category
  179. def validate_weight(self, weight):
  180. try:
  181. add_acl(self.context, self.category)
  182. except AttributeError:
  183. return weight # don't validate weight further if category failed
  184. if weight > self.category.acl.get('can_pin_threads', 0):
  185. if weight == 2:
  186. raise ValidationError(_("You don't have permission to pin threads globally in this category."))
  187. else:
  188. raise ValidationError(_("You don't have permission to pin threads in this category."))
  189. return weight
  190. def validate_is_closed(self, is_closed):
  191. try:
  192. add_acl(self.context, self.category)
  193. except AttributeError:
  194. return is_closed # don't validate closed further if category failed
  195. if is_closed and not self.category.acl.get('can_close_threads'):
  196. raise ValidationError(_("You don't have permission to close threads in this category."))
  197. return is_closed