InvenTree/InvenTree/stock/serializers.py

284 lines
7.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
from .models import StockItemAttachment
from .models import StockItemTestResult
2017-04-11 08:58:44 +00:00
from django.db.models import Sum, Count
from django.db.models.functions import Coalesce
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
"""
class Meta:
model = StockLocation
fields = [
'pk',
'name',
'pathstring',
]
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)
quantity = serializers.FloatField()
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(
'belongs_to',
'build',
'build_order',
'sales_order',
2020-04-19 14:49:13 +00:00
'supplier_part',
'supplier_part__supplier',
'supplier_part__manufacturer',
'allocations',
'sales_order_allocations',
2020-04-19 14:49:13 +00:00
'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.
"""
queryset = queryset.annotate(
allocated=Coalesce(
Sum('sales_order_allocations__quantity', distinct=True), 0) + Coalesce(
Sum('allocations__quantity', distinct=True), 0),
tracking_items=Count('tracking_info'),
)
2020-04-19 22:24:43 +00:00
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)
tracking_items = serializers.IntegerField()
2020-04-19 15:14:19 +00:00
quantity = serializers.FloatField()
allocated = serializers.FloatField()
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',
'build_order',
'belongs_to',
'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',
'sales_order',
2018-04-29 11:02:40 +00:00
'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 = [
'allocated',
2018-04-29 11:02:40 +00:00
'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 StockItemAttachmentSerializer(InvenTreeModelSerializer):
""" Serializer for StockItemAttachment model """
2020-05-12 10:50:03 +00:00
def __init_(self, *args, **kwargs):
user_detail = kwargs.pop('user_detail', False)
super().__init__(*args, **kwargs)
if user_detail is not True:
self.fields.pop('user_detail')
user_detail = UserSerializerBrief(source='user', read_only=True)
class Meta:
model = StockItemAttachment
fields = [
'pk',
'stock_item',
'attachment',
2020-05-12 10:50:03 +00:00
'comment',
'user',
'user_detail',
]
class StockItemTestResultSerializer(InvenTreeModelSerializer):
""" Serializer for the StockItemTestResult model """
class Meta:
model = StockItemTestResult
fields = [
'pk',
2020-05-16 10:56:36 +00:00
'stock_item',
'test',
'result',
'value',
'attachment',
'notes',
'user',
'date'
]
class StockTrackingSerializer(InvenTreeModelSerializer):
2019-04-27 12:49:16 +00:00
""" Serializer for StockItemTracking model """
def __init__(self, *args, **kwargs):
item_detail = kwargs.pop('item_detail', False)
user_detail = kwargs.pop('user_detail', False)
super().__init__(*args, **kwargs)
if item_detail is not True:
self.fields.pop('item_detail')
if user_detail is not True:
self.fields.pop('user_detail')
url = serializers.CharField(source='get_absolute_url', read_only=True)
item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True)
user_detail = UserSerializerBrief(source='user', many=False, read_only=True)
class Meta:
model = StockItemTracking
fields = [
'pk',
'url',
'item',
'item_detail',
'date',
'title',
'notes',
'link',
'quantity',
'user',
'user_detail',
'system',
]
read_only_fields = [
'date',
'user',
'system',
'quantity',
]