diff --git a/InvenTree/part/bom.py b/InvenTree/part/bom.py index 16a8b0b16d..47f6bd082e 100644 --- a/InvenTree/part/bom.py +++ b/InvenTree/part/bom.py @@ -40,7 +40,7 @@ def MakeBomTemplate(fmt): return DownloadFile(data, filename) -def ExportBom(part, fmt='csv', cascade=False): +def ExportBom(part, fmt='csv', cascade=False, max_levels=None): """ Export a BOM (Bill of Materials) for a given part. Args: @@ -68,7 +68,8 @@ def ExportBom(part, fmt='csv', cascade=False): bom_items.append(item) if item.sub_part.assembly: - add_items(item.sub_part.bom_items.all().order_by('id'), level + 1) + if max_levels is None or level < max_levels: + add_items(item.sub_part.bom_items.all().order_by('id'), level + 1) if cascade: # Cascading (multi-level) BOM diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 986749f7b3..dbbced352b 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -56,6 +56,8 @@ class BomExportForm(forms.Form): cascading = forms.BooleanField(label=_("Cascading"), required=False, initial=False, help_text=_("Download cascading / multi-level BOM")) + levels = forms.IntegerField(label=_("Levels"), required=True, initial=0, help_text=_("Select maximum number of BOM levels to export (0 = all levels)")) + def get_choices(self): """ BOM export format choices """ diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index eda1db923b..9ae5cc8026 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -1392,10 +1392,22 @@ class BomDownload(AjaxView): cascade = str2bool(request.GET.get('cascade', False)) + levels = request.GET.get('levels', None) + + if levels is not None: + try: + levels = int(levels) + + if levels <= 0: + levels = None + + except ValueError: + levels = None + if not IsValidBOMFormat(export_format): export_format = 'csv' - return ExportBom(part, fmt=export_format, cascade=cascade) + return ExportBom(part, fmt=export_format, cascade=cascade, max_levels=levels) def get_data(self): return { @@ -1419,6 +1431,7 @@ class BomExport(AjaxView): # Extract POSTed form data fmt = request.POST.get('file_format', 'csv').lower() cascade = str2bool(request.POST.get('cascading', False)) + levels = request.POST.get('levels', None) try: part = Part.objects.get(pk=self.kwargs['pk']) @@ -1434,6 +1447,9 @@ class BomExport(AjaxView): url += '?file_format=' + fmt url += '&cascade=' + str(cascade) + if levels: + url += '&levels=' + str(levels) + data = { 'form_valid': part is not None, 'url': url,