From c706f007331bcb636a8e9f0ec75a7906c47645ce Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 16 Apr 2019 14:22:27 +1000 Subject: [PATCH] Skeletong for exporting BOM in different formats --- InvenTree/part/models.py | 67 ++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 54d5f32a9d..2e7dd7b7c5 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -281,40 +281,81 @@ 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 + def export_bom(self, **kwargs): - lines = [] - - # Construct header line + # Construct the export data header = [] header.append('Part') header.append('Description') header.append('Quantity') header.append('Note') - - lines.append(header) + + rows = [] for it in self.bom_items.all(): line = [] - + line.append(it.sub_part.name) line.append(it.sub_part.description) line.append(it.quantity) line.append(it.note) - lines.append([str(x) for x in line]) + rows.append([str(x) for x in line]) + + file_format = kwargs.get('file_format', 'csv').lower() + + kwargs['header'] = header + kwargs['rows'] = rows + + if file_format == 'csv': + return self.export_bom_csv(**kwargs) + elif file_format in ['xls', 'xlsx']: + return self.export_bom_xls(**kwargs) + elif file_format == 'xml': + return self.export_bom_xml(**kwargs) + elif file_format in ['htm', 'html']: + return self.export_bom_htm(**kwargs) + elif file_format == 'pdf': + return self.export_bom_pdf(**kwargs) + else: + return None + + + def export_bom_csv(self, **kwargs): + + # Construct header line + header = kwargs.get('header') + rows = kwargs.get('rows') # TODO - Choice of formatters goes here? - out = '' + out = ','.join(header) - for line in lines: - print(line) - out += ','.join(line) + for row in rows: out += '\n' + out += ','.join(row) return out + + def export_bom_xls(self, **kwargs): + + return '' + + + def export_bom_xml(self, **kwargs): + return '' + + + def export_bom_htm(self, **kwargs): + return '' + + + def export_bom_pdf(self, **kwargs): + return '' + + + + """ @property def projects(self):