From e29594811baecb9fa4b7db41a8938f7a6f500418 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 22 Jun 2021 22:09:30 +1000 Subject: [PATCH] Allow direct upload of images via the API --- InvenTree/InvenTree/serializers.py | 14 ++++++++++++++ InvenTree/part/serializers.py | 12 ++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/serializers.py b/InvenTree/InvenTree/serializers.py index 2bc89ef637..14e78f94b1 100644 --- a/InvenTree/InvenTree/serializers.py +++ b/InvenTree/InvenTree/serializers.py @@ -101,3 +101,17 @@ class InvenTreeAttachmentSerializerField(serializers.FileField): return None return os.path.join(str(settings.MEDIA_URL), str(value)) + + +class InvenTreeImageSerializierField(serializers.FileField): + """ + Custom image serializer. + On upload, validate that the file is a valid image file + """ + + def to_representation(self, value): + + if not value: + return None + + return os.path.join(str(settings.MEDIA_URL), str(value)) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 76275bd5e1..7d70a843d8 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -7,12 +7,15 @@ from decimal import Decimal from django.db import models from django.db.models import Q from django.db.models.functions import Coalesce -from InvenTree.serializers import (InvenTreeAttachmentSerializerField, - InvenTreeModelSerializer) -from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus + from rest_framework import serializers from sql_util.utils import SubqueryCount, SubquerySum from djmoney.contrib.django_rest_framework import MoneyField + +from InvenTree.serializers import (InvenTreeAttachmentSerializerField, + InvenTreeImageSerializierField, + InvenTreeModelSerializer) +from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus from stock.models import StockItem from .models import (BomItem, Part, PartAttachment, PartCategory, @@ -300,7 +303,8 @@ class PartSerializer(InvenTreeModelSerializer): stock_item_count = serializers.IntegerField(read_only=True) suppliers = serializers.IntegerField(read_only=True) - image = serializers.CharField(source='get_image_url', read_only=True) + image = InvenTreeImageSerializerField(required=False) + # image = serializers.CharField(source='get_image_url', read_only=True) thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True) starred = serializers.SerializerMethodField()