From cb7e98aa1cbf7e6f51b3db57391f0a2bcf5b5074 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 17 Apr 2019 23:52:15 +1000 Subject: [PATCH] Add extra BOM export format options - Uses tablib (already used by django-import-export plugin) - Needs cleanup but works well - Problem exporting XLSX data (needs work!) --- InvenTree/part/models.py | 82 +++--------------------- InvenTree/static/script/inventree/bom.js | 5 +- requirements/base.txt | 1 + 3 files changed, 14 insertions(+), 74 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 1d2f447083..cc7810fb85 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -5,6 +5,8 @@ import os import math +import tablib + from django.utils.translation import gettext_lazy as _ from django.core.exceptions import ValidationError @@ -285,14 +287,12 @@ class Part(models.Model): def export_bom(self, **kwargs): - # Construct the export data - header = [] - header.append('Part') - header.append('Description') - header.append('Quantity') - header.append('Note') - - rows = [] + data = tablib.Dataset(headers=[ + 'Part', + 'Description', + 'Quantity', + 'Note', + ]) for it in self.bom_items.all(): line = [] @@ -302,73 +302,11 @@ class Part(models.Model): line.append(it.quantity) line.append(it.note) - rows.append([str(x) for x in line]) + data.append(line) file_format = kwargs.get('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 = ','.join(header) - - 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): - " Return a list of unique projects that this part is associated with. - A part may be used in zero or more projects. - " - - project_ids = set() - project_parts = self.projectpart_set.all() - - projects = [] - - for pp in project_parts: - if pp.project.id not in project_ids: - project_ids.add(pp.project.id) - projects.append(pp.project) - - return projects - """ + return data.export(file_format) def attach_file(instance, filename): diff --git a/InvenTree/static/script/inventree/bom.js b/InvenTree/static/script/inventree/bom.js index c3e7de0351..273fb1c8fa 100644 --- a/InvenTree/static/script/inventree/bom.js +++ b/InvenTree/static/script/inventree/bom.js @@ -22,9 +22,10 @@ function downloadBom(options = {}) { `; diff --git a/requirements/base.txt b/requirements/base.txt index be0a10fba7..b8e6f4a5e5 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -6,6 +6,7 @@ django_filter>=1.0.2 django-simple-history>=1.8.2 coreapi>=2.3.0 pygments>=2.2.0 +tablib>=0.13.0 django-crispy-forms>=1.7.2 django-import-export>=1.0.0 django-cleanup>=2.1.0