mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Merge pull request #905 from SchrodingersGat/bom-depth
Allow user to set "depth" of exported multi-level BOM
This commit is contained in:
commit
eb2422e6be
@ -40,7 +40,7 @@ def MakeBomTemplate(fmt):
|
|||||||
return DownloadFile(data, filename)
|
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.
|
""" Export a BOM (Bill of Materials) for a given part.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -68,7 +68,8 @@ def ExportBom(part, fmt='csv', cascade=False):
|
|||||||
bom_items.append(item)
|
bom_items.append(item)
|
||||||
|
|
||||||
if item.sub_part.assembly:
|
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:
|
if cascade:
|
||||||
# Cascading (multi-level) BOM
|
# Cascading (multi-level) BOM
|
||||||
|
@ -56,6 +56,8 @@ class BomExportForm(forms.Form):
|
|||||||
|
|
||||||
cascading = forms.BooleanField(label=_("Cascading"), required=False, initial=False, help_text=_("Download cascading / multi-level BOM"))
|
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):
|
def get_choices(self):
|
||||||
""" BOM export format choices """
|
""" BOM export format choices """
|
||||||
|
|
||||||
|
@ -1392,10 +1392,22 @@ class BomDownload(AjaxView):
|
|||||||
|
|
||||||
cascade = str2bool(request.GET.get('cascade', False))
|
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):
|
if not IsValidBOMFormat(export_format):
|
||||||
export_format = 'csv'
|
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):
|
def get_data(self):
|
||||||
return {
|
return {
|
||||||
@ -1419,6 +1431,7 @@ class BomExport(AjaxView):
|
|||||||
# Extract POSTed form data
|
# Extract POSTed form data
|
||||||
fmt = request.POST.get('file_format', 'csv').lower()
|
fmt = request.POST.get('file_format', 'csv').lower()
|
||||||
cascade = str2bool(request.POST.get('cascading', False))
|
cascade = str2bool(request.POST.get('cascading', False))
|
||||||
|
levels = request.POST.get('levels', None)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
part = Part.objects.get(pk=self.kwargs['pk'])
|
part = Part.objects.get(pk=self.kwargs['pk'])
|
||||||
@ -1434,6 +1447,9 @@ class BomExport(AjaxView):
|
|||||||
url += '?file_format=' + fmt
|
url += '?file_format=' + fmt
|
||||||
url += '&cascade=' + str(cascade)
|
url += '&cascade=' + str(cascade)
|
||||||
|
|
||||||
|
if levels:
|
||||||
|
url += '&levels=' + str(levels)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'form_valid': part is not None,
|
'form_valid': part is not None,
|
||||||
'url': url,
|
'url': url,
|
||||||
|
Loading…
Reference in New Issue
Block a user