split.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 _
  5. from misago.threads.models import Thread
  6. from misago.threads.moderation import threads as moderation
  7. from misago.threads.serializers import SplitPostsSerializer
  8. def posts_split_endpoint(request, thread):
  9. if not thread.acl['can_move_posts']:
  10. raise PermissionDenied(_("You can't split posts from this thread."))
  11. serializer = SplitPostsSerializer(
  12. data=request.data,
  13. context={
  14. 'thread': thread,
  15. 'user': request.user,
  16. },
  17. )
  18. if not serializer.is_valid():
  19. return Response(serializer.errors, status=400)
  20. split_posts_to_new_thread(request, thread, serializer.validated_data)
  21. return Response({})
  22. def split_posts_to_new_thread(request, thread, validated_data):
  23. new_thread = Thread(
  24. category=validated_data['category'],
  25. started_on=thread.started_on,
  26. last_post_on=thread.last_post_on,
  27. )
  28. new_thread.set_title(validated_data['title'])
  29. new_thread.save()
  30. for post in validated_data['posts']:
  31. post.move(new_thread)
  32. post.save()
  33. thread.synchronize()
  34. thread.save()
  35. new_thread.synchronize()
  36. new_thread.save()
  37. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  38. moderation.pin_thread_globally(request, new_thread)
  39. elif validated_data.get('weight'):
  40. moderation.pin_thread_locally(request, new_thread)
  41. if validated_data.get('is_hidden', False):
  42. moderation.hide_thread(request, new_thread)
  43. if validated_data.get('is_closed', False):
  44. moderation.close_thread(request, new_thread)
  45. thread.category.synchronize()
  46. thread.category.save()
  47. if new_thread.category != thread.category:
  48. new_thread.category.synchronize()
  49. new_thread.category.save()