Export selected parts

- Select which parts to export in the table
- Export base information
- Include supplier part information
This commit is contained in:
Oliver Walters 2019-08-07 09:52:49 +10:00
parent 55fdcc8cf7
commit dda4569f76
5 changed files with 103 additions and 1 deletions

View File

@ -241,4 +241,17 @@ function loadPartTable(table, url, options={}) {
reload: true,
});
});
$('#multi-part-export').click(function() {
var selections = $(table).bootstrapTable("getSelections");
var parts = '';
selections.forEach(function(item) {
parts += item.pk;
parts += ',';
});
location.href = '/part/export/?parts=' + parts;
});
}

View File

@ -858,7 +858,7 @@ class Part(models.Model):
self.save()
def export_bom(self, **kwargs):
def export_bom(self, **kwargs):
""" Export Bill of Materials to a spreadsheet file.
Includes a row for each item in the BOM.
Also includes extra information such as supplier data.

View File

@ -50,6 +50,7 @@
<ul class='dropdown-menu'>
<li><a href='#' id='multi-part-order' title='Order parts'>Order Parts</a></li>
<li><a href='#' id='multi-part-category' title='Set Part Category'>Set Category</a></li>
<li><a href='#' id='multi-part-export' title='Export'>Export Data</a></li>
</ul>
</div>
</div>

View File

@ -78,6 +78,9 @@ part_urls = [
# Download a BOM upload template
url(r'^bom_template/?', views.BomUploadTemplate.as_view(), name='bom-upload-template'),
# Export data for multiple parts
url(r'^export/', views.PartExport.as_view(), name='part-export'),
# Individual part
url(r'^(?P<pk>\d+)/', include(part_detail_urls)),

View File

@ -14,6 +14,8 @@ from django.views.generic import DetailView, ListView, FormView
from django.forms.models import model_to_dict
from django.forms import HiddenInput, CheckboxInput
import tablib
from fuzzywuzzy import fuzz
from .models import PartCategory, Part, PartAttachment
@ -1159,6 +1161,89 @@ class BomUpload(FormView):
return self.render_to_response(self.get_context_data(form=self.form))
class PartExport(AjaxView):
""" Export a CSV file containing information on multiple parts """
def get(self, request, *args, **kwargs):
part = request.GET.get('parts', '')
parts = []
for pk in part.split(','):
try:
parts.append(Part.objects.get(pk=int(pk)))
except (Part.DoesNotExist, ValueError):
continue
headers = [
'Name',
'Description',
'Category',
'Category ID',
'IPN',
'Revision',
'URL',
'Notes',
'Assembly',
'Component',
'Trackable',
'Salable',
'Active',
'Virtual'
]
# Construct list of suppliers for each part
supplier_names = set()
for part in parts:
supplier_parts = part.supplier_parts.all()
part.suppliers = {}
for sp in supplier_parts:
name = sp.supplier.name
supplier_names.add(name)
part.suppliers[name] = sp
if len(supplier_names) > 0:
headers.append('')
for name in supplier_names:
headers.append(name)
data = tablib.Dataset(headers=headers)
for part in parts:
line = []
line.append(part.name)
line.append(part.description)
line.append(str(part.category))
line.append(part.category.pk)
line.append(part.IPN)
line.append(part.revision)
line.append(part.URL)
line.append(part.notes)
line.append(part.assembly)
line.append(part.component)
line.append(part.trackable)
line.append(part.salable)
line.append(part.active)
line.append(part.virtual)
if len(supplier_names) > 0:
line.append('')
for name in supplier_names:
sp = part.suppliers.get(name, None)
if sp:
line.append(sp.SKU)
else:
line.append('')
data.append(line)
csv = data.export('csv')
return DownloadFile(csv, 'InvenTree_Parts.csv')
class BomUploadTemplate(AjaxView):
"""
Provide a BOM upload template file for download.