From 222c9f18763a64833e87e82d8ad4e12aa2724461 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 13 Jun 2019 22:58:02 +1000 Subject: [PATCH 1/2] Allow quantity editing via /api/stock/ endpoint - Allows creation of a new StockItem with a specified quantity --- InvenTree/stock/serializers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index 21f2ef4e9e..c7cf2ef17a 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -102,7 +102,6 @@ class StockItemSerializer(serializers.ModelSerializer): 'stocktake_date', 'stocktake_user', 'updated', - 'quantity', 'in_stock' ] From ff6df9741f2e42dedc638f4dc997334ffbcfe9af Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 13 Jun 2019 23:03:58 +1000 Subject: [PATCH 2/2] Perform model validation on API serializers If you have a class, why not subclass it?! --- InvenTree/company/serializers.py | 10 ++++++---- InvenTree/part/serializers.py | 6 +++--- InvenTree/stock/models.py | 2 ++ InvenTree/stock/serializers.py | 14 +++++++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 773e033b7f..77a4bddbf3 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -7,10 +7,12 @@ from rest_framework import serializers from .models import Company from .models import SupplierPart, SupplierPriceBreak +from InvenTree.serializers import InvenTreeModelSerializer + from part.serializers import PartBriefSerializer -class CompanyBriefSerializer(serializers.ModelSerializer): +class CompanyBriefSerializer(InvenTreeModelSerializer): """ Serializer for Company object (limited detail) """ url = serializers.CharField(source='get_absolute_url', read_only=True) @@ -24,7 +26,7 @@ class CompanyBriefSerializer(serializers.ModelSerializer): ] -class CompanySerializer(serializers.ModelSerializer): +class CompanySerializer(InvenTreeModelSerializer): """ Serializer for Company object (full detail) """ url = serializers.CharField(source='get_absolute_url', read_only=True) @@ -52,7 +54,7 @@ class CompanySerializer(serializers.ModelSerializer): ] -class SupplierPartSerializer(serializers.ModelSerializer): +class SupplierPartSerializer(InvenTreeModelSerializer): """ Serializer for SupplierPart object """ url = serializers.CharField(source='get_absolute_url', read_only=True) @@ -92,7 +94,7 @@ class SupplierPartSerializer(serializers.ModelSerializer): ] -class SupplierPriceBreakSerializer(serializers.ModelSerializer): +class SupplierPriceBreakSerializer(InvenTreeModelSerializer): """ Serializer for SupplierPriceBreak object """ class Meta: diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 37f5f1db7a..36be5de6db 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -12,7 +12,7 @@ from .models import BomItem from InvenTree.serializers import InvenTreeModelSerializer -class CategorySerializer(serializers.ModelSerializer): +class CategorySerializer(InvenTreeModelSerializer): """ Serializer for PartCategory """ url = serializers.CharField(source='get_absolute_url', read_only=True) @@ -29,7 +29,7 @@ class CategorySerializer(serializers.ModelSerializer): ] -class PartBriefSerializer(serializers.ModelSerializer): +class PartBriefSerializer(InvenTreeModelSerializer): """ Serializer for Part (brief detail) """ url = serializers.CharField(source='get_absolute_url', read_only=True) @@ -56,7 +56,7 @@ class PartBriefSerializer(serializers.ModelSerializer): ] -class PartSerializer(serializers.ModelSerializer): +class PartSerializer(InvenTreeModelSerializer): """ Serializer for complete detail information of a part. Used when displaying all details of a single component. """ diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index b9773f9d77..50a0f39e72 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -142,6 +142,8 @@ class StockItem(models.Model): - Quantity must be 1 if the StockItem has a serial number """ + print("Trying to clean the StockItem") + # The 'supplier_part' field must point to the same part! try: if self.supplier_part is not None: diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index c7cf2ef17a..4ec0530bf4 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -8,10 +8,10 @@ from .models import StockItem, StockLocation from .models import StockItemTracking from part.serializers import PartBriefSerializer -from InvenTree.serializers import UserSerializerBrief +from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer -class LocationBriefSerializer(serializers.ModelSerializer): +class LocationBriefSerializer(InvenTreeModelSerializer): """ Provides a brief serializer for a StockLocation object """ @@ -28,7 +28,7 @@ class LocationBriefSerializer(serializers.ModelSerializer): ] -class StockItemSerializerBrief(serializers.ModelSerializer): +class StockItemSerializerBrief(InvenTreeModelSerializer): """ Brief serializers for a StockItem """ location_name = serializers.CharField(source='location', read_only=True) @@ -47,7 +47,7 @@ class StockItemSerializerBrief(serializers.ModelSerializer): ] -class StockItemSerializer(serializers.ModelSerializer): +class StockItemSerializer(InvenTreeModelSerializer): """ Serializer for a StockItem: - Includes serialization for the linked part @@ -106,14 +106,14 @@ class StockItemSerializer(serializers.ModelSerializer): ] -class StockQuantitySerializer(serializers.ModelSerializer): +class StockQuantitySerializer(InvenTreeModelSerializer): class Meta: model = StockItem fields = ('quantity',) -class LocationSerializer(serializers.ModelSerializer): +class LocationSerializer(InvenTreeModelSerializer): """ Detailed information about a stock location """ @@ -131,7 +131,7 @@ class LocationSerializer(serializers.ModelSerializer): ] -class StockTrackingSerializer(serializers.ModelSerializer): +class StockTrackingSerializer(InvenTreeModelSerializer): """ Serializer for StockItemTracking model """ url = serializers.CharField(source='get_absolute_url', read_only=True)