mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Validating API for SupplierPart, not able to spin-off ManufacturerPart from serialized data
This commit is contained in:
parent
9e56bf90c5
commit
8c8b25a0d2
@ -11,7 +11,9 @@ import math
|
|||||||
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.core.validators import MinValueValidator
|
from django.core.validators import MinValueValidator
|
||||||
|
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
|
||||||
@ -389,6 +391,58 @@ class SupplierPart(models.Model):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('supplier-part-detail', kwargs={'pk': self.id})
|
return reverse('supplier-part-detail', kwargs={'pk': self.id})
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
""" Overriding save method to process the linked 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,
|
||||||
|
link=self.link)
|
||||||
|
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
|
||||||
|
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.validate_unique()
|
||||||
|
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('part', 'supplier', 'SKU')
|
unique_together = ('part', 'supplier', 'SKU')
|
||||||
|
|
||||||
@ -459,6 +513,7 @@ class SupplierPart(models.Model):
|
|||||||
|
|
||||||
items = []
|
items = []
|
||||||
|
|
||||||
|
if self.manufacturer_part:
|
||||||
if self.manufacturer_part.manufacturer:
|
if self.manufacturer_part.manufacturer:
|
||||||
items.append(self.manufacturer_part.manufacturer.name)
|
items.append(self.manufacturer_part.manufacturer.name)
|
||||||
if self.manufacturer_part.MPN:
|
if self.manufacturer_part.MPN:
|
||||||
|
@ -158,7 +158,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', queryset=Company.objects.filter(is_manufacturer=True))
|
# manufacturer_part = ManufacturerPartSerializer(many=False, read_only=True)
|
||||||
|
|
||||||
|
manufacturer = serializers.PrimaryKeyRelatedField(source='manufacturer_part.manufacturer', read_only=True) # queryset=Company.objects.filter(is_manufacturer=True))
|
||||||
|
|
||||||
MPN = serializers.StringRelatedField(source='manufacturer_part.MPN', read_only=True)
|
MPN = serializers.StringRelatedField(source='manufacturer_part.MPN', read_only=True)
|
||||||
|
|
||||||
@ -172,6 +174,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
|
|||||||
'supplier',
|
'supplier',
|
||||||
'supplier_detail',
|
'supplier_detail',
|
||||||
'SKU',
|
'SKU',
|
||||||
|
# 'manufacturer_part',
|
||||||
'manufacturer',
|
'manufacturer',
|
||||||
'MPN',
|
'MPN',
|
||||||
'manufacturer_detail',
|
'manufacturer_detail',
|
||||||
@ -179,6 +182,37 @@ class SupplierPartSerializer(InvenTreeModelSerializer):
|
|||||||
'link',
|
'link',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
""" Extract manufacturer data and process ManufacturerPart """
|
||||||
|
|
||||||
|
print(validated_data)
|
||||||
|
|
||||||
|
# Create SupplierPart
|
||||||
|
supplier_part = super().create(validated_data)
|
||||||
|
|
||||||
|
# Get ManufacturerPart data
|
||||||
|
part = validated_data.get('part', None)
|
||||||
|
manufacturer = validated_data.get('manufacturer', None)
|
||||||
|
MPN = validated_data.get('MPN', None)
|
||||||
|
description = validated_data.get('description', None)
|
||||||
|
link = validated_data.get('link', None)
|
||||||
|
|
||||||
|
print(f'{manufacturer} | {MPN}')
|
||||||
|
|
||||||
|
if manufacturer or MPN:
|
||||||
|
# Create ManufacturerPart
|
||||||
|
manufacturer_part = ManufacturerPart.create(part=part,
|
||||||
|
manufacturer=manufacturer,
|
||||||
|
mpn=MPN,
|
||||||
|
description=description,
|
||||||
|
link=link)
|
||||||
|
print(manufacturer_part)
|
||||||
|
supplier_part.manufacturer_part = manufacturer_part
|
||||||
|
print(supplier_part.manufacturer_part)
|
||||||
|
supplier_part.save()
|
||||||
|
|
||||||
|
return supplier_part
|
||||||
|
|
||||||
|
|
||||||
class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
|
class SupplierPriceBreakSerializer(InvenTreeModelSerializer):
|
||||||
""" Serializer for SupplierPriceBreak object """
|
""" Serializer for SupplierPriceBreak object """
|
||||||
|
@ -528,24 +528,14 @@ class SupplierPartEdit(AjaxUpdateView):
|
|||||||
ajax_form_title = _('Edit Supplier Part')
|
ajax_form_title = _('Edit Supplier Part')
|
||||||
|
|
||||||
def save(self, supplier_part, form, **kwargs):
|
def save(self, supplier_part, form, **kwargs):
|
||||||
""" Save ManufacturerPart data """
|
""" Process ManufacturerPart data """
|
||||||
|
|
||||||
# Save supplier part object
|
manufacturer = form.cleaned_data.get('manufacturer', None)
|
||||||
supplier_part = super().save(supplier_part, form)
|
|
||||||
|
|
||||||
# Save manufacturer part object
|
|
||||||
manufacturer_id = form.cleaned_data.get('manufacturer', None)
|
|
||||||
try:
|
|
||||||
manufacturer = Company.objects.get(pk=manufacturer_id)
|
|
||||||
except Company.DoesNotExist:
|
|
||||||
pass
|
|
||||||
MPN = form.cleaned_data.get('MPN', None)
|
MPN = form.cleaned_data.get('MPN', None)
|
||||||
|
kwargs = {'manufacturer': manufacturer,
|
||||||
manufacturer_part = supplier_part.manufacturer_part
|
'MPN': MPN,
|
||||||
manufacturer_part.manufacturer = manufacturer
|
}
|
||||||
manufacturer_part.MPN = MPN
|
supplier_part.save(**kwargs)
|
||||||
|
|
||||||
manufacturer_part.save()
|
|
||||||
|
|
||||||
def get_form(self):
|
def get_form(self):
|
||||||
form = super().get_form()
|
form = super().get_form()
|
||||||
@ -618,26 +608,16 @@ class SupplierPartCreate(AjaxCreateView):
|
|||||||
If single_pricing is defined, add a price break for quantity=1
|
If single_pricing is defined, add a price break for quantity=1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Process manufacturer data
|
|
||||||
part = form.cleaned_data.get('part', None)
|
|
||||||
manufacturer_id = form.cleaned_data.get('manufacturer', None)
|
|
||||||
|
|
||||||
manufacturer_part = None
|
|
||||||
if manufacturer_id:
|
|
||||||
manufacturer = Company.objects.get(pk=manufacturer_id)
|
|
||||||
MPN = form.cleaned_data.get('MPN', None)
|
|
||||||
description = form.cleaned_data.get('description', None)
|
|
||||||
link = form.cleaned_data.get('link', None)
|
|
||||||
|
|
||||||
manufacturer_part = ManufacturerPart.create(part=part, manufacturer=manufacturer, mpn=MPN, description=description, link=link)
|
|
||||||
|
|
||||||
# Save the supplier part object
|
# Save the supplier part object
|
||||||
supplier_part = super().save(form)
|
supplier_part = super().save(form)
|
||||||
|
|
||||||
if manufacturer_part:
|
# Process manufacturer data
|
||||||
# Link ManufacturerPart
|
manufacturer = form.cleaned_data.get('manufacturer', None)
|
||||||
supplier_part.manufacturer_part = manufacturer_part
|
MPN = form.cleaned_data.get('MPN', None)
|
||||||
supplier_part.save()
|
kwargs = {'manufacturer': manufacturer,
|
||||||
|
'MPN': MPN,
|
||||||
|
}
|
||||||
|
supplier_part.save(**kwargs)
|
||||||
|
|
||||||
single_pricing = form.cleaned_data.get('single_pricing', None)
|
single_pricing = form.cleaned_data.get('single_pricing', None)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user