Commit 4b6ad3c3 authored by Samuel GAIST's avatar Samuel GAIST

[common][api] Simplify deletion of object using perform_destroy hook

parent 23e9e6eb
...@@ -263,32 +263,12 @@ class RetrieveUpdateDestroyContributionView( ...@@ -263,32 +263,12 @@ class RetrieveUpdateDestroyContributionView(
serializer = self.get_serializer(db_object, fields=fields_to_return) serializer = self.get_serializer(db_object, fields=fields_to_return)
return Response(serializer.data) return Response(serializer.data)
def delete(self, request, *args, **kwargs): def perform_destroy(self, instance):
author_name = self.kwargs.get("author_name")
object_name = self.kwargs.get("object_name")
version = self.kwargs.get("version", None)
# Retrieve the object
if version is None:
raise drf_exceptions.ValidationError(
{"version": "A version number must be provided"}
)
db_object = get_object_or_404(
self.model,
author__username__iexact=author_name,
name__iexact=object_name,
version=version,
)
# Check that the object can be deleted # Check that the object can be deleted
if not (db_object.deletable()): if not (instance.deletable()):
raise drf_exceptions.MethodNotAllowed( raise drf_exceptions.MethodNotAllowed(
"The {} can't be deleted anymore (needed by an attestation, an algorithm or another data format)".format( "The {} can't be deleted anymore (needed by an attestation, an algorithm or another data format)".format(
db_object.model_name() instance.model_name()
) )
) )
return super().perform_destroy(instance)
# Deletion of the object
db_object.delete()
return Response(status=204)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment