Added edit/delete for category parameter templates

This commit is contained in:
eeintech 2020-10-31 12:55:52 -05:00
parent 2a563d7370
commit 3a347fba21
4 changed files with 128 additions and 7 deletions

View File

@ -249,7 +249,7 @@ class EditCategoryForm(HelperForm):
class EditCategoryParameterTemplateForm(HelperForm): class EditCategoryParameterTemplateForm(HelperForm):
""" Form for editing a PartParameterTemplate object """ """ Form for editing a PartCategoryParameterTemplate object """
class Meta: class Meta:
model = PartCategoryParameterTemplate model = PartCategoryParameterTemplate

View File

@ -76,15 +76,15 @@ part_detail_urls = [
category_parameter_urls = [ category_parameter_urls = [
url(r'^new/', views.CategoryParameterTemplateCreate.as_view(), name='category-param-template-create'), url(r'^new/', views.CategoryParameterTemplateCreate.as_view(), name='category-param-template-create'),
# url(r'^(?P<pk>\d+)/edit/', views.PartParameterTemplateEdit.as_view(), name='category-param-template-edit'), url(r'^(?P<pid>\d+)/edit/', views.CategoryParameterTemplateEdit.as_view(), name='category-param-template-edit'),
# url(r'^(?P<pk>\d+)/delete/', views.PartParameterTemplateDelete.as_view(), name='category-param-template-edit'), url(r'^(?P<pid>\d+)/delete/', views.CategoryParameterTemplateDelete.as_view(), name='category-param-template-delete'),
] ]
part_category_urls = [ part_category_urls = [
url(r'^edit/?', views.CategoryEdit.as_view(), name='category-edit'), url(r'^edit/?', views.CategoryEdit.as_view(), name='category-edit'),
url(r'^delete/?', views.CategoryDelete.as_view(), name='category-delete'), url(r'^delete/?', views.CategoryDelete.as_view(), name='category-delete'),
url(r'^template/', include(category_parameter_urls)), url(r'^parameters/', include(category_parameter_urls)),
url(r'^parametric/?', views.CategoryParametric.as_view(), name='category-parametric'), url(r'^parametric/?', views.CategoryParametric.as_view(), name='category-parametric'),
url(r'^.*$', views.CategoryDetail.as_view(), name='category-detail'), url(r'^.*$', views.CategoryDetail.as_view(), name='category-detail'),

View File

@ -2174,9 +2174,97 @@ class CategoryParameterTemplateCreate(AjaxCreateView):
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)
try:
# Get category
category = self.get_initial()['category']
# Get existing related parts
parameters = [template.parameter_template.pk
for template in category.get_parameter_templates()]
# Exclude templates already linked to category
updated_choices = []
for choice in form.fields["parameter_template"].choices:
if (choice[0] not in parameters):
updated_choices.append(choice)
# Update choices for related part
form.fields['parameter_template'].choices = updated_choices
except KeyError:
pass
return form return form
class CategoryParameterTemplateEdit(AjaxUpdateView):
""" View for editing a PartCategoryParameterTemplate """
role_required = 'part.change'
model = PartCategoryParameterTemplate
form_class = part_forms.EditCategoryParameterTemplateForm
ajax_form_title = _('Edit Category Parameter Template')
def get_object(self):
try:
self.object = self.model.objects.get(pk=self.kwargs['pid'])
except:
return None
return self.object
def get_form(self):
""" Create a form to upload a new CategoryParameterTemplate
- Hide the 'category' field (parent part)
- Display parameter templates which are not yet related
"""
form = super(AjaxUpdateView, self).get_form()
form.fields['category'].widget = HiddenInput()
if form.is_valid():
form.cleaned_data['category'] = self.kwargs.get('pk', None)
try:
# Get category
category = PartCategory.objects.get(pk=self.kwargs.get('pk', None))
# Get existing related parts
parameters = [template.parameter_template.pk
for template in category.get_parameter_templates()]
# Exclude templates already linked to category
updated_choices = []
for choice in form.fields["parameter_template"].choices:
if (choice[0] not in parameters):
updated_choices.append(choice)
# Update choices for related part
form.fields['parameter_template'].choices = updated_choices
except KeyError:
pass
return form
class CategoryParameterTemplateDelete(AjaxDeleteView):
""" View for deleting an existing PartCategoryParameterTemplate """
role_required = 'part.delete'
model = PartCategoryParameterTemplate
ajax_form_title = _("Delete Category Parameter Template")
def get_object(self):
try:
self.object = self.model.objects.get(pk=self.kwargs['pid'])
except:
return None
return self.object
class BomItemDetail(InvenTreeRoleMixin, DetailView): class BomItemDetail(InvenTreeRoleMixin, DetailView):
""" Detail view for BomItem """ """ Detail view for BomItem """
context_object_name = 'item' context_object_name = 'item'

View File

@ -52,14 +52,23 @@
}, },
{ {
field: 'parameter_template_detail.name', field: 'parameter_template_detail.name',
title: 'Parameter Template', title: '{% trans "Parameter Template" %}',
sortable: 'true', sortable: 'true',
}, },
{ {
field: 'default_value', field: 'default_value',
title: 'Default Value', title: '{% trans "Default Value" %}',
sortable: 'true', sortable: 'true',
}, formatter: function(value, row, index, field) {
var bEdit = "<button title='{% trans "Edit Template" %}' class='template-edit btn btn-default btn-glyph' type='button' pk='" + row.pk + "'><span class='fas fa-edit'></span></button>";
var bDel = "<button title='{% trans "Delete Template" %}' class='template-delete btn btn-default btn-glyph' type='button' pk='" + row.pk + "'><span class='fas fa-trash-alt icon-red'></span></button>";
var html = value
html += "<div class='btn-group float-right' role='group'>" + bEdit + bDel + "</div>";
return html;
}
}
] ]
}); });
@ -72,5 +81,29 @@
}); });
}); });
$("#param-table").on('click', '.template-edit', function() {
var button = $(this);
var url = "/part/category/{{ category }}/parameters/" + button.attr('pk') + "/edit/";
launchModalForm(url, {
success: function() {
$("#param-table").bootstrapTable('refresh');
}
});
});
$("#param-table").on('click', '.template-delete', function() {
var button = $(this);
var url = "/part/category/{{ category }}/parameters/" + button.attr('pk') + "/delete/";
launchModalForm(url, {
success: function() {
$("#param-table").bootstrapTable('refresh');
}
});
});
{% endif %} {% endif %}
{% endblock %} {% endblock %}