delete.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from django.db import transaction
  2. from rest_framework.response import Response
  3. from misago.threads.moderation import threads as moderation
  4. from misago.threads.permissions import allow_delete_thread
  5. from misago.threads.serializers import DeleteThreadsSerializer
  6. @transaction.atomic
  7. def delete_thread(request, thread):
  8. allow_delete_thread(request.user_acl, thread)
  9. moderation.delete_thread(request.user, thread)
  10. return Response({})
  11. def delete_bulk(request, viewmodel):
  12. serializer = DeleteThreadsSerializer(
  13. data={"threads": request.data},
  14. context={"request": request, "viewmodel": viewmodel},
  15. )
  16. if not serializer.is_valid():
  17. if "threads" in serializer.errors:
  18. errors = serializer.errors["threads"]
  19. if "details" in errors:
  20. return Response(hydrate_error_details(errors["details"]), status=400)
  21. return Response({"detail": errors[0]}, status=403)
  22. else:
  23. errors = list(serializer.errors)[0][0]
  24. return Response({"detail": errors}, status=400)
  25. for thread in serializer.validated_data["threads"]:
  26. with transaction.atomic():
  27. delete_thread(request, thread)
  28. return Response([])
  29. def hydrate_error_details(errors):
  30. for error in errors:
  31. error["thread"]["id"] = int(error["thread"]["id"])
  32. return errors