Improve edit form for BuildItem

- Hide the build field
- Limit the choices for StockItem (don't let user change the allocation!)
This commit is contained in:
Oliver Walters 2019-05-02 00:25:19 +10:00
parent a4aec425be
commit 09b7846310
2 changed files with 26 additions and 4 deletions

View File

@ -260,13 +260,13 @@ class BuildItem(models.Model):
if self.stock_item is not None and self.stock_item.part is not None: if self.stock_item is not None and self.stock_item.part is not None:
if self.stock_item.part not in self.build.part.required_parts(): if self.stock_item.part not in self.build.part.required_parts():
errors['stock_item'] = _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part))) errors['stock_item'] = [_("Selected stock item not found in BOM for part '{p}'".format(p=self.build.part.name))]
if self.stock_item is not None and self.quantity > self.stock_item.quantity: if self.stock_item is not None and self.quantity > self.stock_item.quantity:
errors['quantity'] = _("Allocated quantity ({n}) must not exceed available quantity ({q})".format( errors['quantity'] = [_("Allocated quantity ({n}) must not exceed available quantity ({q})".format(
n=self.quantity, n=self.quantity,
q=self.stock_item.quantity q=self.stock_item.quantity
)) ))]
if len(errors) > 0: if len(errors) > 0:
raise ValidationError(errors) raise ValidationError(errors)

View File

@ -13,7 +13,7 @@ from django.forms import HiddenInput
from part.models import Part from part.models import Part
from .models import Build, BuildItem from .models import Build, BuildItem
from .forms import EditBuildForm, EditBuildItemForm, CompleteBuildForm from .forms import EditBuildForm, EditBuildItemForm, CompleteBuildForm
from stock.models import StockLocation from stock.models import StockLocation, StockItem
from InvenTree.views import AjaxView, AjaxUpdateView, AjaxCreateView, AjaxDeleteView from InvenTree.views import AjaxView, AjaxUpdateView, AjaxCreateView, AjaxDeleteView
@ -328,3 +328,25 @@ class BuildItemEdit(AjaxUpdateView):
return { return {
'info': 'Updated Build Item', 'info': 'Updated Build Item',
} }
def get_form(self):
""" Create form for editing a BuildItem.
- Limit the StockItem options to items that match the part
"""
build_item = self.get_object()
form = super(BuildItemEdit, self).get_form()
query = StockItem.objects.all()
if build_item.stock_item:
part_id = build_item.stock_item.part.id
query = query.filter(part=part_id)
form.fields['stock_item'].queryset = query
form.fields['build'].widget = HiddenInput()
return form