Merge pull request #132 from SchrodingersGat/export-improvements

Add extra BOM export format options
This commit is contained in:
Oliver 2019-04-18 00:17:28 +10:00 committed by GitHub
commit 9cb61b5b1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 74 deletions

View File

@ -5,6 +5,8 @@ import os
import math import math
import tablib
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -285,14 +287,12 @@ class Part(models.Model):
def export_bom(self, **kwargs): def export_bom(self, **kwargs):
# Construct the export data data = tablib.Dataset(headers=[
header = [] 'Part',
header.append('Part') 'Description',
header.append('Description') 'Quantity',
header.append('Quantity') 'Note',
header.append('Note') ])
rows = []
for it in self.bom_items.all(): for it in self.bom_items.all():
line = [] line = []
@ -302,73 +302,11 @@ class Part(models.Model):
line.append(it.quantity) line.append(it.quantity)
line.append(it.note) line.append(it.note)
rows.append([str(x) for x in line]) data.append(line)
file_format = kwargs.get('format', 'csv').lower() file_format = kwargs.get('format', 'csv').lower()
kwargs['header'] = header return data.export(file_format)
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
"""
def attach_file(instance, filename): def attach_file(instance, filename):

View File

@ -22,9 +22,10 @@ function downloadBom(options = {}) {
<select id='bom-format' class='select'> <select id='bom-format' class='select'>
<option value='csv'>CSV</option> <option value='csv'>CSV</option>
<option value='xls'>XLSX</option> <option value='xls'>XLSX</option>
<option value='pdf'>PDF</option> <option value='yaml'>YAML</option>
<option value='json'>JSON</option>
<option value='xml'>XML</option> <option value='xml'>XML</option>
<option value='htm'>HTML</option> <option value='html'>HTML</option>
</select> </select>
</div> </div>
`; `;

View File

@ -6,6 +6,7 @@ django_filter>=1.0.2
django-simple-history>=1.8.2 django-simple-history>=1.8.2
coreapi>=2.3.0 coreapi>=2.3.0
pygments>=2.2.0 pygments>=2.2.0
tablib>=0.13.0
django-crispy-forms>=1.7.2 django-crispy-forms>=1.7.2
django-import-export>=1.0.0 django-import-export>=1.0.0
django-cleanup>=2.1.0 django-cleanup>=2.1.0