Merge pull request #887 from eeintech/allow_api_thumbnail_update

Allowing Part thumbnail (image) update via API
This commit is contained in:
Oliver 2020-07-22 09:19:43 +10:00 committed by GitHub
commit daab81fa2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -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<pk>\d+)/?', PartThumbsUpdate.as_view(), name='api-part-thumbs-update'),
])),
url(r'^(?P<pk>\d+)/?', PartDetail.as_view(), name='api-part-detail'),

View File

@ -1,6 +1,7 @@
"""
JSON serializers for Part app
"""
import imghdr
from rest_framework import serializers
@ -92,6 +93,27 @@ class PartThumbSerializer(serializers.Serializer):
count = serializers.IntegerField(read_only=True)
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:
model = Part
fields = [
'image',
]
class PartBriefSerializer(InvenTreeModelSerializer):
""" Serializer for Part (brief detail) """