diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 4e6413b34f..47b7ac21bc 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -205,11 +205,15 @@ class EditPartForm(HelperForm): help_text=_('Confirm part creation'), widget=forms.HiddenInput()) - category_templates = forms.BooleanField(required=False, - initial=False, - help_text=_('Create parameters based on default category templates'), - label=_('Copy category parameter templates'), - widget=forms.HiddenInput()) + selected_category_templates = forms.BooleanField(required=False, + initial=False, + label=_('Include selected category parameter templates'), + widget=forms.HiddenInput()) + + parent_category_templates = forms.BooleanField(required=False, + initial=False, + label=_('Include parent category parameter templates'), + widget=forms.HiddenInput()) class Meta: model = Part @@ -218,7 +222,8 @@ class EditPartForm(HelperForm): 'parameters_copy', 'confirm_creation', 'category', - 'category_templates', + 'selected_category_templates', + 'parent_category_templates', 'name', 'IPN', 'description', diff --git a/InvenTree/part/test_param.py b/InvenTree/part/test_param.py index 2bd3be2478..ad0f5ed3b6 100644 --- a/InvenTree/part/test_param.py +++ b/InvenTree/part/test_param.py @@ -49,10 +49,9 @@ class TestParams(TestCase): n = PartCategoryParameterTemplate.objects.all().count() self.assertEqual(n, 2) - parent_category = PartCategory.objects.get(pk=8).get_root() - self.assertEqual(parent_category.pk, 7) + category = PartCategory.objects.get(pk=7) - c1 = PartCategoryParameterTemplate(category=parent_category, + c1 = PartCategoryParameterTemplate(category=category, parameter_template=t1, default_value='xyz') c1.save() diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 099dd3c515..857cf94106 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -638,8 +638,9 @@ class PartCreate(AjaxCreateView): # Hide the default_supplier field (there are no matching supplier parts yet!) form.fields['default_supplier'].widget = HiddenInput() - # Force display of the 'category_templates' widget - form.fields['category_templates'].widget = CheckboxInput() + # Display category templates widgets + form.fields['selected_category_templates'].widget = CheckboxInput() + form.fields['parent_category_templates'].widget = CheckboxInput() return form @@ -693,17 +694,40 @@ class PartCreate(AjaxCreateView): except AttributeError: pass - # Create part parameters - category_templates = form.cleaned_data['category_templates'] + # Store templates added to part + template_list = [] + + # Create part parameters for selected category + category_templates = form.cleaned_data['selected_category_templates'] if category_templates: - # Get category parent + # Get selected category + category = form.cleaned_data['category'] + + for template in category.get_parameter_templates(): + parameter = PartParameter.create(part=part, + template=template.parameter_template, + data=template.default_value, + save=True) + if parameter: + template_list.append(template.parameter_template) + + # Create part parameters for parent category + category_templates = form.cleaned_data['parent_category_templates'] + if category_templates: + # Get parent category category = form.cleaned_data['category'].get_root() for template in category.get_parameter_templates(): - PartParameter.create(part=part, - template=template.parameter_template, - data=template.default_value, - save=True) + # Check that template wasn't already added + if template.parameter_template not in template_list: + try: + PartParameter.create(part=part, + template=template.parameter_template, + data=template.default_value, + save=True) + except IntegrityError: + # PartParameter already exists + pass return self.renderJsonResponse(request, form, data, context=context) @@ -729,7 +753,8 @@ class PartCreate(AjaxCreateView): initials[label] = self.request.GET.get(label) # Automatically create part parameters from category templates - initials['category_templates'] = str2bool(InvenTreeSetting.get_setting('PART_CATEGORY_PARAMETERS', False)) + initials['selected_category_templates'] = str2bool(InvenTreeSetting.get_setting('PART_CATEGORY_PARAMETERS', False)) + initials['parent_category_templates'] = initials['selected_category_templates'] return initials @@ -2323,13 +2348,9 @@ class CategoryParameterTemplateCreate(AjaxCreateView): default_value = form.cleaned_data['default_value'] # Add parameter template and default value to all categories - for category_id, category_name in PartCategory.get_parent_categories(): - # Change category_id type to integer - category_id = int(category_id) + for category in PartCategory.objects.all(): # Skip selected category (will be processed in the post call) - if category_id != selected_category: - # Get category - category = PartCategory.objects.get(pk=category_id) + if category.pk != selected_category: try: cat_template = PartCategoryParameterTemplate.objects.create(category=category, parameter_template=parameter_template, @@ -2368,6 +2389,7 @@ class CategoryParameterTemplateEdit(AjaxUpdateView): form = super(AjaxUpdateView, self).get_form() form.fields['category'].widget = HiddenInput() + form.fields['add_to_all_categories'].widget = HiddenInput() if form.is_valid(): form.cleaned_data['category'] = self.kwargs.get('pk', None)