BOM upload: improve generation of "import" template

- Better field naming
- Remove some fields which are not required here
- Better description of what is going to happen
This commit is contained in:
Oliver Walters 2020-08-18 11:47:27 +10:00
parent 89c7f72caf
commit 476cc5f661
3 changed files with 73 additions and 7 deletions

View File

@ -139,16 +139,71 @@ class BomItemResource(ModelResource):
bom_id = Field(attribute='pk')
# ID of the parent part
parent_part_id = Field(attribute='part', widget=widgets.ForeignKeyWidget(Part))
parent_part_name = Field(attribute='part__full_name', readonly=True)
# IPN of the parent part
parent_part_ipn = Field(attribute='part__IPN', readonly=True)
sub_part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part))
# Name of the parent part
parent_part_name = Field(attribute='part__name', readonly=True)
sub_part_name = Field(attribute='sub_part__full_name', readonly=True)
# ID of the sub-part
part_id = Field(attribute='sub_part', widget=widgets.ForeignKeyWidget(Part))
# IPN of the sub-part
part_ipn = Field(attribute='sub_part__IPN', readonly=True)
# Name of the sub-part
part_name = Field(attribute='sub_part__name', readonly=True)
# Description of the sub-part
part_description = Field(attribute='sub_part__description', readonly=True)
# Is the sub-part itself an assembly?
sub_assembly = Field(attribute='sub_part__assembly', readonly=True)
def before_export(self, queryset, *args, **kwargs):
self.is_importing = kwargs.get('importing', False)
def get_fields(self, **kwargs):
"""
If we are exporting for the purposes of generating
a 'bom-import' template, there are some fields which
we are not interested in.
"""
fields = super().get_fields(**kwargs)
# If we are not generating an "import" template,
# just return the complete list of fields
if not self.is_importing:
return fields
# Otherwise, remove some fields we are not interested in
idx = 0
to_remove = [
'level',
'bom_id',
'parent_part_id',
'parent_part_ipn',
'parent_part_name',
'part_description',
'sub_assembly'
]
while idx < len(fields):
if fields[idx].column_name.lower() in to_remove:
del fields[idx]
else:
idx += 1
return fields
class Meta:
model = BomItem
skip_unchanged = True

View File

@ -30,8 +30,14 @@ def MakeBomTemplate(fmt):
if not IsValidBOMFormat(fmt):
fmt = 'csv'
# Create an "empty" queryset, essentially.
# This will then export just the row headers!
query = BomItem.objects.filter(pk=None)
dataset = BomItemResource().export(queryset=query)
dataset = BomItemResource().export(
queryset=query,
importing=True
)
data = dataset.export(fmt)

View File

@ -1,18 +1,23 @@
{% extends "part/part_base.html" %}
{% load static %}
{% load i18n %}
{% load inventree_extras %}
{% block details %}
{% include "part/tabs.html" with tab='bom' %}
<h4>Upload Bill of Materials</h4>
<h4>{% trans "Upload Bill of Materials" %}</h4>
<hr>
<p>Step 1 - Select BOM File</p>
<p>{% trans "Step 1 - Select BOM File" %}</p>
<div class='alert alert-info alert-block'>
<p>The BOM file must contain the required named columns as provided in the <a href="/part/bom_template/">BOM Upload Template</a></a></p>
<b>{% trans "Requirements for BOM upload" %}:</b>
<ul>
<li>{% trans "The BOM file must contain the required named columns as provided in the " %} <b><a href="/part/bom_template/">{% trans "BOM Upload Template" %}</a></b></li>
<li>{% trans "Each part must already exist in the database" %}</li>
</ul>
</div>
<form method="post" action='' class='js-modal-form' enctype="multipart/form-data">