Add a 'confirm_creation' input if there are possible part matches

This commit is contained in:
Oliver Walters 2019-05-11 12:29:02 +10:00
parent d9c0d2f5e3
commit 84d2fce8ca
4 changed files with 30 additions and 6 deletions

View File

@ -9,7 +9,7 @@ from .models import BomItem
class PartAdmin(ImportExportModelAdmin): class PartAdmin(ImportExportModelAdmin):
list_display = ('name', 'IPN', 'description', 'total_stock', 'category') list_display = ('long_name', 'IPN', 'description', 'total_stock', 'category')
class PartCategoryAdmin(ImportExportModelAdmin): class PartCategoryAdmin(ImportExportModelAdmin):

View File

@ -60,9 +60,12 @@ class EditPartAttachmentForm(HelperForm):
class EditPartForm(HelperForm): class EditPartForm(HelperForm):
""" Form for editing a Part object """ """ Form for editing a Part object """
confirm_creation = forms.BooleanField(required=False, initial=False, help_text='Confirm part creation', widget=forms.HiddenInput())
class Meta: class Meta:
model = Part model = Part
fields = [ fields = [
'confirm_creation',
'category', 'category',
'name', 'name',
'variant', 'variant',

View File

@ -146,7 +146,7 @@ def match_part_names(match, threshold=80, reverse=True, compare_length=False):
if len(compare) == 0: if len(compare) == 0:
continue continue
ratio = fuzz.partial_ratio(compare, match) ratio = fuzz.partial_token_sort_ratio(compare, match)
if compare_length: if compare_length:
# Also employ primitive length comparison # Also employ primitive length comparison

View File

@ -10,7 +10,7 @@ from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.forms.models import model_to_dict from django.forms.models import model_to_dict
from django.forms import HiddenInput from django.forms import HiddenInput, CheckboxInput
from company.models import Company from company.models import Company
from .models import PartCategory, Part, PartAttachment from .models import PartCategory, Part, PartAttachment
@ -179,9 +179,12 @@ class PartCreate(AjaxCreateView):
form = self.get_form() form = self.get_form()
name = request.POST.get('name', None)
context = {} context = {}
valid = form.is_valid()
name = request.POST.get('name', None)
if name: if name:
matches = match_part_names(name) matches = match_part_names(name)
@ -189,12 +192,30 @@ class PartCreate(AjaxCreateView):
if len(matches) > 0: if len(matches) > 0:
context['matches'] = matches context['matches'] = matches
form.non_field_errors = 'Check matches' # Check if the user has checked the 'confirm_creation' input
confirmed = request.POST.get('confirm_creation', False)
if not confirmed:
form.fields['confirm_creation'].widget = CheckboxInput()
form.errors['confirm_creation'] = ['Possible matches exist - confirm creation of new part']
form.non_field_errors = 'Possible matches exist - confirm creation of new part'
valid = False
data = { data = {
'form_valid': False 'form_valid': valid
} }
if valid:
# Create the new Part
part = form.save()
data['pk'] = part.pk
try:
data['url'] = part.get_absolute_url()
except AttributeError:
pass
return self.renderJsonResponse(request, form, data, context=context) return self.renderJsonResponse(request, form, data, context=context)
def get_initial(self): def get_initial(self):