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
|
|
|
|
|
2017-04-21 13:47:04 +00:00
|
|
|
from .models import StockItem, StockLocation
|
2018-05-08 12:30:32 +00:00
|
|
|
from .models import StockItemTracking
|
2020-05-11 13:32:40 +00:00
|
|
|
from .models import StockItemAttachment
|
2020-05-16 10:45:10 +00:00
|
|
|
from .models import StockItemTestResult
|
2017-04-11 08:58:44 +00:00
|
|
|
|
2020-05-02 03:46:19 +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
|
2018-05-02 13:08:45 +00:00
|
|
|
from part.serializers import PartBriefSerializer
|
2019-06-13 13:03:58 +00:00
|
|
|
from InvenTree.serializers import UserSerializerBrief, InvenTreeModelSerializer
|
2019-04-13 23:23:24 +00:00
|
|
|
|
2018-05-02 13:08:45 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class LocationBriefSerializer(InvenTreeModelSerializer):
|
2019-04-13 23:23:24 +00:00
|
|
|
"""
|
|
|
|
Provides a brief serializer for a StockLocation object
|
|
|
|
"""
|
2018-05-02 13:08:45 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StockLocation
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'name',
|
|
|
|
'pathstring',
|
|
|
|
]
|
|
|
|
|
2017-04-11 08:58:44 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class StockItemSerializerBrief(InvenTreeModelSerializer):
|
2019-04-29 13:56:02 +00:00
|
|
|
""" Brief serializers for a StockItem """
|
|
|
|
|
|
|
|
location_name = serializers.CharField(source='location', read_only=True)
|
2019-05-12 02:16:04 +00:00
|
|
|
part_name = serializers.CharField(source='part.full_name', read_only=True)
|
2020-04-25 13:17:07 +00:00
|
|
|
quantity = serializers.FloatField()
|
2019-04-29 13:56:02 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StockItem
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-04-14 12:57:46 +00:00
|
|
|
'uid',
|
2019-04-29 13:56:02 +00:00
|
|
|
'part',
|
2019-04-29 14:18:58 +00:00
|
|
|
'part_name',
|
2019-04-29 13:56:02 +00:00
|
|
|
'supplier_part',
|
|
|
|
'location',
|
|
|
|
'location_name',
|
|
|
|
'quantity',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
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(
|
2020-05-02 03:46:19 +00:00
|
|
|
'belongs_to',
|
|
|
|
'build',
|
|
|
|
'build_order',
|
|
|
|
'sales_order',
|
2020-04-19 14:49:13 +00:00
|
|
|
'supplier_part',
|
|
|
|
'supplier_part__supplier',
|
|
|
|
'supplier_part__manufacturer',
|
2020-04-22 03:11:19 +00:00
|
|
|
'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
|
|
|
)
|
2019-05-28 07:21:29 +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-05-02 03:46:19 +00:00
|
|
|
queryset = queryset.annotate(
|
2020-05-02 03:51:29 +00:00
|
|
|
allocated=Coalesce(
|
2020-05-02 03:46:19 +00:00
|
|
|
Sum('sales_order_allocations__quantity', distinct=True), 0) + Coalesce(
|
|
|
|
Sum('allocations__quantity', distinct=True), 0),
|
2020-05-02 03:51:29 +00:00
|
|
|
tracking_items=Count('tracking_info'),
|
2020-05-02 03:46:19 +00:00
|
|
|
)
|
|
|
|
|
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)
|
2020-04-06 12:02:23 +00:00
|
|
|
|
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)
|
2019-05-19 22:44:52 +00:00
|
|
|
|
2020-05-02 03:46:19 +00:00
|
|
|
tracking_items = serializers.IntegerField()
|
2020-04-19 15:14:19 +00:00
|
|
|
|
2020-04-22 05:24:11 +00:00
|
|
|
quantity = serializers.FloatField()
|
2020-05-02 03:46:19 +00:00
|
|
|
allocated = serializers.FloatField()
|
2020-04-22 00:11:40 +00:00
|
|
|
|
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 = [
|
2020-04-22 00:11:40 +00:00
|
|
|
'allocated',
|
2020-04-06 00:43:06 +00:00
|
|
|
'batch',
|
2020-04-26 06:38:29 +00:00
|
|
|
'build_order',
|
|
|
|
'belongs_to',
|
2020-04-06 00:43:06 +00:00
|
|
|
'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',
|
2020-04-06 00:43:06 +00:00
|
|
|
'pk',
|
2018-04-29 11:02:40 +00:00
|
|
|
'quantity',
|
2020-04-26 06:38:29 +00:00
|
|
|
'sales_order',
|
2018-04-29 11:02:40 +00:00
|
|
|
'serial',
|
2020-04-06 00:43:06 +00:00
|
|
|
'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
|
|
|
|
2017-04-20 12:08:27 +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 = [
|
2020-05-02 03:46:19 +00:00
|
|
|
'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
|
|
|
]
|
2017-04-20 12:08:27 +00:00
|
|
|
|
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class StockQuantitySerializer(InvenTreeModelSerializer):
|
2017-04-20 12:08:27 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StockItem
|
|
|
|
fields = ('quantity',)
|
|
|
|
|
2017-04-11 08:58:44 +00:00
|
|
|
|
2019-06-13 13:03:58 +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)
|
|
|
|
|
2020-04-03 00:41:51 +00:00
|
|
|
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',
|
2020-04-03 00:41:51 +00:00
|
|
|
'pathstring',
|
|
|
|
'items',
|
2019-04-13 23:23:24 +00:00
|
|
|
]
|
2019-04-25 12:04:02 +00:00
|
|
|
|
|
|
|
|
2020-05-11 13:32:40 +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)
|
|
|
|
|
2020-05-11 13:32:40 +00:00
|
|
|
class Meta:
|
|
|
|
model = StockItemAttachment
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'stock_item',
|
|
|
|
'attachment',
|
2020-05-12 10:50:03 +00:00
|
|
|
'comment',
|
|
|
|
'user',
|
|
|
|
'user_detail',
|
2020-05-11 13:32:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-05-16 10:45:10 +00:00
|
|
|
class StockItemTestResultSerializer(InvenTreeModelSerializer):
|
|
|
|
""" Serializer for the StockItemTestResult model """
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StockItemTestResult
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
2020-05-16 10:56:36 +00:00
|
|
|
'stock_item',
|
2020-05-16 10:45:10 +00:00
|
|
|
'test',
|
|
|
|
'result',
|
|
|
|
'value',
|
|
|
|
'attachment',
|
|
|
|
'notes',
|
|
|
|
'user',
|
|
|
|
'date'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class StockTrackingSerializer(InvenTreeModelSerializer):
|
2019-04-27 12:49:16 +00:00
|
|
|
""" Serializer for StockItemTracking model """
|
2019-04-25 12:04:02 +00:00
|
|
|
|
2020-05-11 22:12:12 +00:00
|
|
|
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')
|
|
|
|
|
2019-04-25 12:04:02 +00:00
|
|
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
|
|
|
|
2020-05-11 22:12:12 +00:00
|
|
|
item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True)
|
2019-04-25 12:04:02 +00:00
|
|
|
|
2020-05-11 22:12:12 +00:00
|
|
|
user_detail = UserSerializerBrief(source='user', many=False, read_only=True)
|
2019-04-25 12:04:02 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StockItemTracking
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'url',
|
|
|
|
'item',
|
2020-05-11 22:12:12 +00:00
|
|
|
'item_detail',
|
2019-04-25 12:04:02 +00:00
|
|
|
'date',
|
|
|
|
'title',
|
|
|
|
'notes',
|
2020-04-06 00:43:06 +00:00
|
|
|
'link',
|
2019-04-25 12:04:02 +00:00
|
|
|
'quantity',
|
|
|
|
'user',
|
2020-05-11 22:12:12 +00:00
|
|
|
'user_detail',
|
2019-04-25 12:04:02 +00:00
|
|
|
'system',
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
'date',
|
|
|
|
'user',
|
|
|
|
'system',
|
|
|
|
'quantity',
|
|
|
|
]
|