Refactor PartParameterCreate form

This commit is contained in:
Oliver 2021-07-09 02:03:33 +10:00
parent cbf0e0bd4a
commit 0a86d947bc
5 changed files with 27 additions and 61 deletions

View File

@ -43,14 +43,21 @@
{% if roles.part.add %}
$('#param-create').click(function() {
launchModalForm("{% url 'part-param-create' %}?part={{ part.id }}", {
reload: true,
secondary: [{
field: 'template',
label: '{% trans "New Template" %}',
title: '{% trans "Create New Parameter Template" %}',
url: "{% url 'part-param-template-create' %}"
}],
constructForm('{% url "api-part-parameter-list" %}', {
method: 'POST',
fields: {
part: {
value: {{ part.pk }},
hidden: true,
},
template: {},
data: {},
},
title: '{% trans "Add Parameter" %}',
onSuccess: function() {
$('#parameter-table').bootstrapTable('refresh');
}
});
});
{% endif %}

View File

@ -33,8 +33,6 @@ part_parameter_urls = [
url(r'^template/new/', views.PartParameterTemplateCreate.as_view(), name='part-param-template-create'),
url(r'^template/(?P<pk>\d+)/edit/', views.PartParameterTemplateEdit.as_view(), name='part-param-template-edit'),
url(r'^template/(?P<pk>\d+)/delete/', views.PartParameterTemplateDelete.as_view(), name='part-param-template-edit'),
url(r'^new/', views.PartParameterCreate.as_view(), name='part-param-create'),
]
part_detail_urls = [

View File

@ -32,7 +32,7 @@ from rapidfuzz import fuzz
from decimal import Decimal, InvalidOperation
from .models import PartCategory, Part, PartRelated
from .models import PartParameterTemplate, PartParameter
from .models import PartParameterTemplate
from .models import PartCategoryParameterTemplate
from .models import BomItem
from .models import match_part_names
@ -2257,56 +2257,6 @@ class PartParameterTemplateDelete(AjaxDeleteView):
ajax_form_title = _("Delete Part Parameter Template")
class PartParameterCreate(AjaxCreateView):
""" View for creating a new PartParameter """
model = PartParameter
form_class = part_forms.EditPartParameterForm
ajax_form_title = _('Create Part Parameter')
def get_initial(self):
initials = {}
part_id = self.request.GET.get('part', None)
if part_id:
try:
initials['part'] = Part.objects.get(pk=part_id)
except (Part.DoesNotExist, ValueError):
pass
return initials
def get_form(self):
""" Return the form object.
- Hide the 'Part' field (specified in URL)
- Limit the 'Template' options (to avoid duplicates)
"""
form = super().get_form()
part_id = self.request.GET.get('part', None)
if part_id:
try:
part = Part.objects.get(pk=part_id)
form.fields['part'].widget = HiddenInput()
query = form.fields['template'].queryset
query = query.exclude(id__in=[param.template.id for param in part.parameters.all()])
form.fields['template'].queryset = query
except (Part.DoesNotExist, ValueError):
pass
return form
class CategoryDetail(InvenTreeRoleMixin, DetailView):
""" Detail view for PartCategory """

View File

@ -1228,6 +1228,9 @@ function renderModelData(name, model, data, parameters, options) {
case 'partcategory':
renderer = renderPartCategory;
break;
case 'partparametertemplate':
renderer = renderPartParameterTemplate;
break;
case 'supplierpart':
renderer = renderSupplierPart;
break;

View File

@ -166,6 +166,14 @@ function renderPartCategory(name, data, parameters, options) {
}
function renderPartParameterTemplate(name, data, parameters, options) {
var html = `<span>${data.name} - [${data.units}]</span>`;
return html;
}
// Rendered for "SupplierPart" model
function renderSupplierPart(name, data, parameters, options) {