2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-03 13:58:47 +00:00

Create a new part directly from the BOM view

- Pass data through to the part creation
- Populate the new part into the select dropdown
-
This commit is contained in:
Oliver Walters 2019-07-07 13:06:59 +10:00
parent ff5af8f217
commit 85e803f345
4 changed files with 65 additions and 18 deletions

View File

@ -54,13 +54,14 @@ class BomUploadManager:
# Fields which would be helpful but are not required # Fields which would be helpful but are not required
OPTIONAL_HEADERS = [ OPTIONAL_HEADERS = [
'Reference', 'Reference',
'Notes',
'Overage',
'Description', 'Description',
'Category', 'Category',
'Supplier', 'Supplier',
'Manufacturer', 'Manufacturer',
'MPN', 'MPN',
'Overage', 'IPN',
'Notes'
] ]
EDITABLE_HEADERS = [ EDITABLE_HEADERS = [

View File

@ -35,7 +35,7 @@
</thead> </thead>
<tbody> <tbody>
{% for row in bom_rows %} {% for row in bom_rows %}
<tr> <tr part-description='{{ row.description }}' part-select='#select_part_{{ row.index }}'>
<td> <td>
<button class='btn btn-default btn-remove' id='del_row_{{ forloop.counter }}' style='display: inline; float: right;' title='Remove row'> <button class='btn btn-default btn-remove' id='del_row_{{ forloop.counter }}' style='display: inline; float: right;' title='Remove row'>
<span row_id='{{ forloop.counter }}' onClick='removeRowFromBomWizard()' class='glyphicon glyphicon-small glyphicon-remove'></span> <span row_id='{{ forloop.counter }}' onClick='removeRowFromBomWizard()' class='glyphicon glyphicon-small glyphicon-remove'></span>
@ -51,7 +51,7 @@
<span row_id='{{ row.index }}' class='glyphicon glyphicon-small glyphicon-plus' onClick='newPartFromBomWizard()'/> <span row_id='{{ row.index }}' class='glyphicon glyphicon-small glyphicon-plus' onClick='newPartFromBomWizard()'/>
</button> </button>
<select class='select bomselect' id='id_part_{{ row.index }}' name='part_select_{{ row.index }}'> <select class='select bomselect' id='select_part_{{ row.index }}' name='part_select_{{ row.index }}'>
<option value=''>---------</option> <option value=''>---------</option>
{% for part in row.part_options %} {% for part in row.part_options %}
<option value='{{ part.id }}'>{{ part.full_name }} - <i>{{ part.description }}</i></option> <option value='{{ part.id }}'>{{ part.full_name }} - <i>{{ part.description }}</i></option>
@ -60,8 +60,12 @@
<i>{{ item.cell }}</i> <i>{{ item.cell }}</i>
{% elif item.column.guess == 'Quantity' %} {% elif item.column.guess == 'Quantity' %}
<input class='numberinput' type='number' min='1' value='{{ row.quantity }}'/> <input class='numberinput' type='number' min='1' value='{{ row.quantity }}'/>
{% elif item.editable %} {% elif item.column.guess == 'Reference' %}
<input id='id_edit_{{ item.column.guess }}_{{ row.index }}' name='edit_{{ item.column.guess }}_{{ row.index }}' value='{{ item.cell }}'/> <input name='reference_{{ row.index }}' value='{{ row.reference }}'/>
{% elif item.column.guess == 'Notes' %}
<input name='notes_{{ row.index }}' value='{{ row.notes }}'/>
{% elif item.column.guess == 'Overage' %}
<input name='overage_{{ row.index }}' value='{{ row.overage }}'/>
{% else %} {% else %}
{{ item.cell }} {{ item.cell }}
{% endif %} {% endif %}

View File

@ -494,6 +494,11 @@ class PartCreate(AjaxCreateView):
initials['keywords'] = category.default_keywords initials['keywords'] = category.default_keywords
except PartCategory.DoesNotExist: except PartCategory.DoesNotExist:
pass pass
# Allow initial data to be passed through as arguments
for label in ['name', 'IPN', 'description', 'revision', 'keywords']:
if label in self.request.GET:
initials[label] = self.request.GET.get(label)
return initials return initials
@ -698,8 +703,14 @@ class BomUpload(FormView):
rows.append({ rows.append({
'index': row.get('index', -1), 'index': row.get('index', -1),
'data': data, 'data': data,
'part_options': row.get('part_options', []),
# User-input (passed between client and server)
'quantity': row.get('quantity', None), 'quantity': row.get('quantity', None),
'part_options': row.get('part_options', []) 'description': row.get('description', ''),
'part': row.get('part', None),
'reference': row.get('reference', ''),
'notes': row.get('notes', ''),
}) })
ctx['part'] = self.part ctx['part'] = self.part
@ -774,21 +785,29 @@ class BomUpload(FormView):
return self.render_to_response(self.get_context_data(form=form)) return self.render_to_response(self.get_context_data(form=form))
def getColumnIndex(self, name):
""" Return the index of the column with the given name.
It named column is not found, return -1
"""
try:
idx = list(self.column_selections.values()).index(name)
except ValueError:
idx = -1
return idx
def preFillSelections(self): def preFillSelections(self):
""" Once data columns have been selected, """ Once data columns have been selected,
attempt to pre-select the proper data from the database. attempt to pre-select the proper data from the database.
""" """
try: q_idx = self.getColumnIndex('Quantity')
# Index of quantity field p_idx = self.getColumnIndex('Part')
q_idx = list(self.column_selections.values()).index('Quantity') d_idx = self.getColumnIndex('Description')
except ValueError: r_idx = self.getColumnIndex('Reference')
q_idx = -1 n_idx = self.getColumnIndex('Notes')
try:
p_idx = list(self.column_selections.values()).index('Part')
except ValueError:
p_idx = -1
for row in self.bom_rows: for row in self.bom_rows:
@ -816,11 +835,21 @@ class BomUpload(FormView):
if len(matches) > 0: if len(matches) > 0:
matches = sorted(matches, key=lambda item: item['match'], reverse=True) matches = sorted(matches, key=lambda item: item['match'], reverse=True)
if d_idx >= 0:
row['description'] = row['data'][d_idx]
if r_idx >= 0:
row['reference'] = row['data'][r_idx]
if n_idx >= 0:
row['notes'] = row['data'][n_idx]
row['part'] = part row['part'] = part
row['quantity'] = quantity row['quantity'] = quantity
row['part_options'] = [m['part'] for m in matches] row['part_options'] = [m['part'] for m in matches]
def extractDataFromFile(self, bom): def extractDataFromFile(self, bom):
""" Read data from the BOM file """ """ Read data from the BOM file """

View File

@ -70,8 +70,21 @@ function newPartFromBomWizard(e) {
var src = e.target || e.srcElement; var src = e.target || e.srcElement;
var row = $(src).closest('tr');
launchModalForm('/part/new/', { launchModalForm('/part/new/', {
success: function() { data: {
'description': row.attr('part-description'),
'name': row.attr('part-name'),
},
success: function(response) {
/* A new part has been created! Push it as an option.
*/
var select = row.attr('part-select');
var option = new Option(response.text, response.pk, true, true);
$(select).append(option).trigger('change');
} }
}); });
} }