Move the export_bom function to the part model

This commit is contained in:
Oliver Walters 2019-04-14 00:50:43 +10:00
parent b8e28c003d
commit 9e5eed5d5e
2 changed files with 33 additions and 9 deletions

View File

@ -278,6 +278,38 @@ class Part(models.Model):
# Return the number of supplier parts available for this part
return self.supplier_parts.count()
def export_bom(self, file_format='csv'):
# TODO - Allow other file formats
lines = []
# Construct header line
header = []
header.append('Part')
header.append('Description')
header.append('Quantity')
lines.append(header)
for it in self.bom_items.all():
line = []
line.append(it.sub_part.name)
line.append(it.sub_part.description)
line.append(it.quantity)
lines.append([str(x) for x in line])
# TODO - Choice of formatters goes here?
out = ''
for line in lines:
print(line)
out += ','.join(line)
out += '\n'
return out
"""
@property
def projects(self):

View File

@ -131,15 +131,7 @@ class BomExport(AjaxView):
# Placeholder to test file export
filename = '"' + part.name + '_BOM.' + export_format + '"'
filedata = "Part,Quantity,Available\n"
for bom_item in part.bom_items.all():
filedata += bom_item.sub_part.name
filedata += ","
filedata += str(bom_item.quantity)
filedata += ","
filedata += str(bom_item.sub_part.available_stock)
filedata += "\n"
filedata = part.export_bom()
return DownloadFile(filedata, filename)