move.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if not serializer.is_valid():
  17. return Response(serializer.errors, status=400)
  18. new_thread = serializer.validated_data['new_thread']
  19. for post in serializer.validated_data['posts']:
  20. post.move(new_thread)
  21. post.save()
  22. thread.synchronize()
  23. thread.save()
  24. new_thread.synchronize()
  25. new_thread.save()
  26. thread.category.synchronize()
  27. thread.category.save()
  28. if thread.category != new_thread.category:
  29. new_thread.category.synchronize()
  30. new_thread.category.save()
  31. return Response({})