API: Allow part deletion if inactive

This commit is contained in:
eeintech 2020-07-08 09:43:09 -05:00
parent 3d7676e292
commit 9b7977a217

View File

@ -190,7 +190,7 @@ class PartThumbs(generics.ListAPIView):
return Response(data)
class PartDetail(generics.RetrieveUpdateAPIView):
class PartDetail(generics.RetrieveUpdateDestroyAPIView):
""" API endpoint for detail view of a single Part object """
queryset = Part.objects.all()
@ -229,6 +229,18 @@ class PartDetail(generics.RetrieveUpdateAPIView):
return self.serializer_class(*args, **kwargs)
def destroy(self, request, *args, **kwargs):
# Retrieve part
part = Part.objects.get(pk=int(kwargs['pk']))
# Check if inactive
if not part.active:
# Delete
return super(PartDetail, self).destroy(request, *args, **kwargs)
else:
# Return 405 error
message = f'Part \'{part.name}\' (pk = {part.pk}) is active: cannot delete'
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED, data=message)
class PartList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of Part objects