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
|
2017-04-11 08:58:44 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
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(
|
|
|
|
'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
|
|
|
)
|
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-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)
|
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-04-19 15:14:19 +00:00
|
|
|
tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True)
|
|
|
|
|
2020-04-22 00:11:40 +00:00
|
|
|
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 = [
|
2020-04-22 00:11:40 +00:00
|
|
|
'allocated',
|
2020-04-06 00:43:06 +00:00
|
|
|
'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',
|
2020-04-06 00:43:06 +00:00
|
|
|
'pk',
|
2018-04-29 11:02:40 +00:00
|
|
|
'quantity',
|
|
|
|
'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 = [
|
|
|
|
'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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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',
|
2020-04-06 00:43:06 +00:00
|
|
|
'link',
|
2019-04-25 12:04:02 +00:00
|
|
|
'quantity',
|
|
|
|
'user',
|
|
|
|
'system',
|
|
|
|
]
|
|
|
|
|
|
|
|
read_only_fields = [
|
|
|
|
'date',
|
|
|
|
'user',
|
|
|
|
'system',
|
|
|
|
'quantity',
|
|
|
|
]
|