From 7117c333790966878008133afe61c3ccff4464ed Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 10 Aug 2021 10:58:11 +1000 Subject: [PATCH 1/3] Raise validation error if the manufacturer part does not point to the correct part --- InvenTree/company/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 3b731381b8..b097de8cf3 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -9,6 +9,8 @@ import os from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinValueValidator +from django.core.exceptions import ValidationError + from django.db import models from django.db.models import Sum, Q, UniqueConstraint @@ -479,6 +481,18 @@ class SupplierPart(models.Model): # This model was moved from the 'Part' app db_table = 'part_supplierpart' + def clean(self): + + super().clean() + + # Ensure that the linked manufacturer_part points to the same part! + if self.manufacturer_part and self.part: + + if not self.manufacturer_part.part == self.part: + raise ValidationError({ + 'manufacturer_part': _("Linked manufacturer part must reference the same base part"), + }) + part = models.ForeignKey('part.Part', on_delete=models.CASCADE, related_name='supplier_parts', verbose_name=_('Base Part'), From e03afce33516d6da135113a33ab1ac72034d92fb Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 10 Aug 2021 11:00:27 +1000 Subject: [PATCH 2/3] Add "instance_filters" for SupplierPart model - Restrict the "manufacturer_part" query based on the current part --- InvenTree/company/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index b097de8cf3..a3c2a34868 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -475,6 +475,15 @@ class SupplierPart(models.Model): def get_absolute_url(self): return reverse('supplier-part-detail', kwargs={'pk': self.id}) + def api_instance_filters(self): + + return { + 'manufacturer_part': { + 'part': self.part.pk + } + } + + class Meta: unique_together = ('part', 'supplier', 'SKU') From c32b6b227237d460bf0d80f3c3cdd6f17439c299 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 10 Aug 2021 11:01:17 +1000 Subject: [PATCH 3/3] Hide the "part" field when editing manufacturer part and supplier part objects --- InvenTree/company/models.py | 1 - InvenTree/templates/js/translated/company.js | 13 +++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index a3c2a34868..2531781631 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -483,7 +483,6 @@ class SupplierPart(models.Model): } } - class Meta: unique_together = ('part', 'supplier', 'SKU') diff --git a/InvenTree/templates/js/translated/company.js b/InvenTree/templates/js/translated/company.js index 9c73fcc111..4ecea2c595 100644 --- a/InvenTree/templates/js/translated/company.js +++ b/InvenTree/templates/js/translated/company.js @@ -54,8 +54,12 @@ function editManufacturerPart(part, options={}) { var url = `/api/company/part/manufacturer/${part}/`; + var fields = manufacturerPartFields(); + + fields.part.hidden = true; + constructForm(url, { - fields: manufacturerPartFields(), + fields: fields, title: '{% trans "Edit Manufacturer Part" %}', onSuccess: options.onSuccess }); @@ -157,8 +161,13 @@ function createSupplierPart(options={}) { function editSupplierPart(part, options={}) { + var fields = supplierPartFields(); + + // Hide the "part" field + fields.part.hidden = true; + constructForm(`/api/company/part/${part}/`, { - fields: supplierPartFields(), + fields: fields, title: '{% trans "Edit Supplier Part" %}', onSuccess: options.onSuccess });