mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Update company API
- option to include manufacturer_detail - improve company serializer
This commit is contained in:
		| @@ -91,12 +91,10 @@ class SupplierPartList(generics.ListCreateAPIView): | ||||
|     def get_serializer(self, *args, **kwargs): | ||||
|  | ||||
|         # Do we wish to include extra detail? | ||||
|         try: | ||||
|             part_detail = str2bool(self.request.GET.get('part_detail', None)) | ||||
|         except AttributeError: | ||||
|             part_detail = None | ||||
|  | ||||
|         kwargs['part_detail'] = part_detail | ||||
|         kwargs['part_detail'] = str2bool(self.request.query_params.get('part_detail', None)) | ||||
|         kwargs['supplier_detail'] = str2bool(self.request.query_params.get('supplier_detail', None)) | ||||
|         kwargs['manufacturer_detail'] = str2bool(self.request.query_params.get('manufacturer_detail', None)) | ||||
|          | ||||
|         kwargs['context'] = self.get_serializer_context() | ||||
|  | ||||
|         return self.serializer_class(*args, **kwargs) | ||||
|   | ||||
| @@ -23,6 +23,7 @@ from markdownx.models import MarkdownxField | ||||
| from stdimage.models import StdImageField | ||||
|  | ||||
| from InvenTree.helpers import getMediaUrl, getBlankImage, getBlankThumbnail | ||||
| from InvenTree.helpers import normalize | ||||
| from InvenTree.fields import InvenTreeURLField, RoundingDecimalField | ||||
| from InvenTree.status_codes import OrderStatus | ||||
| from common.models import Currency | ||||
| @@ -352,7 +353,7 @@ class SupplierPart(models.Model): | ||||
|  | ||||
|         if pb_found: | ||||
|             cost = pb_cost * quantity | ||||
|             return cost + self.base_cost | ||||
|             return normalize(cost + self.base_cost) | ||||
|         else: | ||||
|             return None | ||||
|  | ||||
|   | ||||
| @@ -17,12 +17,16 @@ class CompanyBriefSerializer(InvenTreeModelSerializer): | ||||
|  | ||||
|     url = serializers.CharField(source='get_absolute_url', read_only=True) | ||||
|  | ||||
|     image = serializers.CharField(source='get_thumbnail_url', read_only=True) | ||||
|  | ||||
|     class Meta: | ||||
|         model = Company | ||||
|         fields = [ | ||||
|             'pk', | ||||
|             'url', | ||||
|             'name' | ||||
|             'name', | ||||
|             'description', | ||||
|             'image', | ||||
|         ] | ||||
|  | ||||
|  | ||||
| @@ -64,20 +68,28 @@ class SupplierPartSerializer(InvenTreeModelSerializer): | ||||
|  | ||||
|     part_detail = PartBriefSerializer(source='part', many=False, read_only=True) | ||||
|  | ||||
|     supplier_name = serializers.CharField(source='supplier.name', read_only=True) | ||||
|     supplier_logo = serializers.CharField(source='supplier.get_thumbnail_url', read_only=True) | ||||
|     supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True) | ||||
|     manufacturer_detail = CompanyBriefSerializer(source='manufacturer', many=False, read_only=True) | ||||
|  | ||||
|     pricing = serializers.CharField(source='unit_pricing', read_only=True) | ||||
|  | ||||
|     def __init__(self, *args, **kwargs): | ||||
|  | ||||
|         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) | ||||
|  | ||||
|         if part_detail is not True: | ||||
|             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: | ||||
|         model = SupplierPart | ||||
|         fields = [ | ||||
| @@ -86,11 +98,10 @@ class SupplierPartSerializer(InvenTreeModelSerializer): | ||||
|             'part', | ||||
|             'part_detail', | ||||
|             'supplier', | ||||
|             'supplier_name', | ||||
|             'supplier_logo', | ||||
|             'supplier_detail', | ||||
|             'SKU', | ||||
|             'manufacturer', | ||||
|             'manufacturer_name', | ||||
|             'manufacturer_detail', | ||||
|             'description', | ||||
|             'MPN', | ||||
|             'link', | ||||
|   | ||||
| @@ -53,6 +53,8 @@ | ||||
|             return { | ||||
|                 supplier: {{ company.id }}, | ||||
|                 part_detail: true, | ||||
|                 supplier_detail: true, | ||||
|                 manufacturer_detail: true, | ||||
|             } | ||||
|         }, | ||||
|         columns: [ | ||||
| @@ -94,6 +96,16 @@ | ||||
|                 sortable: true, | ||||
|                 field: '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, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user