split.py 3.3 KB

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