split.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.translation import ugettext as _
  4. from django.utils.translation import ungettext
  5. from misago.conf import settings
  6. from misago.threads.models import Thread
  7. from misago.threads.moderation import threads as moderation
  8. from misago.threads.permissions import exclude_invisible_posts
  9. from misago.threads.serializers import NewThreadSerializer
  10. SPLIT_LIMIT = settings.MISAGO_POSTS_PER_PAGE + settings.MISAGO_POSTS_TAIL
  11. class SplitError(Exception):
  12. def __init__(self, msg):
  13. self.msg = msg
  14. def posts_split_endpoint(request, thread):
  15. if not thread.acl['can_move_posts']:
  16. raise PermissionDenied(_("You can't split posts from this thread."))
  17. try:
  18. posts = clean_posts_for_split(request, thread)
  19. except SplitError as e:
  20. return Response({'detail': e.msg}, status=400)
  21. serializer = NewThreadSerializer(context=request.user, data=request.data)
  22. if serializer.is_valid():
  23. split_posts_to_new_thread(request, thread, serializer.validated_data, posts)
  24. return Response({})
  25. else:
  26. return Response(serializer.errors, status=400)
  27. def clean_posts_for_split(request, thread):
  28. try:
  29. posts_ids = list(map(int, request.data.get('posts', [])))
  30. except (ValueError, TypeError):
  31. raise SplitError(_("One or more post ids received were invalid."))
  32. if not posts_ids:
  33. raise SplitError(_("You have to specify at least one post to split."))
  34. elif len(posts_ids) > SPLIT_LIMIT:
  35. message = ungettext(
  36. "No more than %(limit)s post can be split at single time.",
  37. "No more than %(limit)s posts can be split at single time.",
  38. SPLIT_LIMIT,
  39. )
  40. raise SplitError(message % {'limit': SPLIT_LIMIT})
  41. posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)
  42. posts_queryset = posts_queryset.select_for_update().filter(id__in=posts_ids).order_by('id')
  43. posts = []
  44. for post in posts_queryset:
  45. if post.is_event:
  46. raise SplitError(_("Events can't be split."))
  47. if post.pk == thread.first_post_id:
  48. raise SplitError(_("You can't split thread's first post."))
  49. if post.is_hidden and not thread.category.acl['can_hide_posts']:
  50. raise SplitError(_("You can't split posts the content you can't see."))
  51. posts.append(post)
  52. if len(posts) != len(posts_ids):
  53. raise SplitError(_("One or more posts to split could not be found."))
  54. return posts
  55. def split_posts_to_new_thread(request, thread, validated_data, posts):
  56. new_thread = Thread(
  57. category=validated_data['category'],
  58. started_on=thread.started_on,
  59. last_post_on=thread.last_post_on,
  60. )
  61. new_thread.set_title(validated_data['title'])
  62. new_thread.save()
  63. for post in posts:
  64. post.move(new_thread)
  65. post.save()
  66. thread.synchronize()
  67. thread.save()
  68. new_thread.synchronize()
  69. new_thread.save()
  70. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  71. moderation.pin_thread_globally(request, new_thread)
  72. elif validated_data.get('weight'):
  73. moderation.pin_thread_locally(request, new_thread)
  74. if validated_data.get('is_hidden', False):
  75. moderation.hide_thread(request, new_thread)
  76. if validated_data.get('is_closed', False):
  77. moderation.close_thread(request, new_thread)
  78. thread.category.synchronize()
  79. thread.category.save()
  80. if new_thread.category != thread.category:
  81. new_thread.category.synchronize()
  82. new_thread.category.save()