move.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django.core.exceptions import PermissionDenied
  2. from django.utils.translation import gettext as _
  3. from rest_framework.response import Response
  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={"request": request, "thread": thread, "viewmodel": viewmodel},
  11. )
  12. if not serializer.is_valid():
  13. if "new_thread" in serializer.errors:
  14. errors = serializer.errors["new_thread"]
  15. else:
  16. errors = list(serializer.errors.values())[0]
  17. return Response({"detail": errors[0]}, 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({})