mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-19 13:35:40 +00:00
Adds a BomUpload endpoint to handle upload of complete BOM
This commit is contained in:
@ -1545,7 +1545,7 @@ class BomExtract(generics.CreateAPIView):
|
||||
"""
|
||||
Custom create function to return the extracted data
|
||||
"""
|
||||
|
||||
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
self.perform_create(serializer)
|
||||
@ -1556,6 +1556,16 @@ class BomExtract(generics.CreateAPIView):
|
||||
return Response(data, status=status.HTTP_201_CREATED, headers=headers)
|
||||
|
||||
|
||||
class BomUpload(generics.CreateAPIView):
|
||||
"""
|
||||
API endpoint for uploading a complete Bill of Materials.
|
||||
|
||||
It is assumed that the BOM has been extracted from a file using the BomExtract endpoint.
|
||||
"""
|
||||
|
||||
queryset = Part.objects.all()
|
||||
serializer_class = part_serializers.BomUploadSerializer
|
||||
|
||||
|
||||
class BomDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
""" API endpoint for detail view of a single BomItem object """
|
||||
@ -1710,6 +1720,9 @@ bom_api_urls = [
|
||||
])),
|
||||
|
||||
url(r'^extract/', BomExtract.as_view(), name='api-bom-extract'),
|
||||
|
||||
url(r'^upload/', BomUpload.as_view(), name='api-bom-upload'),
|
||||
|
||||
# Catch-all
|
||||
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
||||
]
|
||||
|
@ -8,7 +8,7 @@ import os
|
||||
import tablib
|
||||
|
||||
from django.urls import reverse_lazy
|
||||
from django.db import models
|
||||
from django.db import models, transaction
|
||||
from django.db.models import Q
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@ -465,7 +465,13 @@ class BomItemSerializer(InvenTreeModelSerializer):
|
||||
|
||||
price_range = serializers.CharField(read_only=True)
|
||||
|
||||
quantity = InvenTreeDecimalField()
|
||||
quantity = InvenTreeDecimalField(required=True)
|
||||
|
||||
def validate_quantity(self, quantity):
|
||||
if quantity <= 0:
|
||||
raise serializers.ValidationError(_("Quantity must be greater than zero"))
|
||||
|
||||
return quantity
|
||||
|
||||
part = serializers.PrimaryKeyRelatedField(queryset=Part.objects.filter(assembly=True))
|
||||
|
||||
@ -927,4 +933,37 @@ class BomExtractSerializer(serializers.Serializer):
|
||||
"""
|
||||
There is no action associated with "saving" this serializer
|
||||
"""
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
class BomUploadSerializer(serializers.Serializer):
|
||||
"""
|
||||
Serializer for uploading a BOM against a specified part.
|
||||
|
||||
A "BOM" is a set of BomItem objects which are to be validated together as a set
|
||||
"""
|
||||
|
||||
items = BomItemSerializer(many=True, required=True)
|
||||
|
||||
def validate(self, data):
|
||||
|
||||
data = super().validate(data)
|
||||
|
||||
items = data['items']
|
||||
|
||||
if len(items) == 0:
|
||||
raise serializers.ValidationError(_("At least one BOM item is required"))
|
||||
|
||||
return data
|
||||
|
||||
def save(self):
|
||||
|
||||
data = self.validated_data
|
||||
|
||||
items = data['items']
|
||||
|
||||
with transaction.atomic():
|
||||
|
||||
for item in items:
|
||||
print(item)
|
||||
|
Reference in New Issue
Block a user