From 324645b67c58c81d8288a3fefbb9efa7eb0b6150 Mon Sep 17 00:00:00 2001 From: eeintech Date: Wed, 4 Nov 2020 12:26:10 -0500 Subject: [PATCH] Added same level category checkbox and method when adding category template --- InvenTree/part/forms.py | 7 ++++++- InvenTree/part/views.py | 28 ++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index 20507bd392..63d884a15e 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -282,16 +282,21 @@ class EditCategoryForm(HelperForm): class EditCategoryParameterTemplateForm(HelperForm): """ Form for editing a PartCategoryParameterTemplate object """ + add_to_same_level_categories = forms.BooleanField(required=False, + initial=False, + help_text=_('Add parameter template to same level categories')) + add_to_all_categories = forms.BooleanField(required=False, initial=False, help_text=_('Add parameter template to all categories')) - + class Meta: model = PartCategoryParameterTemplate fields = [ 'category', 'parameter_template', 'default_value', + 'add_to_same_level_categories', 'add_to_all_categories', ] diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 65b74ec7b2..b522a66159 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -2305,6 +2305,8 @@ class CategoryParameterTemplateCreate(AjaxCreateView): - If the add_to_all_categories object is set, link parameter template to all categories + - If the add_to_same_level_categories object is set, link parameter template to + same level categories """ form = self.get_form() @@ -2312,17 +2314,26 @@ class CategoryParameterTemplateCreate(AjaxCreateView): valid = form.is_valid() if valid: - all_categories = form.cleaned_data['add_to_all_categories'] + add_to_same_level_categories = form.cleaned_data['add_to_same_level_categories'] + add_to_all_categories = form.cleaned_data['add_to_all_categories'] - if all_categories: - selected_category = int(self.kwargs.get('pk', 0)) - parameter_template = form.cleaned_data['parameter_template'] - default_value = form.cleaned_data['default_value'] + selected_category = PartCategory.objects.get(pk=int(self.kwargs['pk'])) + parameter_template = form.cleaned_data['parameter_template'] + default_value = form.cleaned_data['default_value'] - # Add parameter template and default value to all categories - for category in PartCategory.objects.all(): + categories = PartCategory.objects.all() + + if add_to_same_level_categories and not add_to_all_categories: + # Get level + level = selected_category.level + # Filter same level categories + categories = categories.filter(level=level) + + if add_to_same_level_categories or add_to_all_categories: + # Add parameter template and default value to categories + for category in categories: # Skip selected category (will be processed in the post call) - if category.pk != selected_category: + if category.pk != selected_category.pk: try: cat_template = PartCategoryParameterTemplate.objects.create(category=category, parameter_template=parameter_template, @@ -2362,6 +2373,7 @@ class CategoryParameterTemplateEdit(AjaxUpdateView): form.fields['category'].widget = HiddenInput() form.fields['add_to_all_categories'].widget = HiddenInput() + form.fields['add_to_same_level_categories'].widget = HiddenInput() if form.is_valid(): form.cleaned_data['category'] = self.kwargs.get('pk', None)