Merge pull request #905 from SchrodingersGat/bom-depth

Allow user to set "depth" of exported multi-level BOM
This commit is contained in:
Oliver 2020-08-16 09:29:09 +10:00 committed by GitHub
commit eb2422e6be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -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 """

View File

@ -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,