Update company API

- option to include manufacturer_detail
- improve company serializer

(cherry picked from commit 2ea1e72c44)
This commit is contained in:
Oliver Walters 2020-04-13 17:37:44 +10:00
parent 4e3825682a
commit 0001b889f0
4 changed files with 35 additions and 13 deletions

View File

@ -91,12 +91,10 @@ class SupplierPartList(generics.ListCreateAPIView):
def get_serializer(self, *args, **kwargs): def get_serializer(self, *args, **kwargs):
# Do we wish to include extra detail? # Do we wish to include extra detail?
try: kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', None))
part_detail = str2bool(self.request.GET.get('part_detail', None)) kwargs['supplier_detail'] = str2bool(self.request.query_params.get('supplier_detail', None))
except AttributeError: kwargs['manufacturer_detail'] = str2bool(self.request.query_params.get('manufacturer_detail', None))
part_detail = None
kwargs['part_detail'] = part_detail
kwargs['context'] = self.get_serializer_context() kwargs['context'] = self.get_serializer_context()
return self.serializer_class(*args, **kwargs) return self.serializer_class(*args, **kwargs)

View File

@ -23,6 +23,7 @@ from markdownx.models import MarkdownxField
from stdimage.models import StdImageField from stdimage.models import StdImageField
from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail
from InvenTree.helpers import normalize
from InvenTree.fields import InvenTreeURLField, RoundingDecimalField from InvenTree.fields import InvenTreeURLField, RoundingDecimalField
from InvenTree.status_codes import OrderStatus from InvenTree.status_codes import OrderStatus
from common.models import Currency from common.models import Currency
@ -352,7 +353,7 @@ class SupplierPart(models.Model):
if pb_found: if pb_found:
cost = pb_cost * quantity cost = pb_cost * quantity
return cost + self.base_cost return normalize(cost + self.base_cost)
else: else:
return None return None

View File

@ -17,12 +17,16 @@ class CompanyBriefSerializer(InvenTreeModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True) url = serializers.CharField(source='get_absolute_url', read_only=True)
image = serializers.CharField(source='get_thumbnail_url', read_only=True)
class Meta: class Meta:
model = Company model = Company
fields = [ fields = [
'pk', 'pk',
'url', 'url',
'name' 'name',
'description',
'image',
] ]
@ -64,20 +68,28 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
part_detail = PartBriefSerializer(source='part', many=False, read_only=True) part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
supplier_name = serializers.CharField(source='supplier.name', read_only=True) supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True)
supplier_logo = serializers.CharField(source='supplier.get_thumbnail_url', read_only=True) manufacturer_detail = CompanyBriefSerializer(source='manufacturer', many=False, read_only=True)
pricing = serializers.CharField(source='unit_pricing', read_only=True) pricing = serializers.CharField(source='unit_pricing', read_only=True)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
part_detail = kwargs.pop('part_detail', False) part_detail = kwargs.pop('part_detail', False)
supplier_detail = kwargs.pop('supplier_detail', False)
manufacturer_detail = kwargs.pop('manufacturer_detail', False)
super(SupplierPartSerializer, self).__init__(*args, **kwargs) super(SupplierPartSerializer, self).__init__(*args, **kwargs)
if part_detail is not True: if part_detail is not True:
self.fields.pop('part_detail') self.fields.pop('part_detail')
if supplier_detail is not True:
self.fields.pop('supplier_detail')
if manufacturer_detail is not True:
self.fields.pop('manufacturer_detail')
class Meta: class Meta:
model = SupplierPart model = SupplierPart
fields = [ fields = [
@ -86,11 +98,10 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
'part', 'part',
'part_detail', 'part_detail',
'supplier', 'supplier',
'supplier_name', 'supplier_detail',
'supplier_logo',
'SKU', 'SKU',
'manufacturer', 'manufacturer',
'manufacturer_name', 'manufacturer_detail',
'description', 'description',
'MPN', 'MPN',
'link', 'link',

View File

@ -53,6 +53,8 @@
return { return {
supplier: {{ company.id }}, supplier: {{ company.id }},
part_detail: true, part_detail: true,
supplier_detail: true,
manufacturer_detail: true,
} }
}, },
columns: [ columns: [
@ -94,6 +96,16 @@
sortable: true, sortable: true,
field: 'manufacturer', field: 'manufacturer',
title: '{% trans "Manufacturer" %}', title: '{% trans "Manufacturer" %}',
formatter: function(value, row, index, field) {
if (value) {
var name = row.manufacturer_detail.name;
var html = imageHoverIcon(row.manufacturer_detail.image) + renderLink(name, '/company/' + value + '/');
return html;
} else {
return "-";
}
}
}, },
{ {
sortable: true, sortable: true,