mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add a 'confirm_creation' input if there are possible part matches
This commit is contained in:
parent
d9c0d2f5e3
commit
84d2fce8ca
@ -9,7 +9,7 @@ from .models import BomItem
|
||||
|
||||
class PartAdmin(ImportExportModelAdmin):
|
||||
|
||||
list_display = ('name', 'IPN', 'description', 'total_stock', 'category')
|
||||
list_display = ('long_name', 'IPN', 'description', 'total_stock', 'category')
|
||||
|
||||
|
||||
class PartCategoryAdmin(ImportExportModelAdmin):
|
||||
|
@ -60,9 +60,12 @@ class EditPartAttachmentForm(HelperForm):
|
||||
class EditPartForm(HelperForm):
|
||||
""" Form for editing a Part object """
|
||||
|
||||
confirm_creation = forms.BooleanField(required=False, initial=False, help_text='Confirm part creation', widget=forms.HiddenInput())
|
||||
|
||||
class Meta:
|
||||
model = Part
|
||||
fields = [
|
||||
'confirm_creation',
|
||||
'category',
|
||||
'name',
|
||||
'variant',
|
||||
|
@ -146,7 +146,7 @@ def match_part_names(match, threshold=80, reverse=True, compare_length=False):
|
||||
if len(compare) == 0:
|
||||
continue
|
||||
|
||||
ratio = fuzz.partial_ratio(compare, match)
|
||||
ratio = fuzz.partial_token_sort_ratio(compare, match)
|
||||
|
||||
if compare_length:
|
||||
# Also employ primitive length comparison
|
||||
|
@ -10,7 +10,7 @@ from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import DetailView, ListView
|
||||
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 .models import PartCategory, Part, PartAttachment
|
||||
@ -179,9 +179,12 @@ class PartCreate(AjaxCreateView):
|
||||
|
||||
form = self.get_form()
|
||||
|
||||
name = request.POST.get('name', None)
|
||||
|
||||
context = {}
|
||||
|
||||
valid = form.is_valid()
|
||||
|
||||
name = request.POST.get('name', None)
|
||||
|
||||
if name:
|
||||
matches = match_part_names(name)
|
||||
@ -189,12 +192,30 @@ class PartCreate(AjaxCreateView):
|
||||
if len(matches) > 0:
|
||||
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 = {
|
||||
'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)
|
||||
|
||||
def get_initial(self):
|
||||
|
Loading…
Reference in New Issue
Block a user