Only connect ManufacturerPart if a. it exists b. SupplierPart is not yet connected

This commit is contained in:
eeintech 2021-08-11 17:08:46 -04:00
parent a2590f1a3b
commit 91e314ddb5
3 changed files with 26 additions and 49 deletions

View File

@ -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_id = kwargs.pop('manufacturer')
try:
manufacturer = Company.objects.get(pk=int(manufacturer_id))
except (ValueError, Company.DoesNotExist):
manufacturer = None
else:
manufacturer = None
if 'MPN' in kwargs:
MPN = kwargs.pop('MPN')
else:
MPN = None
if manufacturer or MPN:
if not self.manufacturer_part:
# Create ManufacturerPart
manufacturer_part = ManufacturerPart.create(part=self.part,
manufacturer=manufacturer,
mpn=MPN,
description=self.description)
self.manufacturer_part = manufacturer_part
else:
# Update ManufacturerPart (if ID exists)
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 manufacturer_part = None
raise ValidationError(f'ManufacturerPart linked to {self.part} from manufacturer {manufacturer.name}'
f'with part number {MPN} already exists!') if all(key in kwargs for key in ('manufacturer', 'MPN')):
manufacturer_name = kwargs.pop('manufacturer')
MPN = kwargs.pop('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 manufacturer_part:
if not self.manufacturer_part:
# Connect ManufacturerPart to SupplierPart
self.manufacturer_part = manufacturer_part self.manufacturer_part = manufacturer_part
else:
raise ValidationError(f'SupplierPart {self.__str__} is already linked to {self.manufacturer_part}')
self.clean() self.clean()
self.validate_unique() self.validate_unique()

View File

@ -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,11 +238,12 @@ 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 = {
'manufacturer': manufacturer,
'MPN': MPN, 'MPN': MPN,
} }
supplier_part.save(**kwargs) supplier_part.save(**kwargs)

View File

@ -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) {