split.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. serializer.is_valid(raise_exception=True)
  19. split_posts_to_new_thread(request, thread, serializer.validated_data)
  20. return Response({})
  21. def split_posts_to_new_thread(request, thread, validated_data):
  22. new_thread = Thread(
  23. category=validated_data['category'],
  24. started_on=thread.started_on,
  25. last_post_on=thread.last_post_on,
  26. )
  27. new_thread.set_title(validated_data['title'])
  28. new_thread.save()
  29. for post in validated_data['posts']:
  30. post.move(new_thread)
  31. post.save()
  32. thread.synchronize()
  33. thread.save()
  34. new_thread.synchronize()
  35. new_thread.save()
  36. if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
  37. moderation.pin_thread_globally(request, new_thread)
  38. elif validated_data.get('weight'):
  39. moderation.pin_thread_locally(request, new_thread)
  40. if validated_data.get('is_hidden', False):
  41. moderation.hide_thread(request, new_thread)
  42. if validated_data.get('is_closed', False):
  43. moderation.close_thread(request, new_thread)
  44. thread.category.synchronize()
  45. thread.category.save()
  46. if new_thread.category != thread.category:
  47. new_thread.category.synchronize()
  48. new_thread.category.save()