2019-04-27 12:18:07 +00:00
|
|
|
"""
|
|
|
|
JSON serializers for Part app
|
|
|
|
"""
|
|
|
|
|
2017-03-28 11:12:02 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2019-05-05 01:44:23 +00:00
|
|
|
from .models import Part, PartStar
|
2019-05-18 08:04:25 +00:00
|
|
|
|
2019-05-04 23:05:44 +00:00
|
|
|
from .models import PartCategory
|
|
|
|
from .models import BomItem
|
2019-09-07 09:44:10 +00:00
|
|
|
from .models import PartParameter, PartParameterTemplate
|
2018-05-02 13:08:45 +00:00
|
|
|
|
2020-04-19 12:54:46 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2020-04-19 13:56:16 +00:00
|
|
|
from django.db.models import Q, Sum
|
2020-04-19 12:54:46 +00:00
|
|
|
from django.db.models.functions import Coalesce
|
|
|
|
|
|
|
|
from InvenTree.status_codes import StockStatus, OrderStatus, BuildStatus
|
2019-04-26 13:34:15 +00:00
|
|
|
from InvenTree.serializers import InvenTreeModelSerializer
|
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = PartCategory
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'name',
|
2018-05-02 13:42:57 +00:00
|
|
|
'description',
|
2018-05-02 13:08:45 +00:00
|
|
|
'pathstring',
|
|
|
|
'url',
|
2018-05-04 13:54:57 +00:00
|
|
|
'parent',
|
2020-04-02 22:15:09 +00:00
|
|
|
'parts',
|
2018-05-02 13:08:45 +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)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
2020-04-06 02:57:04 +00:00
|
|
|
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
2019-05-20 13:26:27 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def setup_eager_loading(queryset):
|
|
|
|
queryset = queryset.prefetch_related('category')
|
|
|
|
queryset = queryset.prefetch_related('stock_items')
|
|
|
|
queryset = queryset.prefetch_related('bom_items')
|
|
|
|
queryset = queryset.prefetch_related('builds')
|
|
|
|
return queryset
|
2019-05-12 02:16:04 +00:00
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = Part
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'url',
|
2019-05-12 02:16:04 +00:00
|
|
|
'full_name',
|
2018-05-02 13:42:57 +00:00
|
|
|
'description',
|
2019-05-23 11:51:27 +00:00
|
|
|
'total_stock',
|
2019-04-12 12:09:50 +00:00
|
|
|
'available_stock',
|
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',
|
|
|
|
'virtual',
|
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
|
|
|
|
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')
|
|
|
|
|
2020-04-19 12:54:46 +00:00
|
|
|
@staticmethod
|
|
|
|
def prefetch_queryset(queryset):
|
2020-04-19 13:50:41 +00:00
|
|
|
"""
|
|
|
|
Prefetch related database tables,
|
|
|
|
to reduce database hits.
|
|
|
|
"""
|
|
|
|
|
2020-04-19 12:54:46 +00:00
|
|
|
return queryset.prefetch_related(
|
|
|
|
'category',
|
|
|
|
'stock_items',
|
|
|
|
'bom_items',
|
|
|
|
'builds',
|
|
|
|
'supplier_parts',
|
|
|
|
'supplier_parts__purchase_order_line_items',
|
2020-04-19 13:50:41 +00:00
|
|
|
'supplier_parts__purcahes_order_line_items__order',
|
|
|
|
'starred_users',
|
|
|
|
'starred_user__user',
|
|
|
|
'starred_user__part',
|
2020-04-19 12:54:46 +00:00
|
|
|
)
|
2019-06-13 08:33:15 +00:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Filter to limit stock items to "available"
|
|
|
|
stock_filter = Q(stock_items__status__in=StockStatus.AVAILABLE_CODES)
|
|
|
|
|
|
|
|
# Filter to limit orders to "open"
|
|
|
|
order_filter = Q(supplier_parts__purchase_order_line_items__order__status__in=OrderStatus.OPEN)
|
|
|
|
|
|
|
|
# Filter to limit builds to "active"
|
|
|
|
build_filter = Q(builds__status__in=BuildStatus.ACTIVE_CODES)
|
|
|
|
|
|
|
|
# Annotate the number total stock count
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
in_stock=Coalesce(Sum('stock_items__quantity', filter=stock_filter, distinct=True), Decimal(0))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Annotate the number of parts "on order"
|
|
|
|
# Total "on order" parts = "Quantity" - "Received" for each active purchase order
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
ordering=Coalesce(Sum(
|
|
|
|
'supplier_parts__purchase_order_line_items__quantity',
|
|
|
|
filter=order_filter,
|
|
|
|
distinct=True
|
|
|
|
), Decimal(0)) - Coalesce(Sum(
|
|
|
|
'supplier_parts__purchase_order_line_items__received',
|
|
|
|
filter=order_filter,
|
|
|
|
distinct=True
|
|
|
|
), Decimal(0))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Annotate number of parts being build
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
building=Coalesce(
|
|
|
|
Sum('builds__quantity', filter=build_filter, distinct=True), Decimal(0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
image = serializers.CharField(source='get_image_url', read_only=True)
|
|
|
|
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
2020-04-19 13:50:41 +00:00
|
|
|
starred = serializers.SerializerMethodField()
|
2020-04-19 12:54:46 +00:00
|
|
|
|
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',
|
|
|
|
'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-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
|
|
|
|
|
|
|
|
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',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-04-26 13:34:15 +00:00
|
|
|
class BomItemSerializer(InvenTreeModelSerializer):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Serializer for BomItem object """
|
2018-05-02 13:42:57 +00:00
|
|
|
|
2019-05-20 12:53:01 +00:00
|
|
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
2019-04-25 14:29:53 +00:00
|
|
|
sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True)
|
2019-05-21 05:15:54 +00:00
|
|
|
price_range = serializers.CharField(read_only=True)
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
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')
|
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
|
|
|
|
|
2018-05-02 13:42:57 +00:00
|
|
|
class Meta:
|
|
|
|
model = BomItem
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
2019-05-20 12:53:01 +00:00
|
|
|
'part_detail',
|
2018-05-02 13:42:57 +00:00
|
|
|
'sub_part',
|
2019-04-25 14:29:53 +00:00
|
|
|
'sub_part_detail',
|
2019-04-14 12:20:11 +00:00
|
|
|
'quantity',
|
2019-06-27 13:57:21 +00:00
|
|
|
'reference',
|
2019-05-21 05:15:54 +00:00
|
|
|
'price_range',
|
2019-05-14 21:23:02 +00:00
|
|
|
'overage',
|
2019-04-14 12:20:11 +00:00
|
|
|
'note',
|
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
|
|
|
|
|
|
|
|
|
|
|
class PartParameterSerializer(InvenTreeModelSerializer):
|
|
|
|
""" JSON serializers for the PartParameter model """
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartParameter
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'template',
|
|
|
|
'data'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class PartParameterTemplateSerializer(InvenTreeModelSerializer):
|
|
|
|
""" JSON serializer for the PartParameterTemplate model """
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = PartParameterTemplate
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'name',
|
|
|
|
'units',
|
|
|
|
]
|