From af70dd5dfef1d30da345b19c8e4a9241b71a1fe4 Mon Sep 17 00:00:00 2001 From: eeintech Date: Mon, 20 Jul 2020 16:16:26 -0500 Subject: [PATCH 1/2] Allowing Part thumbnail (image) update via API --- InvenTree/part/api.py | 20 +++++++++++++++++++- InvenTree/part/serializers.py | 12 ++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 472ab1996e..9db4ef5425 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -190,6 +190,21 @@ class PartThumbs(generics.ListAPIView): return Response(data) +class PartThumbsUpdate(generics.RetrieveUpdateAPIView): + """ API endpoint for updating Part thumbnails""" + + queryset = Part.objects.all() + serializer_class = part_serializers.PartThumbSerializerUpdate + + permission_classes = [ + permissions.IsAuthenticated, + ] + + filter_backends = [ + DjangoFilterBackend + ] + + class PartDetail(generics.RetrieveUpdateDestroyAPIView): """ API endpoint for detail view of a single Part object """ @@ -716,7 +731,10 @@ part_api_urls = [ url(r'^.*$', PartParameterList.as_view(), name='api-part-param-list'), ])), - url(r'^thumbs/', PartThumbs.as_view(), name='api-part-thumbs'), + url(r'^thumbs/', include([ + url(r'^$', PartThumbs.as_view(), name='api-part-thumbs'), + url(r'^(?P\d+)/?', PartThumbsUpdate.as_view(), name='api-part-thumbs-update'), + ])), url(r'^(?P\d+)/?', PartDetail.as_view(), name='api-part-detail'), diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index f625a02c41..a77b0f1c3f 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -92,6 +92,18 @@ class PartThumbSerializer(serializers.Serializer): count = serializers.IntegerField(read_only=True) +class PartThumbSerializerUpdate(InvenTreeModelSerializer): + """ Serializer for updating Part thumbnail """ + + image = InvenTreeAttachmentSerializerField(required=True) + + class Meta: + model = Part + fields = [ + 'image', + ] + + class PartBriefSerializer(InvenTreeModelSerializer): """ Serializer for Part (brief detail) """ From f386039d5a9a26d65a67deb3e5babf9cd2f8f17e Mon Sep 17 00:00:00 2001 From: eeintech Date: Tue, 21 Jul 2020 16:01:17 -0500 Subject: [PATCH 2/2] Added validation method for uploaded file using imghdr library --- InvenTree/part/serializers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index a77b0f1c3f..d460748468 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -1,6 +1,7 @@ """ JSON serializers for Part app """ +import imghdr from rest_framework import serializers @@ -95,6 +96,15 @@ class PartThumbSerializer(serializers.Serializer): class PartThumbSerializerUpdate(InvenTreeModelSerializer): """ Serializer for updating Part thumbnail """ + def validate_image(self, value): + """ + Check that file is an image. + """ + validate = imghdr.what(value) + if not validate: + raise serializers.ValidationError("File is not an image") + return value + image = InvenTreeAttachmentSerializerField(required=True) class Meta: