diff --git a/InvenTree/InvenTree/static/script/inventree/part.js b/InvenTree/InvenTree/static/script/inventree/part.js index 71f78855f7..18909fff0d 100644 --- a/InvenTree/InvenTree/static/script/inventree/part.js +++ b/InvenTree/InvenTree/static/script/inventree/part.js @@ -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; + }); } \ No newline at end of file diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 2e9e748e99..3c061472c9 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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. diff --git a/InvenTree/part/templates/part/category.html b/InvenTree/part/templates/part/category.html index 4bbb606bec..1a4b1cb8a5 100644 --- a/InvenTree/part/templates/part/category.html +++ b/InvenTree/part/templates/part/category.html @@ -50,6 +50,7 @@ diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index e39375f032..3ec99e7255 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -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\d+)/', include(part_detail_urls)), diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index fae82bfc72..16de788064 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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.