Require Build and StockItem to be unique_together in BuildItem class

This commit is contained in:
Oliver Walters 2019-04-29 22:56:40 +10:00
parent f42116c0d9
commit 17d9d25fb9
3 changed files with 54 additions and 3 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2 on 2019-04-29 12:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('stock', '0009_auto_20190428_0841'),
('build', '0006_auto_20190429_2233'),
]
operations = [
migrations.AlterUniqueTogether(
name='builditem',
unique_together={('build', 'stock_item')},
),
]

View File

@ -6,6 +6,7 @@ Build database model definitions
from __future__ import unicode_literals from __future__ import unicode_literals
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError
from django.urls import reverse from django.urls import reverse
from django.db import models from django.db import models
@ -128,6 +129,26 @@ class BuildItem(models.Model):
quantity: Number of units allocated quantity: Number of units allocated
""" """
class Meta:
unique_together = [
('build', 'stock_item'),
]
def clean(self):
""" Check validity of the BuildItem model.
The following checks are performed:
- StockItem.part must be in the BOM of the Part object referenced by Build
"""
if self.stock_item.part not in self.build.part.required_parts():
print('stock_item:', self.stock_item.part)
for p in self.build.part.bom_items.all():
print('bom_part:', p)
raise ValidationError(
{'stock_item': _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part)))}
)
build = models.ForeignKey( build = models.ForeignKey(
Build, Build,
on_delete=models.CASCADE, on_delete=models.CASCADE,

View File

@ -6,11 +6,11 @@ JSON serializers for Build API
from __future__ import unicode_literals from __future__ import unicode_literals
from rest_framework import serializers from rest_framework import serializers
from InvenTree.serializers import InvenTreeModelSerializer
from .models import Build from .models import Build, BuildItem
class BuildSerializer(InvenTreeModelSerializer):
class BuildSerializer(serializers.ModelSerializer):
""" Serializes a Build object """ """ Serializes a Build object """
url = serializers.CharField(source='get_absolute_url', read_only=True) url = serializers.CharField(source='get_absolute_url', read_only=True)
@ -29,3 +29,15 @@ class BuildSerializer(serializers.ModelSerializer):
'status', 'status',
'status_text', 'status_text',
'notes'] 'notes']
class BuildItemSerializer(InvenTreeModelSerializer):
""" Serializes a BuildItem object """
class Meta:
model = BuildItem
fields = [
'build',
'stock',
'quantity'
]