InvenTree/InvenTree/stock/serializers.py

161 lines
3.9 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
from part.serializers import PartBriefSerializer
2019-04-13 23:23:24 +00:00
from InvenTree.serializers import UserSerializerBrief
class LocationBriefSerializer(serializers.ModelSerializer):
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(serializers.ModelSerializer):
""" 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',
'part',
2019-04-29 14:18:58 +00:00
'part_name',
'supplier_part',
'location',
'location_name',
'quantity',
]
2018-04-29 11:02:40 +00:00
class StockItemSerializer(serializers.ModelSerializer):
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
url = serializers.CharField(source='get_absolute_url', read_only=True)
2019-04-17 13:33:19 +00:00
status_text = serializers.CharField(source='get_status_display', read_only=True)
2017-04-11 08:58:44 +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)
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)
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')
2017-04-11 08:58:44 +00:00
class Meta:
model = StockItem
2018-04-29 11:02:40 +00:00
fields = [
'pk',
'url',
'part',
2019-05-23 12:57:45 +00:00
'part_detail',
2018-04-29 11:02:40 +00:00
'supplier_part',
'location',
2019-05-23 12:57:45 +00:00
'location_detail',
2018-04-30 11:03:25 +00:00
'in_stock',
2018-04-29 11:02:40 +00:00
'quantity',
'serial',
'batch',
'status',
2019-04-17 13:33:19 +00:00
'status_text',
2018-04-29 11:02:40 +00:00
'notes',
]
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
'quantity',
'in_stock'
2018-04-29 11:02:40 +00:00
]
class StockQuantitySerializer(serializers.ModelSerializer):
class Meta:
model = StockItem
fields = ('quantity',)
2017-04-11 08:58:44 +00:00
2018-05-03 15:33:21 +00:00
class LocationSerializer(serializers.ModelSerializer):
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)
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'
]
class StockTrackingSerializer(serializers.ModelSerializer):
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',
'quantity',
'user',
'system',
]
read_only_fields = [
'date',
'user',
'system',
'quantity',
]