diff --git a/InvenTree/InvenTree/forms.py b/InvenTree/InvenTree/forms.py index 3038a1cd7f..3a91821a19 100644 --- a/InvenTree/InvenTree/forms.py +++ b/InvenTree/InvenTree/forms.py @@ -219,10 +219,10 @@ class SettingCategorySelectForm(forms.ModelForm): Div( Div(Field('category'), css_class='col-sm-6', - style='width: 30%;'), + style='width: 70%;'), Div(StrictButton(_('Select Category'), css_class='btn btn-primary', type='submit'), css_class='col-sm-6', - style='width: auto; padding-left: 0;'), + style='width: 30%; padding-left: 0;'), css_class='row', ), ) diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 9b52a89eba..e5bc10c906 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -86,9 +86,9 @@ class InvenTreeSetting(models.Model): }, 'PART_CATEGORY_PARAMETERS': { - 'name': _('Create Parameters From Category Templates'), - 'description': _('Automatically create part parameters from category templates'), - 'default': False, + 'name': _('Copy Category Parameter Templates'), + 'description': _('Copy category parameter templates when creating a part'), + 'default': True, 'validator': bool }, diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 705e4c6afd..633915466f 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -12,6 +12,7 @@ from django.core.exceptions import ValidationError from django.urls import reverse from django.db import models, transaction +from django.db.utils import IntegrityError from django.db.models import Sum, UniqueConstraint from django.db.models.functions import Coalesce from django.core.validators import MinValueValidator @@ -324,6 +325,9 @@ class Part(MPTTModel): If not, it is considered "orphaned" and will be deleted. """ + # Get category templates settings + add_category_templates = kwargs.pop('add_category_templates', None) + if self.pk: previous = Part.objects.get(pk=self.pk) @@ -339,6 +343,44 @@ class Part(MPTTModel): super().save(*args, **kwargs) + if add_category_templates: + # Get part category + category = self.category + + if add_category_templates: + # Store templates added to part + template_list = [] + + # Create part parameters for selected category + category_templates = add_category_templates['main'] + if category_templates: + for template in category.get_parameter_templates(): + parameter = PartParameter.create(part=self, + 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 = add_category_templates['parent'] + if category_templates: + # Get parent categories + parent_categories = category.get_ancestors() + + for category in parent_categories: + for template in category.get_parameter_templates(): + # Check that template wasn't already added + if template.parameter_template not in template_list: + try: + PartParameter.create(part=self, + template=template.parameter_template, + data=template.default_value, + save=True) + except IntegrityError: + # PartParameter already exists + pass + def __str__(self): return f"{self.full_name} - {self.description}" diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 25d2eb8bfb..65b74ec7b2 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -684,7 +684,14 @@ class PartCreate(AjaxCreateView): # Record the user who created this part part.creation_user = request.user - part.save() + # Store category templates settings + add_category_templates = { + 'main': form.cleaned_data['selected_category_templates'], + 'parent': form.cleaned_data['parent_category_templates'], + } + + # Save part and pass category template settings + part.save(**{'add_category_templates': add_category_templates}) data['pk'] = part.pk data['text'] = str(part) @@ -694,42 +701,6 @@ class PartCreate(AjaxCreateView): except AttributeError: pass - # 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 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 categories - parent_categories = form.cleaned_data['category'].get_ancestors() - - for category in parent_categories: - for template in category.get_parameter_templates(): - # 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) def get_initial(self): diff --git a/InvenTree/templates/InvenTree/settings/category.html b/InvenTree/templates/InvenTree/settings/category.html index d17efd9c9a..c84573e8b4 100644 --- a/InvenTree/templates/InvenTree/settings/category.html +++ b/InvenTree/templates/InvenTree/settings/category.html @@ -37,7 +37,8 @@ {% block js_ready %} {{ block.super }} - + + {# Convert dropdown to select2 format #} $(document).ready(function() { attachSelect('#category-select'); }); @@ -78,7 +79,6 @@ ] }); - $("#new-param").click(function() { launchModalForm("{% url 'category-param-template-create' category.pk %}", { success: function() { @@ -110,6 +110,5 @@ } }); }); - {% endif %} {% endblock %}