From 34b784d1e47b70627c374e3724e019d93d0f3f1b Mon Sep 17 00:00:00 2001 From: eeintech Date: Mon, 2 Nov 2020 13:14:31 -0500 Subject: [PATCH] Added setting, checkbox (PartCreateView only) and hook to create part parameters from category templates --- InvenTree/common/models.py | 7 +++++++ InvenTree/part/forms.py | 6 ++++++ InvenTree/part/views.py | 18 ++++++++++++++++++ .../templates/InvenTree/settings/part.html | 1 + 4 files changed, 32 insertions(+) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 0d3afbe226..9b52a89eba 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -85,6 +85,13 @@ class InvenTreeSetting(models.Model): 'validator': bool }, + 'PART_CATEGORY_PARAMETERS': { + 'name': _('Create Parameters From Category Templates'), + 'description': _('Automatically create part parameters from category templates'), + 'default': False, + 'validator': bool + }, + 'BUILDORDER_REFERENCE_PREFIX': { 'name': _('Build Order Reference Prefix'), 'description': _('Prefix value for build order reference'), diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index f553229b5c..dd41a5b583 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -186,6 +186,11 @@ class EditPartForm(HelperForm): help_text=_('Confirm part creation'), widget=forms.HiddenInput()) + category_templates = forms.BooleanField(required=False, + initial=False, + help_text=_('Create parameters from category templates'), + widget=forms.HiddenInput()) + class Meta: model = Part fields = [ @@ -193,6 +198,7 @@ class EditPartForm(HelperForm): 'parameters_copy', 'confirm_creation', 'category', + 'category_templates', 'name', 'IPN', 'description', diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index e913bda57d..e17f7da7f0 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -555,6 +555,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() + return form def post(self, request, *args, **kwargs): @@ -607,6 +610,18 @@ class PartCreate(AjaxCreateView): except AttributeError: pass + # Create part parameters + category_templates = form.cleaned_data['category_templates'] + if category_templates: + # Get category parent + 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) + return self.renderJsonResponse(request, form, data, context=context) def get_initial(self): @@ -630,6 +645,9 @@ class PartCreate(AjaxCreateView): if label in self.request.GET: 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)) + return initials diff --git a/InvenTree/templates/InvenTree/settings/part.html b/InvenTree/templates/InvenTree/settings/part.html index 19578ba858..495526c94a 100644 --- a/InvenTree/templates/InvenTree/settings/part.html +++ b/InvenTree/templates/InvenTree/settings/part.html @@ -18,6 +18,7 @@ {% include "InvenTree/settings/setting.html" with key="PART_COPY_BOM" %} {% include "InvenTree/settings/setting.html" with key="PART_COPY_PARAMETERS" %} {% include "InvenTree/settings/setting.html" with key="PART_COPY_TESTS" %} + {% include "InvenTree/settings/setting.html" with key="PART_CATEGORY_PARAMETERS" %}