Finalized implementation when creating new part

This commit is contained in:
eeintech 2020-11-03 16:54:46 -05:00
parent 72b5a105f8
commit 5a5a36083e
3 changed files with 51 additions and 25 deletions

View File

@ -205,11 +205,15 @@ class EditPartForm(HelperForm):
help_text=_('Confirm part creation'), help_text=_('Confirm part creation'),
widget=forms.HiddenInput()) widget=forms.HiddenInput())
category_templates = forms.BooleanField(required=False, selected_category_templates = forms.BooleanField(required=False,
initial=False, initial=False,
help_text=_('Create parameters based on default category templates'), label=_('Include selected category parameter templates'),
label=_('Copy category parameter templates'), widget=forms.HiddenInput())
widget=forms.HiddenInput())
parent_category_templates = forms.BooleanField(required=False,
initial=False,
label=_('Include parent category parameter templates'),
widget=forms.HiddenInput())
class Meta: class Meta:
model = Part model = Part
@ -218,7 +222,8 @@ class EditPartForm(HelperForm):
'parameters_copy', 'parameters_copy',
'confirm_creation', 'confirm_creation',
'category', 'category',
'category_templates', 'selected_category_templates',
'parent_category_templates',
'name', 'name',
'IPN', 'IPN',
'description', 'description',

View File

@ -49,10 +49,9 @@ class TestParams(TestCase):
n = PartCategoryParameterTemplate.objects.all().count() n = PartCategoryParameterTemplate.objects.all().count()
self.assertEqual(n, 2) self.assertEqual(n, 2)
parent_category = PartCategory.objects.get(pk=8).get_root() category = PartCategory.objects.get(pk=7)
self.assertEqual(parent_category.pk, 7)
c1 = PartCategoryParameterTemplate(category=parent_category, c1 = PartCategoryParameterTemplate(category=category,
parameter_template=t1, parameter_template=t1,
default_value='xyz') default_value='xyz')
c1.save() c1.save()

View File

@ -638,8 +638,9 @@ class PartCreate(AjaxCreateView):
# Hide the default_supplier field (there are no matching supplier parts yet!) # Hide the default_supplier field (there are no matching supplier parts yet!)
form.fields['default_supplier'].widget = HiddenInput() form.fields['default_supplier'].widget = HiddenInput()
# Force display of the 'category_templates' widget # Display category templates widgets
form.fields['category_templates'].widget = CheckboxInput() form.fields['selected_category_templates'].widget = CheckboxInput()
form.fields['parent_category_templates'].widget = CheckboxInput()
return form return form
@ -693,17 +694,40 @@ class PartCreate(AjaxCreateView):
except AttributeError: except AttributeError:
pass pass
# Create part parameters # Store templates added to part
category_templates = form.cleaned_data['category_templates'] template_list = []
# Create part parameters for selected category
category_templates = form.cleaned_data['selected_category_templates']
if 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() category = form.cleaned_data['category'].get_root()
for template in category.get_parameter_templates(): for template in category.get_parameter_templates():
PartParameter.create(part=part, # Check that template wasn't already added
template=template.parameter_template, if template.parameter_template not in template_list:
data=template.default_value, try:
save=True) 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) return self.renderJsonResponse(request, form, data, context=context)
@ -729,7 +753,8 @@ class PartCreate(AjaxCreateView):
initials[label] = self.request.GET.get(label) initials[label] = self.request.GET.get(label)
# Automatically create part parameters from category templates # 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 return initials
@ -2323,13 +2348,9 @@ class CategoryParameterTemplateCreate(AjaxCreateView):
default_value = form.cleaned_data['default_value'] default_value = form.cleaned_data['default_value']
# Add parameter template and default value to all categories # Add parameter template and default value to all categories
for category_id, category_name in PartCategory.get_parent_categories(): for category in PartCategory.objects.all():
# Change category_id type to integer
category_id = int(category_id)
# Skip selected category (will be processed in the post call) # Skip selected category (will be processed in the post call)
if category_id != selected_category: if category.pk != selected_category:
# Get category
category = PartCategory.objects.get(pk=category_id)
try: try:
cat_template = PartCategoryParameterTemplate.objects.create(category=category, cat_template = PartCategoryParameterTemplate.objects.create(category=category,
parameter_template=parameter_template, parameter_template=parameter_template,
@ -2368,6 +2389,7 @@ class CategoryParameterTemplateEdit(AjaxUpdateView):
form = super(AjaxUpdateView, self).get_form() form = super(AjaxUpdateView, self).get_form()
form.fields['category'].widget = HiddenInput() form.fields['category'].widget = HiddenInput()
form.fields['add_to_all_categories'].widget = HiddenInput()
if form.is_valid(): if form.is_valid():
form.cleaned_data['category'] = self.kwargs.get('pk', None) form.cleaned_data['category'] = self.kwargs.get('pk', None)