InvenTree/InvenTree/stock/serializers.py

204 lines
5.2 KiB
Python
Raw Normal View History

2019-04-27 12:49:16 +00:00
"""
JSON serializers for Stock app
"""
2017-04-11 08:58:44 +00:00
from rest_framework import serializers
from .models import StockItem, StockLocation
from .models import StockItemTracking
2017-04-11 08:58:44 +00:00
2020-04-19 22:24:43 +00:00
from company.serializers import SupplierPartSerializer
from part.serializers import PartBriefSerializer
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
2019-04-13 23:23:24 +00:00
class LocationBriefSerializer(InvenTreeModelSerializer):
2019-04-13 23:23:24 +00:00
"""
Provides a brief serializer for a StockLocation object
"""
url = serializers.CharField(source='get_absolute_url', read_only=True)
class Meta:
model = StockLocation
fields = [
'pk',
'name',
'pathstring',
'url',
]
2017-04-11 08:58:44 +00:00
class StockItemSerializerBrief(InvenTreeModelSerializer):
""" Brief serializers for a StockItem """
location_name = serializers.CharField(source='location', read_only=True)
part_name = serializers.CharField(source='part.full_name', read_only=True)
class Meta:
model = StockItem
fields = [
'pk',
2020-04-14 12:57:46 +00:00
'uid',
'part',
2019-04-29 14:18:58 +00:00
'part_name',
'supplier_part',
'location',
'location_name',
'quantity',
]
class StockItemSerializer(InvenTreeModelSerializer):
2019-04-27 12:49:16 +00:00
""" Serializer for a StockItem:
2019-04-13 23:23:24 +00:00
- Includes serialization for the linked part
- Includes serialization for the item location
"""
2019-04-13 23:39:01 +00:00
2020-04-19 14:49:13 +00:00
@staticmethod
def prefetch_queryset(queryset):
"""
Prefetch related database tables,
to reduce database hits.
"""
2017-04-11 08:58:44 +00:00
2020-04-19 14:49:13 +00:00
return queryset.prefetch_related(
'supplier_part',
'supplier_part__supplier',
'supplier_part__manufacturer',
'location',
2020-04-19 15:14:19 +00:00
'part',
'tracking_info',
2020-04-19 14:49:13 +00:00
)
2020-04-19 15:14:19 +00:00
@staticmethod
def annotate_queryset(queryset):
"""
Add some extra annotations to the queryset,
performing database queries as efficiently as possible.
"""
2020-04-19 22:24:43 +00:00
# TODO - Add custom annotated fields
return queryset
2020-04-19 15:14:19 +00:00
2020-04-19 14:49:13 +00:00
status_text = serializers.CharField(source='get_status_display', read_only=True)
2019-05-23 12:57:45 +00:00
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
location_detail = LocationBriefSerializer(source='location', many=False, read_only=True)
2020-04-19 22:24:43 +00:00
supplier_part_detail = SupplierPartSerializer(source='supplier_part', many=False, read_only=True)
2020-04-19 15:14:19 +00:00
tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True)
allocated = serializers.BooleanField(source='is_allocated', read_only=True)
2019-05-23 12:57:45 +00:00
def __init__(self, *args, **kwargs):
part_detail = kwargs.pop('part_detail', False)
location_detail = kwargs.pop('location_detail', False)
2020-04-19 22:24:43 +00:00
supplier_part_detail = kwargs.pop('supplier_part_detail', False)
2019-05-23 12:57:45 +00:00
super(StockItemSerializer, self).__init__(*args, **kwargs)
if part_detail is not True:
self.fields.pop('part_detail')
if location_detail is not True:
self.fields.pop('location_detail')
2020-04-19 22:24:43 +00:00
if supplier_part_detail is not True:
self.fields.pop('supplier_part_detail')
2017-04-11 08:58:44 +00:00
class Meta:
model = StockItem
2018-04-29 11:02:40 +00:00
fields = [
'allocated',
'batch',
'in_stock',
'link',
'location',
'location_detail',
'notes',
2018-04-29 11:02:40 +00:00
'part',
2019-05-23 12:57:45 +00:00
'part_detail',
'pk',
2018-04-29 11:02:40 +00:00
'quantity',
'serial',
'supplier_part',
2020-04-19 22:24:43 +00:00
'supplier_part_detail',
2018-04-29 11:02:40 +00:00
'status',
2019-04-17 13:33:19 +00:00
'status_text',
2020-04-19 15:14:19 +00:00
'tracking_items',
2020-04-14 12:57:46 +00:00
'uid',
2018-04-29 11:02:40 +00:00
]
2017-04-11 08:58:44 +00:00
""" These fields are read-only in this context.
They can be updated by accessing the appropriate API endpoints
"""
2018-04-29 11:02:40 +00:00
read_only_fields = [
'stocktake_date',
'stocktake_user',
'updated',
2019-04-13 23:23:24 +00:00
'in_stock'
2018-04-29 11:02:40 +00:00
]
class StockQuantitySerializer(InvenTreeModelSerializer):
class Meta:
model = StockItem
fields = ('quantity',)
2017-04-11 08:58:44 +00:00
class LocationSerializer(InvenTreeModelSerializer):
2017-04-11 08:58:44 +00:00
""" Detailed information about a stock location
"""
2018-05-03 15:33:21 +00:00
url = serializers.CharField(source='get_absolute_url', read_only=True)
items = serializers.IntegerField(source='item_count', read_only=True)
2017-04-11 08:58:44 +00:00
class Meta:
model = StockLocation
2018-05-03 15:33:21 +00:00
fields = [
2019-04-13 23:23:24 +00:00
'pk',
'url',
'name',
'description',
'parent',
'pathstring',
'items',
2019-04-13 23:23:24 +00:00
]
class StockTrackingSerializer(InvenTreeModelSerializer):
2019-04-27 12:49:16 +00:00
""" Serializer for StockItemTracking model """
url = serializers.CharField(source='get_absolute_url', read_only=True)
user = UserSerializerBrief(many=False, read_only=True)
item = StockItemSerializerBrief(many=False, read_only=True)
class Meta:
model = StockItemTracking
fields = [
'pk',
'url',
'item',
'date',
'title',
'notes',
'link',
'quantity',
'user',
'system',
]
read_only_fields = [
'date',
'user',
'system',
'quantity',
]