mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Fixes to the way ManufacturerPart is saved, manufacturer table filtering and test units
This commit is contained in:
		@@ -94,6 +94,7 @@ class ManufacturerPartList(generics.ListCreateAPIView):
 | 
			
		||||
    queryset = ManufacturerPart.objects.all().prefetch_related(
 | 
			
		||||
        'part',
 | 
			
		||||
        'manufacturer',
 | 
			
		||||
        'supplier_parts',
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    serializer_class = ManufacturerPartSerializer
 | 
			
		||||
@@ -141,6 +142,12 @@ class ManufacturerPartList(generics.ListCreateAPIView):
 | 
			
		||||
        if part is not None:
 | 
			
		||||
            queryset = queryset.filter(part=part)
 | 
			
		||||
 | 
			
		||||
        # Filter by supplier part?
 | 
			
		||||
        supplier_part = params.get('supplier_part', None)
 | 
			
		||||
 | 
			
		||||
        if supplier_part is not None:
 | 
			
		||||
            queryset = queryset.filter(supplier_parts=supplier_part)
 | 
			
		||||
 | 
			
		||||
        # Filter by 'active' status of the part?
 | 
			
		||||
        active = params.get('active', None)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -20,3 +20,20 @@
 | 
			
		||||
    part: 5
 | 
			
		||||
    manufacturer: 7
 | 
			
		||||
    MPN: 'MPN789'
 | 
			
		||||
 | 
			
		||||
# Supplier parts linked to Manufacturer parts
 | 
			
		||||
- model: company.supplierpart
 | 
			
		||||
  pk: 10
 | 
			
		||||
  fields:
 | 
			
		||||
    part: 3
 | 
			
		||||
    manufacturer_part: 2
 | 
			
		||||
    supplier: 2
 | 
			
		||||
    SKU: 'MPN456-APPEL'
 | 
			
		||||
 | 
			
		||||
- model: company.supplierpart
 | 
			
		||||
  pk: 11
 | 
			
		||||
  fields:
 | 
			
		||||
    part: 3
 | 
			
		||||
    manufacturer_part: 2
 | 
			
		||||
    supplier: 3
 | 
			
		||||
    SKU: 'MPN456-ZERG'
 | 
			
		||||
 
 | 
			
		||||
@@ -52,20 +52,3 @@
 | 
			
		||||
    part: 2
 | 
			
		||||
    supplier: 2
 | 
			
		||||
    SKU: 'ZERGM312'
 | 
			
		||||
 | 
			
		||||
# Supplier parts linked to Manufacturer parts
 | 
			
		||||
- model: company.supplierpart
 | 
			
		||||
  pk: 10
 | 
			
		||||
  fields:
 | 
			
		||||
    part: 3
 | 
			
		||||
    manufacturer_part: 2
 | 
			
		||||
    supplier: 2
 | 
			
		||||
    SKU: 'MPN456-APPEL'
 | 
			
		||||
 | 
			
		||||
- model: company.supplierpart
 | 
			
		||||
  pk: 11
 | 
			
		||||
  fields:
 | 
			
		||||
    part: 3
 | 
			
		||||
    manufacturer_part: 2
 | 
			
		||||
    supplier: 3
 | 
			
		||||
    SKU: 'MPN456-ZERG'
 | 
			
		||||
 
 | 
			
		||||
@@ -338,7 +338,7 @@ class ManufacturerPart(models.Model):
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @classmethod
 | 
			
		||||
    def create(cls, part, manufacturer, mpn, link, description):
 | 
			
		||||
    def create(cls, part, manufacturer, mpn, description, link=None):
 | 
			
		||||
        """ Check if ManufacturerPart instance does not already exist
 | 
			
		||||
            then create it
 | 
			
		||||
        """
 | 
			
		||||
@@ -415,8 +415,7 @@ class SupplierPart(models.Model):
 | 
			
		||||
                manufacturer_part = ManufacturerPart.create(part=self.part,
 | 
			
		||||
                                                            manufacturer=manufacturer,
 | 
			
		||||
                                                            mpn=MPN,
 | 
			
		||||
                                                            description=self.description,
 | 
			
		||||
                                                            link=self.link)
 | 
			
		||||
                                                            description=self.description)
 | 
			
		||||
                self.manufacturer_part = manufacturer_part
 | 
			
		||||
            else:
 | 
			
		||||
                # Update ManufacturerPart (if ID exists)
 | 
			
		||||
 
 | 
			
		||||
@@ -162,6 +162,8 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
 | 
			
		||||
    
 | 
			
		||||
    MPN = serializers.StringRelatedField(source='manufacturer_part.MPN')
 | 
			
		||||
 | 
			
		||||
    manufacturer_part = ManufacturerPartSerializer(read_only=True)
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = SupplierPart
 | 
			
		||||
        fields = [
 | 
			
		||||
@@ -175,6 +177,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
 | 
			
		||||
            'manufacturer',
 | 
			
		||||
            'MPN',
 | 
			
		||||
            'manufacturer_detail',
 | 
			
		||||
            'manufacturer_part',
 | 
			
		||||
            'description',
 | 
			
		||||
            'link',
 | 
			
		||||
        ]
 | 
			
		||||
@@ -190,25 +193,10 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
 | 
			
		||||
        MPN = self.initial_data.get('MPN', None)
 | 
			
		||||
 | 
			
		||||
        if manufacturer_id or MPN:
 | 
			
		||||
            # Get SupplierPart data
 | 
			
		||||
            part = validated_data.get('part', None)
 | 
			
		||||
            description = validated_data.get('description', None)
 | 
			
		||||
            link = validated_data.get('link', None)
 | 
			
		||||
 | 
			
		||||
            # Get manufacturer
 | 
			
		||||
            try:
 | 
			
		||||
                manufacturer = Company.objects.get(pk=int(manufacturer_id))
 | 
			
		||||
            except Company.DoesNotExist:
 | 
			
		||||
                manufacturer = None
 | 
			
		||||
 | 
			
		||||
            # Create ManufacturerPart
 | 
			
		||||
            manufacturer_part = ManufacturerPart.create(part=part,
 | 
			
		||||
                                                        manufacturer=manufacturer,
 | 
			
		||||
                                                        mpn=MPN,
 | 
			
		||||
                                                        description=description,
 | 
			
		||||
                                                        link=link)
 | 
			
		||||
            supplier_part.manufacturer_part = manufacturer_part
 | 
			
		||||
            supplier_part.save()
 | 
			
		||||
            kwargs = {'manufacturer': manufacturer_id,
 | 
			
		||||
                      'MPN': MPN,
 | 
			
		||||
                      }
 | 
			
		||||
            supplier_part.save(**kwargs)
 | 
			
		||||
 | 
			
		||||
        return supplier_part
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -33,7 +33,7 @@
 | 
			
		||||
        "{% url 'api-manufacturer-part-list' %}",
 | 
			
		||||
        {
 | 
			
		||||
            params: {
 | 
			
		||||
                part: {{ part.id }},
 | 
			
		||||
                supplier_part: {{ part.id }},
 | 
			
		||||
                part_detail: true,
 | 
			
		||||
                manufacturer_detail: true,
 | 
			
		||||
            },
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user