mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Added checkbox to add parameter template to all categories
This commit is contained in:
parent
3a347fba21
commit
978b5f869d
@ -201,7 +201,8 @@ class ColorThemeSelectForm(forms.ModelForm):
|
|||||||
class SettingCategorySelectForm(forms.ModelForm):
|
class SettingCategorySelectForm(forms.ModelForm):
|
||||||
""" Form for setting category settings """
|
""" Form for setting category settings """
|
||||||
|
|
||||||
name = forms.ChoiceField(choices=(), required=False)
|
name = forms.ChoiceField(choices=[('', '-' * 10)] + PartCategory.get_parent_categories(),
|
||||||
|
required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PartCategory
|
model = PartCategory
|
||||||
@ -212,9 +213,6 @@ class SettingCategorySelectForm(forms.ModelForm):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(SettingCategorySelectForm, self).__init__(*args, **kwargs)
|
super(SettingCategorySelectForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
# Populate category choices
|
|
||||||
self.fields['name'].choices = PartCategory.get_parent_categories()
|
|
||||||
|
|
||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
# Form rendering
|
# Form rendering
|
||||||
self.helper.form_show_labels = False
|
self.helper.form_show_labels = False
|
||||||
@ -222,10 +220,10 @@ class SettingCategorySelectForm(forms.ModelForm):
|
|||||||
Div(
|
Div(
|
||||||
Div(Field('name'),
|
Div(Field('name'),
|
||||||
css_class='col-sm-6',
|
css_class='col-sm-6',
|
||||||
style='width: 200px;'),
|
style='width: auto;'),
|
||||||
Div(StrictButton(_('Select Category'), css_class='btn btn-primary', type='submit'),
|
Div(StrictButton(_('Select Category'), css_class='btn btn-primary', type='submit'),
|
||||||
css_class='col-sm-6',
|
css_class='col-sm-6',
|
||||||
style='width: auto;'),
|
style='width: auto; padding-left: 0;'),
|
||||||
css_class='row',
|
css_class='row',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -251,12 +251,17 @@ class EditCategoryForm(HelperForm):
|
|||||||
class EditCategoryParameterTemplateForm(HelperForm):
|
class EditCategoryParameterTemplateForm(HelperForm):
|
||||||
""" Form for editing a PartCategoryParameterTemplate object """
|
""" Form for editing a PartCategoryParameterTemplate object """
|
||||||
|
|
||||||
|
add_to_all_categories = forms.BooleanField(required=False,
|
||||||
|
initial=False,
|
||||||
|
help_text=_('Add parameter template to all categories'))
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PartCategoryParameterTemplate
|
model = PartCategoryParameterTemplate
|
||||||
fields = [
|
fields = [
|
||||||
'category',
|
'category',
|
||||||
'parameter_template',
|
'parameter_template',
|
||||||
'default_value',
|
'default_value',
|
||||||
|
'add_to_all_categories',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -167,13 +167,10 @@ class PartCategory(InvenTreeTree):
|
|||||||
def get_parent_categories(cls):
|
def get_parent_categories(cls):
|
||||||
""" Return tuple list of parent (root) categories """
|
""" Return tuple list of parent (root) categories """
|
||||||
|
|
||||||
# Store parent categories (add empty label)
|
|
||||||
parent_categories = [
|
|
||||||
('', '-' * 10)
|
|
||||||
]
|
|
||||||
# Get root nodes
|
# Get root nodes
|
||||||
root_categories = cls.objects.filter(level=0)
|
root_categories = cls.objects.filter(level=0)
|
||||||
|
|
||||||
|
parent_categories = []
|
||||||
for category in root_categories:
|
for category in root_categories:
|
||||||
parent_categories.append((category.id, category.name))
|
parent_categories.append((category.id, category.name))
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
from django.db.utils import IntegrityError
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.shortcuts import HttpResponseRedirect
|
from django.shortcuts import HttpResponseRedirect
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
@ -2178,7 +2179,7 @@ class CategoryParameterTemplateCreate(AjaxCreateView):
|
|||||||
# Get category
|
# Get category
|
||||||
category = self.get_initial()['category']
|
category = self.get_initial()['category']
|
||||||
|
|
||||||
# Get existing related parts
|
# Get existing parameter templates
|
||||||
parameters = [template.parameter_template.pk
|
parameters = [template.parameter_template.pk
|
||||||
for template in category.get_parameter_templates()]
|
for template in category.get_parameter_templates()]
|
||||||
|
|
||||||
@ -2188,13 +2189,51 @@ class CategoryParameterTemplateCreate(AjaxCreateView):
|
|||||||
if (choice[0] not in parameters):
|
if (choice[0] not in parameters):
|
||||||
updated_choices.append(choice)
|
updated_choices.append(choice)
|
||||||
|
|
||||||
# Update choices for related part
|
# Update choices for parameter templates
|
||||||
form.fields['parameter_template'].choices = updated_choices
|
form.fields['parameter_template'].choices = updated_choices
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return form
|
return form
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
""" Capture the POST request
|
||||||
|
|
||||||
|
- If the add_to_all_categories object is set, link parameter template to
|
||||||
|
all categories
|
||||||
|
"""
|
||||||
|
|
||||||
|
form = self.get_form()
|
||||||
|
|
||||||
|
valid = form.is_valid()
|
||||||
|
|
||||||
|
if valid:
|
||||||
|
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']
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
# Skip selected category (will be processed in the post call)
|
||||||
|
if category_id != selected_category:
|
||||||
|
# Get category
|
||||||
|
category = PartCategory.objects.get(pk=category_id)
|
||||||
|
try:
|
||||||
|
cat_template = PartCategoryParameterTemplate.objects.create(category=category,
|
||||||
|
parameter_template=parameter_template,
|
||||||
|
default_value=default_value)
|
||||||
|
cat_template.save()
|
||||||
|
except IntegrityError:
|
||||||
|
# Parameter template is already linked to category
|
||||||
|
pass
|
||||||
|
|
||||||
|
return super().post(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class CategoryParameterTemplateEdit(AjaxUpdateView):
|
class CategoryParameterTemplateEdit(AjaxUpdateView):
|
||||||
""" View for editing a PartCategoryParameterTemplate """
|
""" View for editing a PartCategoryParameterTemplate """
|
||||||
@ -2230,7 +2269,7 @@ class CategoryParameterTemplateEdit(AjaxUpdateView):
|
|||||||
# Get category
|
# Get category
|
||||||
category = PartCategory.objects.get(pk=self.kwargs.get('pk', None))
|
category = PartCategory.objects.get(pk=self.kwargs.get('pk', None))
|
||||||
|
|
||||||
# Get existing related parts
|
# Get existing parameter templates
|
||||||
parameters = [template.parameter_template.pk
|
parameters = [template.parameter_template.pk
|
||||||
for template in category.get_parameter_templates()]
|
for template in category.get_parameter_templates()]
|
||||||
|
|
||||||
@ -2240,7 +2279,7 @@ class CategoryParameterTemplateEdit(AjaxUpdateView):
|
|||||||
if (choice[0] not in parameters):
|
if (choice[0] not in parameters):
|
||||||
updated_choices.append(choice)
|
updated_choices.append(choice)
|
||||||
|
|
||||||
# Update choices for related part
|
# Update choices for parameter templates
|
||||||
form.fields['parameter_template'].choices = updated_choices
|
form.fields['parameter_template'].choices = updated_choices
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user