move.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from rest_framework.response import Response
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.translation import ugettext as _
  4. from misago.threads.serializers import MovePostsSerializer
  5. def posts_move_endpoint(request, thread, viewmodel):
  6. if not thread.acl['can_move_posts']:
  7. raise PermissionDenied(_("You can't move posts in this thread."))
  8. serializer = MovePostsSerializer(
  9. data=request.data,
  10. context={
  11. 'request': request,
  12. 'thread': thread,
  13. 'viewmodel': viewmodel,
  14. }
  15. )
  16. serializer.is_valid(raise_exception=True)
  17. new_thread = serializer.validated_data['new_thread']
  18. for post in serializer.validated_data['posts']:
  19. post.move(new_thread)
  20. post.save()
  21. thread.synchronize()
  22. thread.save()
  23. new_thread.synchronize()
  24. new_thread.save()
  25. thread.category.synchronize()
  26. thread.category.save()
  27. if thread.category != new_thread.category:
  28. new_thread.category.synchronize()
  29. new_thread.category.save()
  30. return Response({})