Cleanup get_required_parts function

This commit is contained in:
Oliver Walters 2020-10-23 22:49:46 +11:00
parent 5db043ab4b
commit 076d5c4f7f
4 changed files with 34 additions and 15 deletions

View File

@ -584,7 +584,7 @@ class BuildItem(models.Model):
errors = {} errors = {}
try: try:
if self.stock_item.part not in self.build.part.required_parts(): if self.stock_item.part not in self.build.part.getRequiredParts(recursive=False):
errors['stock_item'] = [_("Selected stock item not found in BOM for part '{p}'".format(p=self.build.part.full_name))] errors['stock_item'] = [_("Selected stock item not found in BOM for part '{p}'".format(p=self.build.part.full_name))]
if self.quantity > self.stock_item.quantity: if self.quantity > self.stock_item.quantity:

View File

@ -591,13 +591,20 @@ class BuildItemCreate(AjaxCreateView):
pass pass
# Exclude StockItem objects which are already allocated to this build and part # Exclude StockItem objects which are already allocated to this build and part
stock_filter = stock_filter.exclude(id__in=[item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)]) stock_filter = stock_filter.exclude(
id__in=[
item.stock_item.id for item in BuildItem.objects.filter(build=build_id, stock_item__part=part_id)
]
)
except Part.DoesNotExist: except Part.DoesNotExist:
self.part = None self.part = None
pass pass
form.fields['stock_item'].query = stock_filter else:
self.part = None
form.fields['stock_item'].queryset = stock_filter
self.available_stock = stock_filter.all() self.available_stock = stock_filter.all()
@ -708,13 +715,12 @@ class BuildItemEdit(AjaxUpdateView):
} }
def get_form(self): def get_form(self):
""" Create form for editing a BuildItem. """
Create form for editing a BuildItem.
- Limit the StockItem options to items that match the part - Limit the StockItem options to items that match the part
""" """
build_item = self.get_object()
form = super(BuildItemEdit, self).get_form() form = super(BuildItemEdit, self).get_form()
# Hide fields which we do not wish the user to edit # Hide fields which we do not wish the user to edit

View File

@ -931,12 +931,25 @@ class Part(MPTTModel):
self.bom_items.all().delete() self.bom_items.all().delete()
def required_parts(self): def getRequiredParts(self, recursive=False, parts=set()):
""" Return a list of parts required to make this part (list of BOM items) """ """
parts = [] Return a list of parts required to make this part (i.e. BOM items).
for bom in self.bom_items.all().select_related('sub_part'): Args:
parts.append(bom.sub_part) recursive: If True iterate down through sub-assemblies
parts: Set of parts already found (to prevent recursion issues)
"""
for bom_item in self.bom_items.all().select_related('sub_part'):
sub_part = bom_item.sub_part
if sub_part not in parts:
parts.add(sub_part)
if recursive:
sub_part.getRequiredParts(recursive=True, parts=parts)
return parts return parts

View File

@ -2127,7 +2127,7 @@ class BomItemCreate(AjaxCreateView):
query = query.filter(active=True) query = query.filter(active=True)
# Eliminate any options that are already in the BOM! # Eliminate any options that are already in the BOM!
query = query.exclude(id__in=[item.id for item in part.required_parts()]) query = query.exclude(id__in=[item.id for item in part.getRequiredParts()])
form.fields['sub_part'].queryset = query form.fields['sub_part'].queryset = query
@ -2195,7 +2195,7 @@ class BomItemEdit(AjaxUpdateView):
except ValueError: except ValueError:
sub_part_id = -1 sub_part_id = -1
existing = [item.pk for item in part.required_parts()] existing = [item.pk for item in part.getRequiredParts()]
if sub_part_id in existing: if sub_part_id in existing:
existing.remove(sub_part_id) existing.remove(sub_part_id)