diff --git a/InvenTree/part/forms.py b/InvenTree/part/forms.py index d1a3dcd74e..81a75a22fb 100644 --- a/InvenTree/part/forms.py +++ b/InvenTree/part/forms.py @@ -249,7 +249,7 @@ class EditCategoryForm(HelperForm): class EditCategoryParameterTemplateForm(HelperForm): - """ Form for editing a PartParameterTemplate object """ + """ Form for editing a PartCategoryParameterTemplate object """ class Meta: model = PartCategoryParameterTemplate diff --git a/InvenTree/part/urls.py b/InvenTree/part/urls.py index b5881fd0b4..488f4fe12e 100644 --- a/InvenTree/part/urls.py +++ b/InvenTree/part/urls.py @@ -76,15 +76,15 @@ part_detail_urls = [ category_parameter_urls = [ url(r'^new/', views.CategoryParameterTemplateCreate.as_view(), name='category-param-template-create'), - # url(r'^(?P\d+)/edit/', views.PartParameterTemplateEdit.as_view(), name='category-param-template-edit'), - # url(r'^(?P\d+)/delete/', views.PartParameterTemplateDelete.as_view(), name='category-param-template-edit'), + url(r'^(?P\d+)/edit/', views.CategoryParameterTemplateEdit.as_view(), name='category-param-template-edit'), + url(r'^(?P\d+)/delete/', views.CategoryParameterTemplateDelete.as_view(), name='category-param-template-delete'), ] part_category_urls = [ url(r'^edit/?', views.CategoryEdit.as_view(), name='category-edit'), 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'^.*$', views.CategoryDetail.as_view(), name='category-detail'), diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index d13f2223b1..09eb8b8a4f 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -2174,9 +2174,97 @@ class CategoryParameterTemplateCreate(AjaxCreateView): if form.is_valid(): 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 +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): """ Detail view for BomItem """ context_object_name = 'item' diff --git a/InvenTree/templates/InvenTree/settings/category.html b/InvenTree/templates/InvenTree/settings/category.html index c91058e02b..d5e27d79a7 100644 --- a/InvenTree/templates/InvenTree/settings/category.html +++ b/InvenTree/templates/InvenTree/settings/category.html @@ -52,14 +52,23 @@ }, { field: 'parameter_template_detail.name', - title: 'Parameter Template', + title: '{% trans "Parameter Template" %}', sortable: 'true', }, { field: 'default_value', - title: 'Default Value', + title: '{% trans "Default Value" %}', sortable: 'true', - }, + formatter: function(value, row, index, field) { + var bEdit = ""; + var bDel = ""; + + var html = value + html += "
" + bEdit + bDel + "
"; + + 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 %} {% endblock %}