move.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. if 'thread_url' in serializer.errors:
  18. errors = serializer.errors['thread_url']
  19. else:
  20. errors = list(serializer.errors.values())[0]
  21. return Response({'detail': errors[0]}, status=400)
  22. new_thread = serializer.new_thread
  23. for post in serializer.posts_cache:
  24. post.move(new_thread)
  25. post.save()
  26. thread.synchronize()
  27. thread.save()
  28. new_thread.synchronize()
  29. new_thread.save()
  30. thread.category.synchronize()
  31. thread.category.save()
  32. if thread.category != new_thread.category:
  33. new_thread.category.synchronize()
  34. new_thread.category.save()
  35. return Response({})