Allow override of values from calling function

This commit is contained in:
Oliver 2021-06-29 20:44:44 +10:00
parent 5230a5a41b
commit cf0feffe26
6 changed files with 125 additions and 72 deletions

View File

@ -70,6 +70,7 @@ class CompanySerializer(InvenTreeModelSerializer):
'phone',
'address',
'email',
'currency',
'contact',
'link',
'image',

View File

@ -16,10 +16,7 @@
{% if pagetype == 'manufacturers' and roles.purchase_order.add or pagetype == 'suppliers' and roles.purchase_order.add or pagetype == 'customers' and roles.sales_order.add %}
<div id='button-toolbar'>
<button type='button' class="btn btn-success" id='new-company'>
<span class='fas fa-plus-circle'></span> {{ button_text }} (Server Side)
</button>
<button type='button' class="btn btn-success" id='new-company-2'>
<span class='fas fa-plus-circle'></span> {{ button_text }} (Client Side)
<span class='fas fa-plus-circle'></span> {{ button_text }}
</button>
<div class='btn-group'>
</div>
@ -32,60 +29,49 @@
{% endblock %}
{% block js_ready %}
{{ block.super }}
$('#new-company').click(function () {
launchModalForm("{{ create_url }}", {
follow: true
});
});
$('#new-company-2').click(function() {
$('#new-company').click(function() {
createCompany({
fields: {
is_supplier: {
value: {% if pagetype == 'suppliers' %}true{% else %}false{% endif %},
},
is_manufacturer: {
value: {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %},
},
is_customer: {
value: {% if pagetype == 'customers' %}true{% else %}false{% endif %},
},
}
});
return;
constructForm(
'{% url "api-build-list" %}',
'{% url "api-company-list" %}',
{
method: 'POST',
title: '{% trans "Edit Part Details" %}',
title: '{% trans "Create new Company" %}',
follow: true,
fields: {
title: {
prefix: `<span class='fas fa-user'></span>`
},
reference: {},
part: {
filters: {
}
},
quantity: {},
/*
name: {
onEdit: function() {
console.log('Edited name field');
}
},
name: {},
description: {},
category: {
filters: {
parent: 1,
},
secondary: {
label: '{% trans "New Category" %}',
},
website: {},
address: {},
currency: {},
phone: {},
email: {},
contact: {},
is_supplier: {
value: {% if pagetype == 'suppliers' %}true{% else %}false{% endif %},
},
active: {
onEdit: function() {
console.log('edited active field');
}
is_manufacturer: {
value: {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %},
},
is_customer: {
value: {% if pagetype == 'customers' %}true{% else %}false{% endif %},
},
purchaseable: {},
salable: {},
component: {},
'website',
'address',
'phone',
'email',
'contact',
'is_supplier',
'is_manufacturer',
'is_customer',
*/
}
}
);

View File

@ -188,21 +188,6 @@ class CompanyViewTest(CompanyViewTestBase):
response = self.client.get(reverse('company-index'))
self.assertEqual(response.status_code, 200)
def test_company_create(self):
"""
Test the view for creating a company
"""
# Check that different company types return different form titles
response = self.client.get(reverse('supplier-create'), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(response, 'Create new Supplier')
response = self.client.get(reverse('manufacturer-create'), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(response, 'Create new Manufacturer')
response = self.client.get(reverse('customer-create'), HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(response, 'Create new Customer')
class ManufacturerPartViewTests(CompanyViewTestBase):
"""

View File

@ -63,21 +63,18 @@ class CompanyIndex(InvenTreeRoleMixin, ListView):
'title': _('Suppliers'),
'button_text': _('New Supplier'),
'filters': {'is_supplier': 'true'},
'create_url': reverse('supplier-create'),
'pagetype': 'suppliers',
},
reverse('manufacturer-index'): {
'title': _('Manufacturers'),
'button_text': _('New Manufacturer'),
'filters': {'is_manufacturer': 'true'},
'create_url': reverse('manufacturer-create'),
'pagetype': 'manufacturers',
},
reverse('customer-index'): {
'title': _('Customers'),
'button_text': _('New Customer'),
'filters': {'is_customer': 'true'},
'create_url': reverse('customer-create'),
'pagetype': 'customers',
}
}
@ -86,7 +83,6 @@ class CompanyIndex(InvenTreeRoleMixin, ListView):
'title': _('Companies'),
'button_text': _('New Company'),
'filters': {},
'create_url': reverse('company-create'),
'pagetype': 'companies'
}

View File

@ -1,5 +1,75 @@
{% load i18n %}
/*
* Launches a form to create a new company.
* As this can be called from many different contexts,
* we abstract it here!
*/
function createCompany(options={}) {
// Default field set
var fields = {
name: {},
description: {},
website: {},
address: {},
currency: {},
phone: {},
email: {},
contact: {},
is_supplier: {},
is_manufacturer: {},
is_customer: {}
};
// Override / update default fields as required
fields = Object.assign(fields, options.fields || {});
constructForm(
'{% url "api-company-list" %}',
{
method: 'POST',
fields: fields,
follow: true,
title: '{% trans "Add new Company" %}',
}
);
}
// Launch form to create a new manufacturer part
function createManufacturerPart(options={}) {
var fields = {
'part': {
secondary: {
label: '{% trans "New Part" %}',
}
},
'manufacturer': {
secondary: {
label: '{% trans "New Manufacturer" %}',
}
},
'MPN': {},
'description': {},
'link': {},
};
fields = Object.assign(fields, options.fields || {});
constructForm(
'{% url "api-manufacturer-part-list" %}',
{
fields: fields,
method: 'POST',
follow: true,
title: '{% trans "Add new Manufacturer Part" %}',
}
);
}
function loadCompanyTable(table, url, options={}) {
/*
* Load company listing data into specified table.

View File

@ -109,14 +109,24 @@ function getApiEndpointOptions(url, callback, options) {
* -
*/
function constructCreateForm(fields, options) {
// Check if default values were provided for any fields
for (const name in fields) {
var field = fields[name];
if (field.default != null) {
field.value = field.default;
var field_options = options.fields[name] || {};
// If a 'value' is not provided for the field,
if (field.value == null) {
if ('value' in field_options) {
// Client has specified the default value for the field
field.value = field_options.value;
} else if (field.default != null) {
// OPTIONS endpoint provided default value for this field
field.value = field.default;
}
}
}
@ -278,6 +288,11 @@ function constructFormBody(fields, options) {
// Field prefix
fields[field].prefix = field_options.prefix;
// // Field value?
// if (fields[field].value == null) {
// fields[field].value = field_options.value;
// }
}
}