InvenTree/InvenTree/part/serializers.py

226 lines
6.6 KiB
Python
Raw Normal View History

2019-04-27 12:18:07 +00:00
"""
JSON serializers for Part app
"""
from rest_framework import serializers
2019-05-05 01:44:23 +00:00
from .models import Part, PartStar
from .models import PartCategory
from .models import BomItem
from .models import PartParameter, PartParameterTemplate
from InvenTree.serializers import InvenTreeModelSerializer
2019-04-13 23:25:46 +00:00
class CategorySerializer(InvenTreeModelSerializer):
2019-04-27 12:18:07 +00:00
""" Serializer for PartCategory """
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)
class Meta:
model = PartCategory
fields = [
'pk',
'name',
2018-05-02 13:42:57 +00:00
'description',
'pathstring',
'url',
2018-05-04 13:54:57 +00:00
'parent',
2020-04-02 22:15:09 +00:00
'parts',
]
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)
class PartBriefSerializer(InvenTreeModelSerializer):
2019-04-27 12:18:07 +00:00
""" Serializer for Part (brief detail) """
url = serializers.CharField(source='get_absolute_url', read_only=True)
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
@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
class Meta:
model = Part
fields = [
'pk',
'url',
'full_name',
2018-05-02 13:42:57 +00:00
'description',
'total_stock',
'available_stock',
'thumbnail',
'active',
'assembly',
'virtual',
]
2017-03-28 12:14:36 +00:00
2018-04-15 15:02:17 +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
allocated_stock = serializers.FloatField(source='allocation_count', read_only=True)
bom_items = serializers.IntegerField(source='bom_count', read_only=True)
building = serializers.FloatField(source='quantity_being_built', read_only=False)
category_name = serializers.CharField(source='category_path', read_only=True)
image = serializers.CharField(source='get_image_url', read_only=True)
on_order = serializers.FloatField(read_only=True)
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
url = serializers.CharField(source='get_absolute_url', read_only=True)
used_in = serializers.IntegerField(source='used_in_count', read_only=True)
2019-06-13 08:33:15 +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
2017-04-10 23:41:03 +00:00
class Meta:
model = Part
partial = True
fields = [
2020-04-05 05:46:18 +00:00
'active',
'allocated_stock',
'assembly',
'bom_items',
2020-04-05 05:59:48 +00:00
'building',
'category',
'category_name',
2020-04-05 05:46:18 +00:00
'component',
'description',
'full_name',
2020-04-05 05:46:18 +00:00
'image',
'IPN',
'is_template',
'keywords',
2020-04-05 09:18:32 +00:00
'link',
2020-04-05 05:46:18 +00:00
'name',
'notes',
2019-06-13 08:33:15 +00:00
'on_order',
2020-04-05 05:46:18 +00:00
'pk',
'purchaseable',
'salable',
2020-04-05 05:46:18 +00:00
'thumbnail',
'trackable',
'total_stock',
'units',
'used_in',
'url', # Link to the part detail page
'variant_of',
'virtual',
]
2018-05-02 13:42:57 +00:00
class PartStarSerializer(InvenTreeModelSerializer):
""" Serializer for a PartStar object """
partname = serializers.CharField(source='part.full_name', read_only=True)
username = serializers.CharField(source='user.username', read_only=True)
class Meta:
model = PartStar
fields = [
'pk',
'part',
'partname',
'user',
'username',
]
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)
sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True)
price_range = serializers.CharField(read_only=True)
validated = serializers.BooleanField(read_only=True, source='is_line_valid')
2018-05-02 13:42:57 +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.
# 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',
'sub_part_detail',
2019-04-14 12:20:11 +00:00
'quantity',
'reference',
'price_range',
2019-05-14 21:23:02 +00:00
'overage',
2019-04-14 12:20:11 +00:00
'note',
'validated',
]
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',
]