2019-04-27 12:18:07 +00:00
|
|
|
"""
|
|
|
|
JSON serializers for Part app
|
|
|
|
"""
|
2021-08-15 12:43:52 +00:00
|
|
|
|
2020-07-21 21:01:17 +00:00
|
|
|
import imghdr
|
2020-04-19 12:54:46 +00:00
|
|
|
from decimal import Decimal
|
2022-02-04 13:12:11 +00:00
|
|
|
import os
|
|
|
|
import tablib
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2021-06-24 14:17:58 +00:00
|
|
|
from django.urls import reverse_lazy
|
2022-02-06 23:54:37 +00:00
|
|
|
from django.db import models, transaction
|
2020-09-05 12:35:19 +00:00
|
|
|
from django.db.models import Q
|
2020-04-19 12:54:46 +00:00
|
|
|
from django.db.models.functions import Coalesce
|
2021-12-21 06:16:27 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-06-22 12:09:30 +00:00
|
|
|
|
2020-10-12 18:14:24 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
from sql_util.utils import SubqueryCount, SubquerySum
|
2021-05-13 21:09:52 +00:00
|
|
|
from djmoney.contrib.django_rest_framework import MoneyField
|
2021-06-22 12:09:30 +00:00
|
|
|
|
|
|
|
from InvenTree.serializers import (InvenTreeAttachmentSerializerField,
|
2021-11-18 12:01:19 +00:00
|
|
|
InvenTreeDecimalField,
|
2021-06-22 12:16:11 +00:00
|
|
|
InvenTreeImageSerializerField,
|
2021-08-08 23:49:55 +00:00
|
|
|
InvenTreeModelSerializer,
|
2021-08-15 12:43:52 +00:00
|
|
|
InvenTreeAttachmentSerializer,
|
2021-08-08 23:49:55 +00:00
|
|
|
InvenTreeMoneySerializer)
|
2021-08-15 12:43:52 +00:00
|
|
|
|
2021-06-22 12:09:30 +00:00
|
|
|
from InvenTree.status_codes import BuildStatus, PurchaseOrderStatus
|
2020-10-12 18:14:24 +00:00
|
|
|
from stock.models import StockItem
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2021-10-13 02:58:41 +00:00
|
|
|
from .models import (BomItem, BomItemSubstitute,
|
2021-11-25 03:18:21 +00:00
|
|
|
Part, PartAttachment, PartCategory, PartRelated,
|
2020-10-12 18:14:24 +00:00
|
|
|
PartParameter, PartParameterTemplate, PartSellPriceBreak,
|
2021-06-05 15:01:49 +00:00
|
|
|
PartStar, PartTestTemplate, PartCategoryParameterTemplate,
|
|
|
|
PartInternalPriceBreak)
|
2019-04-26 13:34:15 +00:00
|
|
|
|
2019-04-13 23:25:46 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class CategorySerializer(InvenTreeModelSerializer):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Serializer for PartCategory """
|
2018-05-02 13:08:45 +00:00
|
|
|
|
2021-11-03 12:22:31 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_starred(self, category):
|
|
|
|
"""
|
|
|
|
Return True if the category is directly "starred" by the current user
|
|
|
|
"""
|
|
|
|
|
2021-11-03 13:01:52 +00:00
|
|
|
return category in self.context.get('starred_categories', [])
|
2021-11-03 12:22:31 +00:00
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
|
|
|
|
2020-04-02 22:15:09 +00:00
|
|
|
parts = serializers.IntegerField(source='item_count', read_only=True)
|
|
|
|
|
2021-07-18 11:11:53 +00:00
|
|
|
level = serializers.IntegerField(read_only=True)
|
|
|
|
|
2021-11-03 12:22:31 +00:00
|
|
|
starred = serializers.SerializerMethodField()
|
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = PartCategory
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'name',
|
2018-05-02 13:42:57 +00:00
|
|
|
'description',
|
2020-10-12 18:14:24 +00:00
|
|
|
'default_location',
|
2021-06-29 15:04:39 +00:00
|
|
|
'default_keywords',
|
2021-07-18 11:11:53 +00:00
|
|
|
'level',
|
2018-05-04 13:54:57 +00:00
|
|
|
'parent',
|
2020-04-02 22:15:09 +00:00
|
|
|
'parts',
|
2021-07-18 11:11:53 +00:00
|
|
|
'pathstring',
|
2021-11-03 12:22:31 +00:00
|
|
|
'starred',
|
2021-07-18 11:11:53 +00:00
|
|
|
'url',
|
2018-05-02 13:08:45 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-12-06 21:35:14 +00:00
|
|
|
class CategoryTree(InvenTreeModelSerializer):
|
2021-12-10 13:08:17 +00:00
|
|
|
"""
|
|
|
|
Serializer for PartCategory tree
|
|
|
|
"""
|
2021-12-06 21:35:14 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartCategory
|
|
|
|
fields = [
|
2021-12-10 13:08:17 +00:00
|
|
|
'pk',
|
|
|
|
'name',
|
2021-12-06 21:35:14 +00:00
|
|
|
'parent',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-08-15 12:43:52 +00:00
|
|
|
class PartAttachmentSerializer(InvenTreeAttachmentSerializer):
|
2020-05-11 13:25:55 +00:00
|
|
|
"""
|
|
|
|
Serializer for the PartAttachment class
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartAttachment
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'attachment',
|
2021-08-15 12:43:52 +00:00
|
|
|
'filename',
|
2021-11-28 01:57:37 +00:00
|
|
|
'link',
|
2021-06-30 07:44:23 +00:00
|
|
|
'comment',
|
|
|
|
'upload_date',
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
'upload_date',
|
2020-05-11 13:25:55 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-05-17 03:56:49 +00:00
|
|
|
class PartTestTemplateSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for the PartTestTemplate class
|
|
|
|
"""
|
|
|
|
|
2020-05-18 09:15:40 +00:00
|
|
|
key = serializers.CharField(read_only=True)
|
|
|
|
|
2020-05-17 03:56:49 +00:00
|
|
|
class Meta:
|
|
|
|
model = PartTestTemplate
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-05-18 09:15:40 +00:00
|
|
|
'key',
|
2020-05-17 03:56:49 +00:00
|
|
|
'part',
|
|
|
|
'test_name',
|
2020-05-18 09:15:40 +00:00
|
|
|
'description',
|
|
|
|
'required',
|
|
|
|
'requires_value',
|
|
|
|
'requires_attachment',
|
2020-05-17 03:56:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-09-18 12:11:51 +00:00
|
|
|
class PartSalePriceSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for sale prices for Part model.
|
|
|
|
"""
|
|
|
|
|
2021-11-18 12:01:19 +00:00
|
|
|
quantity = InvenTreeDecimalField()
|
2020-09-18 12:11:51 +00:00
|
|
|
|
2021-08-08 23:49:55 +00:00
|
|
|
price = InvenTreeMoneySerializer(
|
|
|
|
allow_null=True
|
|
|
|
)
|
|
|
|
|
|
|
|
price_string = serializers.CharField(source='price', read_only=True)
|
2020-09-18 12:11:51 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartSellPriceBreak
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'quantity',
|
2020-11-10 13:21:06 +00:00
|
|
|
'price',
|
2021-08-08 23:49:55 +00:00
|
|
|
'price_string',
|
2020-09-19 09:52:48 +00:00
|
|
|
]
|
2020-09-18 12:11:51 +00:00
|
|
|
|
|
|
|
|
2021-06-05 15:01:49 +00:00
|
|
|
class PartInternalPriceSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for internal prices for Part model.
|
|
|
|
"""
|
|
|
|
|
2021-11-18 12:01:19 +00:00
|
|
|
quantity = InvenTreeDecimalField()
|
2021-06-05 15:01:49 +00:00
|
|
|
|
2021-08-08 23:49:55 +00:00
|
|
|
price = InvenTreeMoneySerializer(
|
|
|
|
allow_null=True
|
|
|
|
)
|
|
|
|
|
|
|
|
price_string = serializers.CharField(source='price', read_only=True)
|
2021-06-05 15:01:49 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartInternalPriceBreak
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'quantity',
|
|
|
|
'price',
|
2021-08-08 23:49:55 +00:00
|
|
|
'price_string',
|
2021-06-05 15:01:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-02-10 11:03:06 +00:00
|
|
|
class PartThumbSerializer(serializers.Serializer):
|
|
|
|
"""
|
|
|
|
Serializer for the 'image' field of the Part model.
|
|
|
|
Used to serve and display existing Part images.
|
|
|
|
"""
|
|
|
|
|
|
|
|
image = serializers.URLField(read_only=True)
|
|
|
|
count = serializers.IntegerField(read_only=True)
|
|
|
|
|
|
|
|
|
2020-07-20 21:16:26 +00:00
|
|
|
class PartThumbSerializerUpdate(InvenTreeModelSerializer):
|
|
|
|
""" Serializer for updating Part thumbnail """
|
|
|
|
|
2020-07-21 21:01:17 +00:00
|
|
|
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
|
|
|
|
|
2020-07-20 21:16:26 +00:00
|
|
|
image = InvenTreeAttachmentSerializerField(required=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Part
|
|
|
|
fields = [
|
|
|
|
'image',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class PartBriefSerializer(InvenTreeModelSerializer):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Serializer for Part (brief detail) """
|
2018-05-02 13:08:45 +00:00
|
|
|
|
2020-04-06 02:57:04 +00:00
|
|
|
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2020-04-28 13:17:15 +00:00
|
|
|
stock = serializers.FloatField(source='total_stock')
|
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = Part
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-06-05 22:47:06 +00:00
|
|
|
'IPN',
|
2021-10-05 05:05:18 +00:00
|
|
|
'default_location',
|
2020-06-05 22:47:06 +00:00
|
|
|
'name',
|
|
|
|
'revision',
|
2019-05-12 02:16:04 +00:00
|
|
|
'full_name',
|
2018-05-02 13:42:57 +00:00
|
|
|
'description',
|
2020-04-06 02:57:04 +00:00
|
|
|
'thumbnail',
|
2019-07-10 03:18:07 +00:00
|
|
|
'active',
|
2020-02-11 10:43:17 +00:00
|
|
|
'assembly',
|
2020-05-22 13:33:27 +00:00
|
|
|
'is_template',
|
2020-04-21 12:02:17 +00:00
|
|
|
'purchaseable',
|
|
|
|
'salable',
|
2020-04-28 13:17:15 +00:00
|
|
|
'stock',
|
2020-05-22 13:33:27 +00:00
|
|
|
'trackable',
|
2020-02-11 10:43:17 +00:00
|
|
|
'virtual',
|
2021-08-26 11:54:25 +00:00
|
|
|
'units',
|
2018-05-02 13:08:45 +00:00
|
|
|
]
|
2017-03-28 12:14:36 +00:00
|
|
|
|
2018-04-15 15:02:17 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class PartSerializer(InvenTreeModelSerializer):
|
2017-04-10 23:41:03 +00:00
|
|
|
""" Serializer for complete detail information of a part.
|
|
|
|
Used when displaying all details of a single component.
|
|
|
|
"""
|
2017-03-29 12:45:27 +00:00
|
|
|
|
2021-06-24 14:17:58 +00:00
|
|
|
def get_api_url(self):
|
|
|
|
return reverse_lazy('api-part-list')
|
|
|
|
|
2020-04-19 13:50:41 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Custom initialization method for PartSerializer,
|
|
|
|
so that we can optionally pass extra fields based on the query.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.starred_parts = kwargs.pop('starred_parts', [])
|
|
|
|
|
|
|
|
category_detail = kwargs.pop('category_detail', False)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if category_detail is not True:
|
|
|
|
self.fields.pop('category_detail')
|
|
|
|
|
2019-05-19 22:13:22 +00:00
|
|
|
@staticmethod
|
2020-04-19 12:54:46 +00:00
|
|
|
def annotate_queryset(queryset):
|
|
|
|
"""
|
2020-04-19 13:56:16 +00:00
|
|
|
Add some extra annotations to the queryset,
|
2020-04-19 12:54:46 +00:00
|
|
|
performing database queries as efficiently as possible,
|
|
|
|
to reduce database trips.
|
|
|
|
"""
|
|
|
|
|
2021-11-03 13:38:34 +00:00
|
|
|
# TODO: Update the "in_stock" annotation to include stock for variants of the part
|
|
|
|
# Ref: https://github.com/inventree/InvenTree/issues/2240
|
|
|
|
|
2020-09-05 12:35:19 +00:00
|
|
|
# Annotate with the total 'in stock' quantity
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
in_stock=Coalesce(
|
2020-09-05 13:35:48 +00:00
|
|
|
SubquerySum('stock_items__quantity', filter=StockItem.IN_STOCK_FILTER),
|
2021-04-20 00:59:28 +00:00
|
|
|
Decimal(0),
|
|
|
|
output_field=models.DecimalField(),
|
2020-09-05 12:35:19 +00:00
|
|
|
),
|
|
|
|
)
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2020-09-05 13:28:54 +00:00
|
|
|
# Annotate with the total number of stock items
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
stock_item_count=SubqueryCount('stock_items')
|
|
|
|
)
|
|
|
|
|
2020-04-19 12:54:46 +00:00
|
|
|
# Filter to limit builds to "active"
|
2020-09-05 12:35:19 +00:00
|
|
|
build_filter = Q(
|
|
|
|
status__in=BuildStatus.ACTIVE_CODES
|
|
|
|
)
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2020-09-05 12:35:19 +00:00
|
|
|
# Annotate with the total 'building' quantity
|
2020-04-19 12:54:46 +00:00
|
|
|
queryset = queryset.annotate(
|
|
|
|
building=Coalesce(
|
2020-09-05 12:35:19 +00:00
|
|
|
SubquerySum('builds__quantity', filter=build_filter),
|
|
|
|
Decimal(0),
|
2021-04-20 00:59:28 +00:00
|
|
|
output_field=models.DecimalField(),
|
2020-09-05 12:35:19 +00:00
|
|
|
)
|
|
|
|
)
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2020-09-05 12:35:19 +00:00
|
|
|
# Filter to limit orders to "open"
|
|
|
|
order_filter = Q(
|
|
|
|
order__status__in=PurchaseOrderStatus.OPEN
|
|
|
|
)
|
|
|
|
|
|
|
|
# Annotate with the total 'on order' quantity
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
ordering=Coalesce(
|
|
|
|
SubquerySum('supplier_parts__purchase_order_line_items__quantity', filter=order_filter),
|
|
|
|
Decimal(0),
|
2021-04-20 00:59:28 +00:00
|
|
|
output_field=models.DecimalField(),
|
2020-09-05 12:35:19 +00:00
|
|
|
) - Coalesce(
|
|
|
|
SubquerySum('supplier_parts__purchase_order_line_items__received', filter=order_filter),
|
|
|
|
Decimal(0),
|
2021-04-20 00:59:28 +00:00
|
|
|
output_field=models.DecimalField(),
|
2020-04-19 12:54:46 +00:00
|
|
|
)
|
|
|
|
)
|
2021-02-22 07:44:31 +00:00
|
|
|
|
|
|
|
# Annotate with the number of 'suppliers'
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
suppliers=Coalesce(
|
|
|
|
SubqueryCount('supplier_parts'),
|
|
|
|
Decimal(0),
|
2021-04-20 00:59:28 +00:00
|
|
|
output_field=models.DecimalField(),
|
2021-02-22 07:44:31 +00:00
|
|
|
),
|
|
|
|
)
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2019-05-19 22:13:22 +00:00
|
|
|
return queryset
|
|
|
|
|
2020-04-19 13:50:41 +00:00
|
|
|
def get_starred(self, part):
|
|
|
|
"""
|
|
|
|
Return "true" if the part is starred by the current user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return part in self.starred_parts
|
|
|
|
|
|
|
|
# Extra detail for the category
|
|
|
|
category_detail = CategorySerializer(source='category', many=False, read_only=True)
|
|
|
|
|
|
|
|
# Calculated fields
|
2020-04-19 12:54:46 +00:00
|
|
|
in_stock = serializers.FloatField(read_only=True)
|
|
|
|
ordering = serializers.FloatField(read_only=True)
|
|
|
|
building = serializers.FloatField(read_only=True)
|
2020-09-05 13:28:54 +00:00
|
|
|
stock_item_count = serializers.IntegerField(read_only=True)
|
2021-02-22 07:44:31 +00:00
|
|
|
suppliers = serializers.IntegerField(read_only=True)
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2021-06-23 01:40:37 +00:00
|
|
|
image = InvenTreeImageSerializerField(required=False, allow_null=True)
|
2020-04-19 12:54:46 +00:00
|
|
|
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
2021-06-24 21:36:37 +00:00
|
|
|
starred = serializers.SerializerMethodField()
|
2020-04-19 12:54:46 +00:00
|
|
|
|
2020-08-15 11:20:31 +00:00
|
|
|
# PrimaryKeyRelated fields (Note: enforcing field type here results in much faster queries, somehow...)
|
|
|
|
category = serializers.PrimaryKeyRelatedField(queryset=PartCategory.objects.all())
|
|
|
|
|
2020-04-19 13:50:41 +00:00
|
|
|
# TODO - Include annotation for the following fields:
|
|
|
|
# allocated_stock = serializers.FloatField(source='allocation_count', read_only=True)
|
|
|
|
# bom_items = serializers.IntegerField(source='bom_count', read_only=True)
|
|
|
|
# used_in = serializers.IntegerField(source='used_in_count', read_only=True)
|
2020-04-15 13:41:16 +00:00
|
|
|
|
2017-04-10 23:41:03 +00:00
|
|
|
class Meta:
|
|
|
|
model = Part
|
2019-04-28 13:40:26 +00:00
|
|
|
partial = True
|
2018-04-23 11:10:13 +00:00
|
|
|
fields = [
|
2020-04-05 05:46:18 +00:00
|
|
|
'active',
|
2020-04-19 13:50:41 +00:00
|
|
|
# 'allocated_stock',
|
2020-04-05 05:46:18 +00:00
|
|
|
'assembly',
|
2020-04-19 13:50:41 +00:00
|
|
|
# 'bom_items',
|
2019-05-14 07:23:20 +00:00
|
|
|
'category',
|
2020-04-19 13:50:41 +00:00
|
|
|
'category_detail',
|
2020-04-05 05:46:18 +00:00
|
|
|
'component',
|
2021-01-04 13:37:42 +00:00
|
|
|
'default_expiry',
|
2021-07-02 07:11:07 +00:00
|
|
|
'default_location',
|
|
|
|
'default_supplier',
|
|
|
|
'description',
|
2019-05-12 02:16:04 +00:00
|
|
|
'full_name',
|
2020-04-05 05:46:18 +00:00
|
|
|
'image',
|
2020-04-19 12:54:46 +00:00
|
|
|
'in_stock',
|
|
|
|
'ordering',
|
|
|
|
'building',
|
2018-04-23 11:10:13 +00:00
|
|
|
'IPN',
|
2019-05-25 14:29:17 +00:00
|
|
|
'is_template',
|
2019-05-14 07:23:20 +00:00
|
|
|
'keywords',
|
2020-04-05 09:18:32 +00:00
|
|
|
'link',
|
2020-04-19 13:50:41 +00:00
|
|
|
'minimum_stock',
|
2020-04-05 05:46:18 +00:00
|
|
|
'name',
|
|
|
|
'notes',
|
|
|
|
'pk',
|
2019-06-18 09:24:10 +00:00
|
|
|
'purchaseable',
|
2020-04-19 13:50:41 +00:00
|
|
|
'revision',
|
2018-04-23 11:10:13 +00:00
|
|
|
'salable',
|
2020-04-19 13:50:41 +00:00
|
|
|
'starred',
|
2020-09-05 13:28:54 +00:00
|
|
|
'stock_item_count',
|
2021-02-22 07:44:31 +00:00
|
|
|
'suppliers',
|
2020-04-05 05:46:18 +00:00
|
|
|
'thumbnail',
|
|
|
|
'trackable',
|
|
|
|
'units',
|
2020-04-19 13:50:41 +00:00
|
|
|
# 'used_in',
|
2020-04-05 05:46:18 +00:00
|
|
|
'variant_of',
|
2019-06-18 09:24:10 +00:00
|
|
|
'virtual',
|
2018-04-23 11:10:13 +00:00
|
|
|
]
|
2018-05-02 13:42:57 +00:00
|
|
|
|
|
|
|
|
2021-11-25 03:42:44 +00:00
|
|
|
class PartRelationSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for a PartRelated model
|
|
|
|
"""
|
|
|
|
|
|
|
|
part_1_detail = PartSerializer(source='part_1', read_only=True, many=False)
|
2021-11-25 03:45:28 +00:00
|
|
|
part_2_detail = PartSerializer(source='part_2', read_only=True, many=False)
|
2021-11-25 03:42:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartRelated
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part_1',
|
|
|
|
'part_1_detail',
|
|
|
|
'part_2',
|
|
|
|
'part_2_detail',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-05-04 23:05:44 +00:00
|
|
|
class PartStarSerializer(InvenTreeModelSerializer):
|
|
|
|
""" Serializer for a PartStar object """
|
|
|
|
|
2019-05-12 02:16:04 +00:00
|
|
|
partname = serializers.CharField(source='part.full_name', read_only=True)
|
2019-05-04 23:05:44 +00:00
|
|
|
username = serializers.CharField(source='user.username', read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartStar
|
|
|
|
fields = [
|
2019-05-05 00:36:48 +00:00
|
|
|
'pk',
|
2019-05-04 23:05:44 +00:00
|
|
|
'part',
|
|
|
|
'partname',
|
|
|
|
'user',
|
|
|
|
'username',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-10-13 02:58:41 +00:00
|
|
|
class BomItemSubstituteSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for the BomItemSubstitute class
|
|
|
|
"""
|
|
|
|
|
|
|
|
part_detail = PartBriefSerializer(source='part', read_only=True, many=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = BomItemSubstitute
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'bom_item',
|
|
|
|
'part',
|
|
|
|
'part_detail',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-26 13:34:15 +00:00
|
|
|
class BomItemSerializer(InvenTreeModelSerializer):
|
2021-10-13 00:07:44 +00:00
|
|
|
"""
|
|
|
|
Serializer for BomItem object
|
|
|
|
"""
|
2018-05-02 13:42:57 +00:00
|
|
|
|
2021-06-27 11:48:08 +00:00
|
|
|
price_range = serializers.CharField(read_only=True)
|
2020-04-25 11:13:38 +00:00
|
|
|
|
2022-02-06 23:54:37 +00:00
|
|
|
quantity = InvenTreeDecimalField(required=True)
|
|
|
|
|
|
|
|
def validate_quantity(self, quantity):
|
|
|
|
if quantity <= 0:
|
|
|
|
raise serializers.ValidationError(_("Quantity must be greater than zero"))
|
2022-02-07 01:32:50 +00:00
|
|
|
|
2022-02-06 23:54:37 +00:00
|
|
|
return quantity
|
2020-08-15 11:52:32 +00:00
|
|
|
|
|
|
|
part = serializers.PrimaryKeyRelatedField(queryset=Part.objects.filter(assembly=True))
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-10-13 02:58:41 +00:00
|
|
|
substitutes = BomItemSubstituteSerializer(many=True, read_only=True)
|
2021-10-13 00:07:44 +00:00
|
|
|
|
2019-05-20 12:53:01 +00:00
|
|
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
2020-08-15 11:52:32 +00:00
|
|
|
|
|
|
|
sub_part = serializers.PrimaryKeyRelatedField(queryset=Part.objects.filter(component=True))
|
|
|
|
|
2019-04-25 14:29:53 +00:00
|
|
|
sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True)
|
2020-04-21 11:38:04 +00:00
|
|
|
|
2019-09-05 03:10:26 +00:00
|
|
|
validated = serializers.BooleanField(read_only=True, source='is_line_valid')
|
2018-05-02 13:42:57 +00:00
|
|
|
|
2021-12-12 11:18:29 +00:00
|
|
|
purchase_price_min = MoneyField(max_digits=19, decimal_places=4, read_only=True)
|
2021-05-13 19:47:42 +00:00
|
|
|
|
2021-12-12 11:18:29 +00:00
|
|
|
purchase_price_max = MoneyField(max_digits=19, decimal_places=4, read_only=True)
|
2021-11-22 23:28:23 +00:00
|
|
|
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_avg = serializers.SerializerMethodField()
|
2021-11-22 23:28:23 +00:00
|
|
|
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_range = serializers.SerializerMethodField()
|
2021-05-13 19:47:42 +00:00
|
|
|
|
2019-05-23 12:36:19 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2019-05-23 12:59:56 +00:00
|
|
|
# part_detail and sub_part_detail serializers are only included if requested.
|
2019-05-23 12:36:19 +00:00
|
|
|
# This saves a bunch of database requests
|
|
|
|
|
|
|
|
part_detail = kwargs.pop('part_detail', False)
|
|
|
|
sub_part_detail = kwargs.pop('sub_part_detail', False)
|
2021-10-08 01:47:03 +00:00
|
|
|
include_pricing = kwargs.pop('include_pricing', False)
|
2019-05-23 12:36:19 +00:00
|
|
|
|
|
|
|
super(BomItemSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if part_detail is not True:
|
|
|
|
self.fields.pop('part_detail')
|
|
|
|
|
|
|
|
if sub_part_detail is not True:
|
|
|
|
self.fields.pop('sub_part_detail')
|
|
|
|
|
2021-10-08 01:47:03 +00:00
|
|
|
if not include_pricing:
|
|
|
|
# Remove all pricing related fields
|
|
|
|
self.fields.pop('price_range')
|
|
|
|
self.fields.pop('purchase_price_min')
|
|
|
|
self.fields.pop('purchase_price_max')
|
|
|
|
self.fields.pop('purchase_price_avg')
|
|
|
|
self.fields.pop('purchase_price_range')
|
|
|
|
|
2019-05-19 22:31:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def setup_eager_loading(queryset):
|
2019-05-20 14:16:00 +00:00
|
|
|
queryset = queryset.prefetch_related('part')
|
|
|
|
queryset = queryset.prefetch_related('part__category')
|
|
|
|
queryset = queryset.prefetch_related('part__stock_items')
|
2020-08-15 11:52:32 +00:00
|
|
|
|
2019-05-19 22:31:03 +00:00
|
|
|
queryset = queryset.prefetch_related('sub_part')
|
|
|
|
queryset = queryset.prefetch_related('sub_part__category')
|
2019-05-20 12:53:01 +00:00
|
|
|
queryset = queryset.prefetch_related('sub_part__stock_items')
|
2019-05-21 05:42:52 +00:00
|
|
|
queryset = queryset.prefetch_related('sub_part__supplier_parts__pricebreaks')
|
2019-05-19 22:31:03 +00:00
|
|
|
return queryset
|
|
|
|
|
2021-05-14 20:16:23 +00:00
|
|
|
def get_purchase_price_range(self, obj):
|
|
|
|
""" Return purchase price range """
|
|
|
|
|
2021-05-14 20:59:59 +00:00
|
|
|
try:
|
|
|
|
purchase_price_min = obj.purchase_price_min
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
purchase_price_max = obj.purchase_price_max
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if purchase_price_min and not purchase_price_max:
|
2021-05-14 20:16:23 +00:00
|
|
|
# Get price range
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_range = str(purchase_price_max)
|
|
|
|
elif not purchase_price_min and purchase_price_max:
|
2021-05-14 20:16:23 +00:00
|
|
|
# Get price range
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_range = str(purchase_price_max)
|
|
|
|
elif purchase_price_min and purchase_price_max:
|
2021-05-14 20:16:23 +00:00
|
|
|
# Get price range
|
2021-05-14 20:59:59 +00:00
|
|
|
if purchase_price_min >= purchase_price_max:
|
2021-05-14 20:16:23 +00:00
|
|
|
# If min > max: use min only
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_range = str(purchase_price_min)
|
2021-05-14 20:16:23 +00:00
|
|
|
else:
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_range = str(purchase_price_min) + " - " + str(purchase_price_max)
|
2021-05-14 20:16:23 +00:00
|
|
|
else:
|
|
|
|
purchase_price_range = '-'
|
|
|
|
|
|
|
|
return purchase_price_range
|
|
|
|
|
|
|
|
def get_purchase_price_avg(self, obj):
|
|
|
|
""" Return purchase price average """
|
2021-11-22 23:28:23 +00:00
|
|
|
|
2021-05-14 20:59:59 +00:00
|
|
|
try:
|
|
|
|
purchase_price_avg = obj.purchase_price_avg
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if purchase_price_avg:
|
2021-05-14 20:16:23 +00:00
|
|
|
# Get string representation of price average
|
2021-05-14 20:59:59 +00:00
|
|
|
purchase_price_avg = str(purchase_price_avg)
|
2021-05-14 20:16:23 +00:00
|
|
|
else:
|
|
|
|
purchase_price_avg = '-'
|
|
|
|
|
|
|
|
return purchase_price_avg
|
|
|
|
|
2018-05-02 13:42:57 +00:00
|
|
|
class Meta:
|
|
|
|
model = BomItem
|
|
|
|
fields = [
|
2021-06-01 03:59:01 +00:00
|
|
|
'allow_variants',
|
2021-02-17 10:53:26 +00:00
|
|
|
'inherited',
|
|
|
|
'note',
|
|
|
|
'optional',
|
|
|
|
'overage',
|
2018-05-02 13:42:57 +00:00
|
|
|
'pk',
|
|
|
|
'part',
|
2019-05-20 12:53:01 +00:00
|
|
|
'part_detail',
|
2021-06-01 03:59:01 +00:00
|
|
|
'purchase_price_avg',
|
|
|
|
'purchase_price_max',
|
|
|
|
'purchase_price_min',
|
|
|
|
'purchase_price_range',
|
2019-04-14 12:20:11 +00:00
|
|
|
'quantity',
|
2019-06-27 13:57:21 +00:00
|
|
|
'reference',
|
2021-02-17 10:53:26 +00:00
|
|
|
'sub_part',
|
|
|
|
'sub_part_detail',
|
2021-10-13 00:07:44 +00:00
|
|
|
'substitutes',
|
2021-06-27 11:48:08 +00:00
|
|
|
'price_range',
|
2019-09-05 03:10:26 +00:00
|
|
|
'validated',
|
2018-05-02 14:47:03 +00:00
|
|
|
]
|
2019-09-07 09:44:10 +00:00
|
|
|
|
|
|
|
|
2021-07-08 15:56:04 +00:00
|
|
|
class PartParameterTemplateSerializer(InvenTreeModelSerializer):
|
|
|
|
""" JSON serializer for the PartParameterTemplate model """
|
2019-09-07 09:44:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-07-08 15:56:04 +00:00
|
|
|
model = PartParameterTemplate
|
2019-09-07 09:44:10 +00:00
|
|
|
fields = [
|
|
|
|
'pk',
|
2021-07-08 15:56:04 +00:00
|
|
|
'name',
|
|
|
|
'units',
|
2019-09-07 09:44:10 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-07-08 15:56:04 +00:00
|
|
|
class PartParameterSerializer(InvenTreeModelSerializer):
|
|
|
|
""" JSON serializers for the PartParameter model """
|
|
|
|
|
|
|
|
template_detail = PartParameterTemplateSerializer(source='template', many=False, read_only=True)
|
2019-09-07 09:44:10 +00:00
|
|
|
|
|
|
|
class Meta:
|
2021-07-08 15:56:04 +00:00
|
|
|
model = PartParameter
|
2019-09-07 09:44:10 +00:00
|
|
|
fields = [
|
|
|
|
'pk',
|
2021-07-08 15:56:04 +00:00
|
|
|
'part',
|
|
|
|
'template',
|
|
|
|
'template_detail',
|
|
|
|
'data'
|
2019-09-07 09:44:10 +00:00
|
|
|
]
|
2020-10-31 14:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CategoryParameterTemplateSerializer(InvenTreeModelSerializer):
|
|
|
|
""" Serializer for PartCategoryParameterTemplate """
|
|
|
|
|
2020-11-16 21:10:00 +00:00
|
|
|
parameter_template = PartParameterTemplateSerializer(many=False,
|
|
|
|
read_only=True)
|
2020-10-31 14:18:33 +00:00
|
|
|
|
2021-07-28 04:05:49 +00:00
|
|
|
category_detail = CategorySerializer(source='category', many=False, read_only=True)
|
|
|
|
|
2020-10-31 14:18:33 +00:00
|
|
|
class Meta:
|
|
|
|
model = PartCategoryParameterTemplate
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'category',
|
2021-07-28 04:05:49 +00:00
|
|
|
'category_detail',
|
2020-10-31 14:18:33 +00:00
|
|
|
'parameter_template',
|
|
|
|
'default_value',
|
|
|
|
]
|
2021-12-21 06:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PartCopyBOMSerializer(serializers.Serializer):
|
|
|
|
"""
|
|
|
|
Serializer for copying a BOM from another part
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = [
|
|
|
|
'part',
|
|
|
|
'remove_existing',
|
|
|
|
]
|
|
|
|
|
|
|
|
part = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Part.objects.all(),
|
|
|
|
many=False,
|
|
|
|
required=True,
|
|
|
|
allow_null=False,
|
|
|
|
label=_('Part'),
|
|
|
|
help_text=_('Select part to copy BOM from'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def validate_part(self, part):
|
|
|
|
"""
|
|
|
|
Check that a 'valid' part was selected
|
|
|
|
"""
|
|
|
|
|
|
|
|
return part
|
|
|
|
|
|
|
|
remove_existing = serializers.BooleanField(
|
|
|
|
label=_('Remove Existing Data'),
|
2021-12-21 11:07:08 +00:00
|
|
|
help_text=_('Remove existing BOM items before copying'),
|
|
|
|
default=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
include_inherited = serializers.BooleanField(
|
|
|
|
label=_('Include Inherited'),
|
|
|
|
help_text=_('Include BOM items which are inherited from templated parts'),
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
skip_invalid = serializers.BooleanField(
|
|
|
|
label=_('Skip Invalid Rows'),
|
|
|
|
help_text=_('Enable this option to skip invalid rows'),
|
|
|
|
default=False,
|
2021-12-21 06:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Actually duplicate the BOM
|
|
|
|
"""
|
|
|
|
|
|
|
|
base_part = self.context['part']
|
|
|
|
|
|
|
|
data = self.validated_data
|
|
|
|
|
2021-12-21 11:07:08 +00:00
|
|
|
base_part.copy_bom_from(
|
|
|
|
data['part'],
|
|
|
|
clear=data.get('remove_existing', True),
|
|
|
|
skip_invalid=data.get('skip_invalid', False),
|
|
|
|
include_inherited=data.get('include_inherited', False),
|
|
|
|
)
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BomExtractSerializer(serializers.Serializer):
|
|
|
|
"""
|
|
|
|
Serializer for uploading a file and extracting data from it.
|
|
|
|
|
|
|
|
Note: 2022-02-04 - This needs a *serious* refactor in future, probably
|
|
|
|
|
|
|
|
When parsing the file, the following things happen:
|
|
|
|
|
|
|
|
a) Check file format and validity
|
|
|
|
b) Look for "required" fields
|
|
|
|
c) Look for "part" fields - used to "infer" part
|
|
|
|
|
|
|
|
Once the file itself has been validated, we iterate through each data row:
|
|
|
|
|
|
|
|
- If the "level" column is provided, ignore anything below level 1
|
|
|
|
- Try to "guess" the part based on part_id / part_name / part_ipn
|
|
|
|
- Extract other fields as required
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2022-02-07 01:20:18 +00:00
|
|
|
class Meta:
|
|
|
|
fields = [
|
|
|
|
'bom_file',
|
|
|
|
'part',
|
|
|
|
'clear_existing',
|
|
|
|
]
|
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
# These columns must be present
|
|
|
|
REQUIRED_COLUMNS = [
|
|
|
|
'quantity',
|
|
|
|
]
|
|
|
|
|
|
|
|
# We need at least one column to specify a "part"
|
|
|
|
PART_COLUMNS = [
|
|
|
|
'part',
|
|
|
|
'part_id',
|
|
|
|
'part_name',
|
|
|
|
'part_ipn',
|
|
|
|
]
|
|
|
|
|
|
|
|
# These columns are "optional"
|
|
|
|
OPTIONAL_COLUMNS = [
|
|
|
|
'allow_variants',
|
|
|
|
'inherited',
|
|
|
|
'optional',
|
|
|
|
'overage',
|
|
|
|
'note',
|
|
|
|
'reference',
|
|
|
|
]
|
|
|
|
|
|
|
|
def find_matching_column(self, col_name, columns):
|
|
|
|
|
|
|
|
# Direct match
|
|
|
|
if col_name in columns:
|
|
|
|
return col_name
|
|
|
|
|
|
|
|
col_name = col_name.lower().strip()
|
|
|
|
|
|
|
|
for col in columns:
|
|
|
|
if col.lower().strip() == col_name:
|
|
|
|
return col
|
|
|
|
|
|
|
|
# No match
|
|
|
|
return None
|
|
|
|
|
2022-02-04 14:26:08 +00:00
|
|
|
def find_matching_data(self, row, col_name, columns):
|
|
|
|
"""
|
|
|
|
Extract data from the row, based on the "expected" column name
|
|
|
|
"""
|
|
|
|
|
|
|
|
col_name = self.find_matching_column(col_name, columns)
|
|
|
|
|
|
|
|
return row.get(col_name, None)
|
|
|
|
|
2022-02-04 13:12:11 +00:00
|
|
|
bom_file = serializers.FileField(
|
|
|
|
label=_("BOM File"),
|
|
|
|
help_text=_("Select Bill of Materials file"),
|
|
|
|
required=True,
|
|
|
|
allow_empty_file=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def validate_bom_file(self, bom_file):
|
|
|
|
"""
|
|
|
|
Perform validation checks on the uploaded BOM file
|
|
|
|
"""
|
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
self.filename = bom_file.name
|
|
|
|
|
2022-02-04 13:12:11 +00:00
|
|
|
name, ext = os.path.splitext(bom_file.name)
|
|
|
|
|
|
|
|
# Remove the leading . from the extension
|
|
|
|
ext = ext[1:]
|
|
|
|
|
|
|
|
accepted_file_types = [
|
|
|
|
'xls', 'xlsx',
|
|
|
|
'csv', 'tsv',
|
|
|
|
'xml',
|
|
|
|
]
|
|
|
|
|
|
|
|
if ext not in accepted_file_types:
|
|
|
|
raise serializers.ValidationError(_("Unsupported file type"))
|
|
|
|
|
|
|
|
# Impose a 50MB limit on uploaded BOM files
|
|
|
|
max_upload_file_size = 50 * 1024 * 1024
|
|
|
|
|
|
|
|
if bom_file.size > max_upload_file_size:
|
|
|
|
raise serializers.ValidationError(_("File is too large"))
|
|
|
|
|
|
|
|
# Read file data into memory (bytes object)
|
2022-02-08 21:31:08 +00:00
|
|
|
try:
|
|
|
|
data = bom_file.read()
|
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError(str(e))
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
if ext in ['csv', 'tsv', 'xml']:
|
2022-02-08 21:31:08 +00:00
|
|
|
try:
|
|
|
|
data = data.decode()
|
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError(str(e))
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
# Convert to a tablib dataset (we expect headers)
|
2022-02-08 21:31:08 +00:00
|
|
|
try:
|
|
|
|
self.dataset = tablib.Dataset().load(data, ext, headers=True)
|
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError(str(e))
|
2022-02-04 13:12:11 +00:00
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
for header in self.REQUIRED_COLUMNS:
|
2022-02-04 13:12:11 +00:00
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
match = self.find_matching_column(header, self.dataset.headers)
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
if match is None:
|
|
|
|
raise serializers.ValidationError(_("Missing required column") + f": '{header}'")
|
|
|
|
|
|
|
|
part_column_matches = {}
|
|
|
|
|
|
|
|
part_match = False
|
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
for col in self.PART_COLUMNS:
|
|
|
|
col_match = self.find_matching_column(col, self.dataset.headers)
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
part_column_matches[col] = col_match
|
|
|
|
|
|
|
|
if col_match is not None:
|
|
|
|
part_match = True
|
|
|
|
|
|
|
|
if not part_match:
|
|
|
|
raise serializers.ValidationError(_("No part column found"))
|
|
|
|
|
2022-02-09 00:27:51 +00:00
|
|
|
if len(self.dataset) == 0:
|
|
|
|
raise serializers.ValidationError(_("No data rows found"))
|
|
|
|
|
2022-02-04 13:12:11 +00:00
|
|
|
return bom_file
|
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
def extract_data(self):
|
|
|
|
"""
|
|
|
|
Read individual rows out of the BOM file
|
|
|
|
"""
|
|
|
|
|
|
|
|
rows = []
|
2022-02-09 00:30:58 +00:00
|
|
|
errors = []
|
2022-02-04 13:30:00 +00:00
|
|
|
|
2022-02-09 00:34:25 +00:00
|
|
|
found_parts = set()
|
|
|
|
|
2022-02-04 14:26:08 +00:00
|
|
|
headers = self.dataset.headers
|
|
|
|
|
|
|
|
level_column = self.find_matching_column('level', headers)
|
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
for row in self.dataset.dict:
|
2022-02-04 14:26:08 +00:00
|
|
|
|
2022-02-09 12:02:09 +00:00
|
|
|
row_error = {}
|
2022-02-09 00:30:58 +00:00
|
|
|
|
2022-02-04 14:26:08 +00:00
|
|
|
"""
|
|
|
|
If the "level" column is specified, and this is not a top-level BOM item, ignore the row!
|
|
|
|
"""
|
|
|
|
if level_column is not None:
|
|
|
|
level = row.get('level', None)
|
|
|
|
|
|
|
|
if level is not None:
|
|
|
|
try:
|
|
|
|
level = int(level)
|
|
|
|
if level != 1:
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
"""
|
|
|
|
Next, we try to "guess" the part, based on the provided data.
|
|
|
|
|
|
|
|
A) If the part_id is supplied, use that!
|
|
|
|
B) If the part name and/or part_ipn are supplied, maybe we can use those?
|
|
|
|
"""
|
|
|
|
part_id = self.find_matching_data(row, 'part_id', headers)
|
|
|
|
part_name = self.find_matching_data(row, 'part_name', headers)
|
|
|
|
part_ipn = self.find_matching_data(row, 'part_ipn', headers)
|
|
|
|
|
|
|
|
part = None
|
|
|
|
|
|
|
|
if part_id is not None:
|
|
|
|
try:
|
|
|
|
part = Part.objects.get(pk=part_id)
|
|
|
|
except (ValueError, Part.DoesNotExist):
|
|
|
|
pass
|
|
|
|
|
2022-02-06 21:37:39 +00:00
|
|
|
# Optionally, specify using field "part"
|
|
|
|
if part is None:
|
|
|
|
pk = self.find_matching_data(row, 'part', headers)
|
|
|
|
|
|
|
|
if pk is not None:
|
|
|
|
try:
|
|
|
|
part = Part.objects.get(pk=pk)
|
|
|
|
except (ValueError, Part.DoesNotExist):
|
|
|
|
pass
|
|
|
|
|
2022-02-04 14:26:08 +00:00
|
|
|
if part is None:
|
|
|
|
|
2022-02-09 12:26:13 +00:00
|
|
|
if part_name or part_ipn:
|
2022-02-04 14:26:08 +00:00
|
|
|
queryset = Part.objects.all()
|
|
|
|
|
2022-02-09 12:26:13 +00:00
|
|
|
if part_name:
|
2022-02-04 14:26:08 +00:00
|
|
|
queryset = queryset.filter(name=part_name)
|
|
|
|
|
2022-02-09 12:26:13 +00:00
|
|
|
if part_ipn:
|
2022-02-04 14:26:08 +00:00
|
|
|
queryset = queryset.filter(IPN=part_ipn)
|
|
|
|
|
|
|
|
# Only if we have a single direct match
|
2022-02-09 00:30:58 +00:00
|
|
|
if queryset.exists():
|
|
|
|
if queryset.count() == 1:
|
|
|
|
part = queryset.first()
|
|
|
|
else:
|
|
|
|
# Multiple matches!
|
2022-02-09 12:02:09 +00:00
|
|
|
row_error['part'] = _('Multiple matching parts found')
|
2022-02-09 00:31:36 +00:00
|
|
|
|
2022-02-09 00:34:25 +00:00
|
|
|
if part is None:
|
2022-02-09 12:02:09 +00:00
|
|
|
if 'part' not in row_error:
|
|
|
|
row_error['part'] = _('No matching part found')
|
2022-02-09 00:34:25 +00:00
|
|
|
else:
|
|
|
|
if part.pk in found_parts:
|
2022-02-09 12:02:09 +00:00
|
|
|
row_error['part'] = _("Duplicate part selected")
|
|
|
|
|
|
|
|
elif not part.component:
|
|
|
|
row_error['part'] = _('Part is not designated as a component')
|
|
|
|
|
|
|
|
found_parts.add(part.pk)
|
2022-02-04 14:26:08 +00:00
|
|
|
|
|
|
|
row['part'] = part.pk if part is not None else None
|
|
|
|
|
2022-02-09 12:02:09 +00:00
|
|
|
"""
|
|
|
|
Read out the 'quantity' column - check that it is valid
|
|
|
|
"""
|
|
|
|
quantity = self.find_matching_data(row, 'quantity', self.dataset.headers)
|
|
|
|
|
2022-02-14 22:11:14 +00:00
|
|
|
# Ensure quantity field is provided
|
|
|
|
row['quantity'] = quantity
|
|
|
|
|
2022-02-09 12:02:09 +00:00
|
|
|
if quantity is None:
|
|
|
|
row_error['quantity'] = _('Quantity not provided')
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
quantity = Decimal(quantity)
|
|
|
|
|
|
|
|
if quantity <= 0:
|
|
|
|
row_error['quantity'] = _('Quantity must be greater than zero')
|
|
|
|
except:
|
|
|
|
row_error['quantity'] = _('Invalid quantity')
|
|
|
|
|
2022-02-09 00:27:51 +00:00
|
|
|
# For each "optional" column, ensure the column names are allocated correctly
|
|
|
|
for field_name in self.OPTIONAL_COLUMNS:
|
|
|
|
if field_name not in row:
|
|
|
|
row[field_name] = self.find_matching_data(row, field_name, self.dataset.headers)
|
2022-02-09 00:31:36 +00:00
|
|
|
|
2022-02-04 13:30:00 +00:00
|
|
|
rows.append(row)
|
2022-02-09 12:02:09 +00:00
|
|
|
errors.append(row_error)
|
2022-02-04 13:30:00 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'rows': rows,
|
2022-02-09 00:30:58 +00:00
|
|
|
'errors': errors,
|
2022-02-04 14:26:08 +00:00
|
|
|
'headers': headers,
|
2022-02-04 13:30:00 +00:00
|
|
|
'filename': self.filename,
|
|
|
|
}
|
|
|
|
|
2022-02-07 01:20:18 +00:00
|
|
|
part = serializers.PrimaryKeyRelatedField(queryset=Part.objects.filter(assembly=True), required=True)
|
|
|
|
|
|
|
|
clear_existing = serializers.BooleanField(
|
|
|
|
label=_("Clear Existing BOM"),
|
|
|
|
help_text=_("Delete existing BOM data first"),
|
|
|
|
)
|
2022-02-04 13:12:11 +00:00
|
|
|
|
|
|
|
def save(self):
|
2022-02-07 01:32:50 +00:00
|
|
|
|
2022-02-07 01:20:18 +00:00
|
|
|
data = self.validated_data
|
|
|
|
|
|
|
|
master_part = data['part']
|
|
|
|
clear_existing = data['clear_existing']
|
|
|
|
|
|
|
|
if clear_existing:
|
|
|
|
|
|
|
|
# Remove all existing BOM items
|
|
|
|
master_part.bom_items.all().delete()
|
2022-02-06 23:54:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
|
|
|
|
items = data['items']
|
|
|
|
|
|
|
|
if len(items) == 0:
|
|
|
|
raise serializers.ValidationError(_("At least one BOM item is required"))
|
|
|
|
|
2022-02-07 01:20:18 +00:00
|
|
|
data = super().validate(data)
|
|
|
|
|
2022-02-06 23:54:37 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
|
|
|
|
data = self.validated_data
|
|
|
|
|
|
|
|
items = data['items']
|
|
|
|
|
2022-02-07 00:35:07 +00:00
|
|
|
try:
|
|
|
|
with transaction.atomic():
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
|
|
|
|
part = item['part']
|
|
|
|
sub_part = item['sub_part']
|
|
|
|
|
|
|
|
# Ignore duplicate BOM items
|
|
|
|
if BomItem.objects.filter(part=part, sub_part=sub_part).exists():
|
|
|
|
continue
|
2022-02-06 23:54:37 +00:00
|
|
|
|
2022-02-07 00:35:07 +00:00
|
|
|
# Create a new BomItem object
|
|
|
|
BomItem.objects.create(**item)
|
2022-02-07 01:32:50 +00:00
|
|
|
|
2022-02-07 00:35:07 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError(detail=serializers.as_serializer_error(e))
|