move.py 1.3 KB

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