mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Only connect ManufacturerPart if a. it exists b. SupplierPart is not yet connected
This commit is contained in:
		@@ -12,7 +12,6 @@ from django.core.validators import MinValueValidator
 | 
				
			|||||||
from django.core.exceptions import ValidationError
 | 
					from django.core.exceptions import ValidationError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.db import models
 | 
					from django.db import models
 | 
				
			||||||
from django.db.utils import IntegrityError
 | 
					 | 
				
			||||||
from django.db.models import Sum, Q, UniqueConstraint
 | 
					from django.db.models import Sum, Q, UniqueConstraint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.apps import apps
 | 
					from django.apps import apps
 | 
				
			||||||
@@ -503,50 +502,27 @@ class SupplierPart(models.Model):
 | 
				
			|||||||
                })
 | 
					                })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def save(self, *args, **kwargs):
 | 
					    def save(self, *args, **kwargs):
 | 
				
			||||||
        """ Overriding save method to process the linked ManufacturerPart
 | 
					        """ Overriding save method to connect an existing ManufacturerPart """
 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if 'manufacturer' in kwargs:
 | 
					        manufacturer_part = None
 | 
				
			||||||
            manufacturer_id = kwargs.pop('manufacturer')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            try:
 | 
					        if all(key in kwargs for key in ('manufacturer', 'MPN')):
 | 
				
			||||||
                manufacturer = Company.objects.get(pk=int(manufacturer_id))
 | 
					            manufacturer_name = kwargs.pop('manufacturer')
 | 
				
			||||||
            except (ValueError, Company.DoesNotExist):
 | 
					 | 
				
			||||||
                manufacturer = None
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            manufacturer = None
 | 
					 | 
				
			||||||
        if 'MPN' in kwargs:
 | 
					 | 
				
			||||||
            MPN = kwargs.pop('MPN')
 | 
					            MPN = kwargs.pop('MPN')
 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            MPN = None
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if manufacturer or MPN:
 | 
					            # Retrieve manufacturer part
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                manufacturer_part = ManufacturerPart.objects.get(manufacturer__name=manufacturer_name, MPN=MPN)
 | 
				
			||||||
 | 
					            except (ValueError, Company.DoesNotExist):
 | 
				
			||||||
 | 
					                # ManufacturerPart does not exist
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if manufacturer_part:
 | 
				
			||||||
            if not self.manufacturer_part:
 | 
					            if not self.manufacturer_part:
 | 
				
			||||||
                # Create ManufacturerPart
 | 
					                # Connect ManufacturerPart to SupplierPart
 | 
				
			||||||
                manufacturer_part = ManufacturerPart.create(part=self.part,
 | 
					 | 
				
			||||||
                                                            manufacturer=manufacturer,
 | 
					 | 
				
			||||||
                                                            mpn=MPN,
 | 
					 | 
				
			||||||
                                                            description=self.description)
 | 
					 | 
				
			||||||
                self.manufacturer_part = manufacturer_part
 | 
					                self.manufacturer_part = manufacturer_part
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                # Update ManufacturerPart (if ID exists)
 | 
					                raise ValidationError(f'SupplierPart {self.__str__} is already linked to {self.manufacturer_part}')
 | 
				
			||||||
                try:
 | 
					 | 
				
			||||||
                    manufacturer_part_id = self.manufacturer_part.id
 | 
					 | 
				
			||||||
                except AttributeError:
 | 
					 | 
				
			||||||
                    manufacturer_part_id = None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if manufacturer_part_id:
 | 
					 | 
				
			||||||
                    try:
 | 
					 | 
				
			||||||
                        (manufacturer_part, created) = ManufacturerPart.objects.update_or_create(part=self.part,
 | 
					 | 
				
			||||||
                                                                                                 manufacturer=manufacturer,
 | 
					 | 
				
			||||||
                                                                                                 MPN=MPN)
 | 
					 | 
				
			||||||
                    except IntegrityError:
 | 
					 | 
				
			||||||
                        manufacturer_part = None
 | 
					 | 
				
			||||||
                        raise ValidationError(f'ManufacturerPart linked to {self.part} from manufacturer {manufacturer.name}'
 | 
					 | 
				
			||||||
                                              f'with part number {MPN} already exists!')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if manufacturer_part:
 | 
					 | 
				
			||||||
                    self.manufacturer_part = manufacturer_part
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.clean()
 | 
					        self.clean()
 | 
				
			||||||
        self.validate_unique()
 | 
					        self.validate_unique()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -204,9 +204,9 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
 | 
					    supplier = serializers.PrimaryKeyRelatedField(queryset=Company.objects.filter(is_supplier=True))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    manufacturer = serializers.PrimaryKeyRelatedField(source='manufacturer_part.manufacturer', read_only=True)
 | 
					    manufacturer = serializers.CharField(read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    MPN = serializers.StringRelatedField(source='manufacturer_part.MPN')
 | 
					    MPN = serializers.CharField(read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    manufacturer_part_detail = ManufacturerPartSerializer(source='manufacturer_part', read_only=True)
 | 
					    manufacturer_part_detail = ManufacturerPartSerializer(source='manufacturer_part', read_only=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -238,13 +238,14 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
 | 
				
			|||||||
        supplier_part = super().create(validated_data)
 | 
					        supplier_part = super().create(validated_data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Get ManufacturerPart raw data (unvalidated)
 | 
					        # Get ManufacturerPart raw data (unvalidated)
 | 
				
			||||||
        manufacturer_id = self.initial_data.get('manufacturer', None)
 | 
					        manufacturer = self.initial_data.get('manufacturer', None)
 | 
				
			||||||
        MPN = self.initial_data.get('MPN', None)
 | 
					        MPN = self.initial_data.get('MPN', None)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if manufacturer_id or MPN:
 | 
					        if manufacturer and MPN:
 | 
				
			||||||
            kwargs = {'manufacturer': manufacturer_id,
 | 
					            kwargs = {
 | 
				
			||||||
                      'MPN': MPN,
 | 
					                'manufacturer': manufacturer,
 | 
				
			||||||
                      }
 | 
					                'MPN': MPN,
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            supplier_part.save(**kwargs)
 | 
					            supplier_part.save(**kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return supplier_part
 | 
					        return supplier_part
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -744,12 +744,12 @@ function loadSupplierPartTable(table, url, options) {
 | 
				
			|||||||
                visible: params['manufacturer_detail'],
 | 
					                visible: params['manufacturer_detail'],
 | 
				
			||||||
                switchable: params['manufacturer_detail'],
 | 
					                switchable: params['manufacturer_detail'],
 | 
				
			||||||
                sortable: true,
 | 
					                sortable: true,
 | 
				
			||||||
                field: 'manufacturer',
 | 
					                field: 'manufacturer_detail.name',
 | 
				
			||||||
                title: '{% trans "Manufacturer" %}',
 | 
					                title: '{% trans "Manufacturer" %}',
 | 
				
			||||||
                formatter: function(value, row, index, field) {
 | 
					                formatter: function(value, row, index, field) {
 | 
				
			||||||
                    if (value && row.manufacturer_detail) {
 | 
					                    if (value && row.manufacturer_detail) {
 | 
				
			||||||
                        var name = row.manufacturer_detail.name;
 | 
					                        var name = value;
 | 
				
			||||||
                        var url = `/company/${value}/`;
 | 
					                        var url = `/company/${row.manufacturer_detail.pk}/`;
 | 
				
			||||||
                        var html = imageHoverIcon(row.manufacturer_detail.image) + renderLink(name, url);
 | 
					                        var html = imageHoverIcon(row.manufacturer_detail.image) + renderLink(name, url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        return html;
 | 
					                        return html;
 | 
				
			||||||
@@ -762,7 +762,7 @@ function loadSupplierPartTable(table, url, options) {
 | 
				
			|||||||
                visible: params['manufacturer_detail'],
 | 
					                visible: params['manufacturer_detail'],
 | 
				
			||||||
                switchable: params['manufacturer_detail'],
 | 
					                switchable: params['manufacturer_detail'],
 | 
				
			||||||
                sortable: true,
 | 
					                sortable: true,
 | 
				
			||||||
                field: 'MPN',
 | 
					                field: 'manufacturer_part_detail.MPN',
 | 
				
			||||||
                title: '{% trans "MPN" %}',
 | 
					                title: '{% trans "MPN" %}',
 | 
				
			||||||
                formatter: function(value, row, index, field) {
 | 
					                formatter: function(value, row, index, field) {
 | 
				
			||||||
                    if (value && row.manufacturer_part) {
 | 
					                    if (value && row.manufacturer_part) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user