Match each part to the list of available parts

- Order selections based on "best" match
This commit is contained in:
Oliver Walters 2019-07-07 11:44:17 +10:00
parent f251620917
commit a6da3ed4a4
3 changed files with 35 additions and 7 deletions

View File

@ -49,12 +49,11 @@
{% if item.column.guess == 'Part' %}
<select class='select bomselect' id='id_part_{{ row.index }}' name='part_select_{{ row.index }}'>
<option value=''>---------</option>
{% for part in allowed_parts_list %}
<option value='{{ part.id }}'>{{ part.full_name }} - {{ part.description }}</option>
{% for part in row.part_options %}
<option value='{{ part.id }}'>{{ part.full_name }} - <i>{{ part.description }}</i></option>
{% endfor %}
</select>
<br>
{{ item.cell }}
<i>{{ item.cell }}</i>
{% elif item.column.guess == 'Quantity' %}
<input class='numberinput' type='number' min='1' value='{{ row.quantity }}'/>
{% elif item.editable %}
@ -76,6 +75,9 @@
{% block js_ready %}
{{ block.super }}
$('.bomselect').select2();
$('.bomselect').select2({
dropdownAutoWidth: true,
matcher: partialMatcher,
});
{% endblock %}

View File

@ -14,6 +14,8 @@ from django.views.generic.edit import FormMixin
from django.forms.models import model_to_dict
from django.forms import HiddenInput, CheckboxInput
from fuzzywuzzy import fuzz
from .models import PartCategory, Part, PartAttachment
from .models import BomItem
from .models import match_part_names
@ -697,6 +699,7 @@ class BomUpload(FormView):
'index': row.get('index', -1),
'data': data,
'quantity': row.get('quantity', None),
'part_options': row.get('part_options', [])
})
ctx['part'] = self.part
@ -782,21 +785,40 @@ class BomUpload(FormView):
except ValueError:
q_idx = -1
try:
p_idx = list(self.column_selections.values()).index('Part')
except ValueError:
p_idx = -1
for row in self.bom_rows:
quantity = 0
part = None
if q_idx >= 0:
print(row)
q_val = row['data'][q_idx]
print("row:", row['index'], "q:", q_val)
try:
quantity = int(q_val)
except ValueError:
pass
if p_idx >= 0:
p_val = row['data'][p_idx]
# Fuzzy match the values and see what happends
matches = []
for part in self.allowed_parts:
ratio = fuzz.partial_ratio(part.name + part.description, p_val)
matches.append({'part': part, 'match': ratio})
if len(matches) > 0:
matches = sorted(matches, key=lambda item: item['match'], reverse=True)
row['part'] = part
row['quantity'] = quantity
row['part_options'] = [m['part'] for m in matches]
def extractDataFromFile(self, bom):

View File

@ -77,6 +77,10 @@
width: 100%;
}
.bomselect {
max-width: 250px;
}
/* Part image icons with full-display on mouse hover */
.hover-img-thumb {