diff --git a/InvenTree/part/admin.py b/InvenTree/part/admin.py index 11a5b2c28a..c377c27989 100644 --- a/InvenTree/part/admin.py +++ b/InvenTree/part/admin.py @@ -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): diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 1a3f8b6125..9678819e41 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -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', diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 19a0383b36..562fe27f2b 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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 diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 73555bfc0a..a1e38ea563 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -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):