split.py 3.3 KB

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