2019-04-27 15:09:48 +00:00
|
|
|
"""
|
2019-04-27 12:18:07 +00:00
|
|
|
JSON serializers for Company app
|
|
|
|
"""
|
|
|
|
|
2021-06-30 04:07:15 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2017-04-14 04:45:17 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2020-09-05 13:03:38 +00:00
|
|
|
from sql_util.utils import SubqueryCount
|
2020-05-02 05:23:28 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
from InvenTree.serializers import InvenTreeModelSerializer
|
2021-06-22 22:39:06 +00:00
|
|
|
from InvenTree.serializers import InvenTreeImageSerializerField
|
2019-06-13 13:03:58 +00:00
|
|
|
|
2019-05-18 08:04:25 +00:00
|
|
|
from part.serializers import PartBriefSerializer
|
2017-04-14 04:45:17 +00:00
|
|
|
|
2021-06-22 22:40:51 +00:00
|
|
|
from .models import Company
|
|
|
|
from .models import ManufacturerPart, ManufacturerPartParameter
|
|
|
|
from .models import SupplierPart, SupplierPriceBreak
|
|
|
|
|
2021-07-03 02:45:59 +00:00
|
|
|
from common.settings import currency_code_default, currency_code_mappings
|
2021-06-29 13:14:39 +00:00
|
|
|
|
2018-04-23 11:18:35 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class CompanyBriefSerializer(InvenTreeModelSerializer):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Serializer for Company object (limited detail) """
|
2018-05-02 14:47:03 +00:00
|
|
|
|
|
|
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
|
|
|
|
2020-04-13 07:37:44 +00:00
|
|
|
image = serializers.CharField(source='get_thumbnail_url', read_only=True)
|
|
|
|
|
2018-05-02 14:47:03 +00:00
|
|
|
class Meta:
|
|
|
|
model = Company
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'url',
|
2020-04-13 07:37:44 +00:00
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'image',
|
2018-05-02 14:47:03 +00:00
|
|
|
]
|
|
|
|
|
2019-04-13 23:30:45 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class CompanySerializer(InvenTreeModelSerializer):
|
2019-04-27 12:18:07 +00:00
|
|
|
""" Serializer for Company object (full detail) """
|
2017-04-14 04:45:17 +00:00
|
|
|
|
2020-05-02 05:23:28 +00:00
|
|
|
@staticmethod
|
|
|
|
def annotate_queryset(queryset):
|
|
|
|
|
2020-09-05 13:03:38 +00:00
|
|
|
# Add count of parts manufactured
|
|
|
|
queryset = queryset.annotate(
|
|
|
|
parts_manufactured=SubqueryCount('manufactured_parts')
|
2020-05-02 05:23:28 +00:00
|
|
|
)
|
2018-05-02 13:17:24 +00:00
|
|
|
|
2020-09-05 13:03:38 +00:00
|
|
|
queryset = queryset.annotate(
|
|
|
|
parts_supplied=SubqueryCount('supplied_parts')
|
|
|
|
)
|
|
|
|
|
|
|
|
return queryset
|
|
|
|
|
2020-05-02 05:23:28 +00:00
|
|
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-06-23 01:40:37 +00:00
|
|
|
image = InvenTreeImageSerializerField(required=False, allow_null=True)
|
2019-09-02 12:06:42 +00:00
|
|
|
|
2020-05-02 05:36:48 +00:00
|
|
|
parts_supplied = serializers.IntegerField(read_only=True)
|
|
|
|
parts_manufactured = serializers.IntegerField(read_only=True)
|
2020-05-02 05:23:28 +00:00
|
|
|
|
2021-06-29 13:14:39 +00:00
|
|
|
currency = serializers.ChoiceField(
|
2021-07-03 02:45:59 +00:00
|
|
|
choices=currency_code_mappings(),
|
2021-07-02 12:02:18 +00:00
|
|
|
initial=currency_code_default,
|
|
|
|
help_text=_('Default currency used for this supplier'),
|
|
|
|
label=_('Currency Code'),
|
|
|
|
required=True,
|
2021-06-29 13:14:39 +00:00
|
|
|
)
|
|
|
|
|
2017-04-14 04:45:17 +00:00
|
|
|
class Meta:
|
2018-04-18 23:01:07 +00:00
|
|
|
model = Company
|
2019-05-16 12:29:39 +00:00
|
|
|
fields = [
|
2019-05-22 14:31:27 +00:00
|
|
|
'pk',
|
2019-05-16 12:29:39 +00:00
|
|
|
'url',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'website',
|
|
|
|
'name',
|
|
|
|
'phone',
|
|
|
|
'address',
|
|
|
|
'email',
|
2021-06-29 10:44:44 +00:00
|
|
|
'currency',
|
2019-05-16 12:29:39 +00:00
|
|
|
'contact',
|
2020-04-06 01:36:25 +00:00
|
|
|
'link',
|
2019-05-16 12:29:39 +00:00
|
|
|
'image',
|
|
|
|
'is_customer',
|
2020-04-13 02:16:42 +00:00
|
|
|
'is_manufacturer',
|
2019-05-16 12:29:39 +00:00
|
|
|
'is_supplier',
|
2020-04-13 02:16:42 +00:00
|
|
|
'notes',
|
2020-05-02 05:23:28 +00:00
|
|
|
'parts_supplied',
|
|
|
|
'parts_manufactured',
|
2019-05-16 12:29:39 +00:00
|
|
|
]
|
2019-05-18 08:04:25 +00:00
|
|
|
|
|
|
|
|
2021-03-24 15:44:51 +00:00
|
|
|
class ManufacturerPartSerializer(InvenTreeModelSerializer):
|
2021-03-30 20:48:16 +00:00
|
|
|
""" Serializer for ManufacturerPart object """
|
2021-03-24 15:44:51 +00:00
|
|
|
|
|
|
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
|
|
|
|
|
|
|
manufacturer_detail = CompanyBriefSerializer(source='manufacturer', many=False, read_only=True)
|
|
|
|
|
|
|
|
pretty_name = serializers.CharField(read_only=True)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
part_detail = kwargs.pop('part_detail', False)
|
|
|
|
manufacturer_detail = kwargs.pop('manufacturer_detail', False)
|
|
|
|
prettify = kwargs.pop('pretty', False)
|
|
|
|
|
|
|
|
super(ManufacturerPartSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if part_detail is not True:
|
|
|
|
self.fields.pop('part_detail')
|
|
|
|
|
|
|
|
if manufacturer_detail is not True:
|
|
|
|
self.fields.pop('manufacturer_detail')
|
|
|
|
|
|
|
|
if prettify is not True:
|
|
|
|
self.fields.pop('pretty_name')
|
|
|
|
|
|
|
|
manufacturer = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_manufacturer=True))
|
|
|
|
|
|
|
|
class Meta:
|
2021-03-30 17:55:20 +00:00
|
|
|
model = ManufacturerPart
|
2021-03-24 15:44:51 +00:00
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'part_detail',
|
|
|
|
'pretty_name',
|
|
|
|
'manufacturer',
|
|
|
|
'manufacturer_detail',
|
|
|
|
'description',
|
|
|
|
'MPN',
|
|
|
|
'link',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-06-20 14:28:28 +00:00
|
|
|
class ManufacturerPartParameterSerializer(InvenTreeModelSerializer):
|
|
|
|
"""
|
|
|
|
Serializer for the ManufacturerPartParameter model
|
|
|
|
"""
|
|
|
|
|
|
|
|
manufacturer_part_detail = ManufacturerPartSerializer(source='manufacturer_part', many=False, read_only=True)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
man_detail = kwargs.pop('manufacturer_part_detail', False)
|
|
|
|
|
|
|
|
super(ManufacturerPartParameterSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if not man_detail:
|
|
|
|
self.fields.pop('manufacturer_part_detail')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ManufacturerPartParameter
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'manufacturer_part',
|
|
|
|
'manufacturer_part_detail',
|
|
|
|
'name',
|
|
|
|
'value',
|
|
|
|
'units',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class SupplierPartSerializer(InvenTreeModelSerializer):
|
2019-05-18 08:04:25 +00:00
|
|
|
""" Serializer for SupplierPart object """
|
|
|
|
|
|
|
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
|
|
|
|
2020-04-13 07:37:44 +00:00
|
|
|
supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
|
2020-08-26 13:23:57 +00:00
|
|
|
|
2021-03-30 20:48:16 +00:00
|
|
|
manufacturer_detail = CompanyBriefSerializer(source='manufacturer_part.manufacturer', many=False, read_only=True)
|
2019-05-18 08:04:25 +00:00
|
|
|
|
2020-08-26 13:23:57 +00:00
|
|
|
pretty_name = serializers.CharField(read_only=True)
|
|
|
|
|
2019-05-23 12:44:37 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
2021-07-02 07:11:07 +00:00
|
|
|
part_detail = kwargs.pop('part_detail', True)
|
|
|
|
supplier_detail = kwargs.pop('supplier_detail', True)
|
|
|
|
manufacturer_detail = kwargs.pop('manufacturer_detail', True)
|
|
|
|
|
2020-08-26 13:23:57 +00:00
|
|
|
prettify = kwargs.pop('pretty', False)
|
2019-05-23 12:44:37 +00:00
|
|
|
|
|
|
|
super(SupplierPartSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
if part_detail is not True:
|
|
|
|
self.fields.pop('part_detail')
|
|
|
|
|
2020-04-13 07:37:44 +00:00
|
|
|
if supplier_detail is not True:
|
|
|
|
self.fields.pop('supplier_detail')
|
|
|
|
|
|
|
|
if manufacturer_detail is not True:
|
|
|
|
self.fields.pop('manufacturer_detail')
|
|
|
|
|
2020-08-26 13:23:57 +00:00
|
|
|
if prettify is not True:
|
2020-08-26 13:50:49 +00:00
|
|
|
self.fields.pop('pretty_name')
|
2020-08-26 13:23:57 +00:00
|
|
|
|
2020-08-15 11:24:01 +00:00
|
|
|
supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-04-01 13:11:47 +00:00
|
|
|
manufacturer = serializers.PrimaryKeyRelatedField(source='manufacturer_part.manufacturer', read_only=True)
|
2021-05-06 10:11:38 +00:00
|
|
|
|
2021-04-01 13:11:47 +00:00
|
|
|
MPN = serializers.StringRelatedField(source='manufacturer_part.MPN')
|
2020-08-15 11:24:01 +00:00
|
|
|
|
2021-04-02 15:13:57 +00:00
|
|
|
manufacturer_part = ManufacturerPartSerializer(read_only=True)
|
|
|
|
|
2019-05-18 08:04:25 +00:00
|
|
|
class Meta:
|
|
|
|
model = SupplierPart
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'part_detail',
|
2020-08-26 13:23:57 +00:00
|
|
|
'pretty_name',
|
2019-05-18 08:04:25 +00:00
|
|
|
'supplier',
|
2020-04-13 07:37:44 +00:00
|
|
|
'supplier_detail',
|
2019-05-18 08:04:25 +00:00
|
|
|
'SKU',
|
2021-03-30 17:55:20 +00:00
|
|
|
'manufacturer',
|
|
|
|
'MPN',
|
2020-04-13 07:37:44 +00:00
|
|
|
'manufacturer_detail',
|
2021-04-02 15:13:57 +00:00
|
|
|
'manufacturer_part',
|
2019-05-24 11:33:45 +00:00
|
|
|
'description',
|
2020-04-06 01:36:25 +00:00
|
|
|
'link',
|
2019-05-18 08:04:25 +00:00
|
|
|
]
|
|
|
|
|
2021-03-31 22:04:28 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
""" Extract manufacturer data and process ManufacturerPart """
|
|
|
|
|
|
|
|
# Create SupplierPart
|
|
|
|
supplier_part = super().create(validated_data)
|
|
|
|
|
2021-04-01 13:11:47 +00:00
|
|
|
# Get ManufacturerPart raw data (unvalidated)
|
|
|
|
manufacturer_id = self.initial_data.get('manufacturer', None)
|
|
|
|
MPN = self.initial_data.get('MPN', None)
|
|
|
|
|
2021-04-30 17:54:56 +00:00
|
|
|
if manufacturer_id and MPN:
|
|
|
|
kwargs = {
|
|
|
|
'manufacturer': manufacturer_id,
|
|
|
|
'MPN': MPN,
|
|
|
|
}
|
2021-04-02 15:13:57 +00:00
|
|
|
supplier_part.save(**kwargs)
|
2021-03-31 22:04:28 +00:00
|
|
|
|
|
|
|
return supplier_part
|
|
|
|
|
2019-05-18 08:04:25 +00:00
|
|
|
|
2019-06-13 13:03:58 +00:00
|
|
|
class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
|
2019-05-18 08:04:25 +00:00
|
|
|
""" Serializer for SupplierPriceBreak object """
|
|
|
|
|
2020-09-17 13:19:50 +00:00
|
|
|
quantity = serializers.FloatField()
|
|
|
|
|
2020-11-10 13:21:06 +00:00
|
|
|
price = serializers.CharField()
|
2020-09-17 13:19:50 +00:00
|
|
|
|
2021-06-30 04:07:15 +00:00
|
|
|
price_currency = serializers.ChoiceField(
|
2021-07-03 02:45:59 +00:00
|
|
|
choices=currency_code_mappings(),
|
2021-06-30 04:07:15 +00:00
|
|
|
default=currency_code_default,
|
|
|
|
label=_('Currency'),
|
|
|
|
)
|
|
|
|
|
2019-05-18 08:04:25 +00:00
|
|
|
class Meta:
|
|
|
|
model = SupplierPriceBreak
|
|
|
|
fields = [
|
|
|
|
'pk',
|
|
|
|
'part',
|
|
|
|
'quantity',
|
2020-11-10 13:21:06 +00:00
|
|
|
'price',
|
2021-06-30 04:07:15 +00:00
|
|
|
'price_currency',
|
2019-05-18 08:04:25 +00:00
|
|
|
]
|