mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
Allow override of values from calling function
This commit is contained in:
parent
5230a5a41b
commit
cf0feffe26
@ -70,6 +70,7 @@ class CompanySerializer(InvenTreeModelSerializer):
|
|||||||
'phone',
|
'phone',
|
||||||
'address',
|
'address',
|
||||||
'email',
|
'email',
|
||||||
|
'currency',
|
||||||
'contact',
|
'contact',
|
||||||
'link',
|
'link',
|
||||||
'image',
|
'image',
|
||||||
|
@ -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 %}
|
{% 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'>
|
<div id='button-toolbar'>
|
||||||
<button type='button' class="btn btn-success" id='new-company'>
|
<button type='button' class="btn btn-success" id='new-company'>
|
||||||
<span class='fas fa-plus-circle'></span> {{ button_text }} (Server Side)
|
<span class='fas fa-plus-circle'></span> {{ button_text }}
|
||||||
</button>
|
|
||||||
<button type='button' class="btn btn-success" id='new-company-2'>
|
|
||||||
<span class='fas fa-plus-circle'></span> {{ button_text }} (Client Side)
|
|
||||||
</button>
|
</button>
|
||||||
<div class='btn-group'>
|
<div class='btn-group'>
|
||||||
</div>
|
</div>
|
||||||
@ -32,60 +29,49 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block js_ready %}
|
{% block js_ready %}
|
||||||
{{ block.super }}
|
{{ 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(
|
constructForm(
|
||||||
'{% url "api-build-list" %}',
|
'{% url "api-company-list" %}',
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
title: '{% trans "Edit Part Details" %}',
|
title: '{% trans "Create new Company" %}',
|
||||||
|
follow: true,
|
||||||
fields: {
|
fields: {
|
||||||
title: {
|
name: {},
|
||||||
prefix: `<span class='fas fa-user'></span>`
|
|
||||||
},
|
|
||||||
reference: {},
|
|
||||||
part: {
|
|
||||||
filters: {
|
|
||||||
}
|
|
||||||
},
|
|
||||||
quantity: {},
|
|
||||||
/*
|
|
||||||
name: {
|
|
||||||
onEdit: function() {
|
|
||||||
console.log('Edited name field');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
description: {},
|
description: {},
|
||||||
category: {
|
website: {},
|
||||||
filters: {
|
address: {},
|
||||||
parent: 1,
|
currency: {},
|
||||||
},
|
phone: {},
|
||||||
secondary: {
|
email: {},
|
||||||
label: '{% trans "New Category" %}',
|
contact: {},
|
||||||
},
|
is_supplier: {
|
||||||
|
value: {% if pagetype == 'suppliers' %}true{% else %}false{% endif %},
|
||||||
},
|
},
|
||||||
active: {
|
is_manufacturer: {
|
||||||
onEdit: function() {
|
value: {% if pagetype == 'manufacturers' %}true{% else %}false{% endif %},
|
||||||
console.log('edited active field');
|
},
|
||||||
}
|
is_customer: {
|
||||||
|
value: {% if pagetype == 'customers' %}true{% else %}false{% endif %},
|
||||||
},
|
},
|
||||||
purchaseable: {},
|
|
||||||
salable: {},
|
|
||||||
component: {},
|
|
||||||
'website',
|
|
||||||
'address',
|
|
||||||
'phone',
|
|
||||||
'email',
|
|
||||||
'contact',
|
|
||||||
'is_supplier',
|
|
||||||
'is_manufacturer',
|
|
||||||
'is_customer',
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -188,21 +188,6 @@ class CompanyViewTest(CompanyViewTestBase):
|
|||||||
response = self.client.get(reverse('company-index'))
|
response = self.client.get(reverse('company-index'))
|
||||||
self.assertEqual(response.status_code, 200)
|
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):
|
class ManufacturerPartViewTests(CompanyViewTestBase):
|
||||||
"""
|
"""
|
||||||
|
@ -63,21 +63,18 @@ class CompanyIndex(InvenTreeRoleMixin, ListView):
|
|||||||
'title': _('Suppliers'),
|
'title': _('Suppliers'),
|
||||||
'button_text': _('New Supplier'),
|
'button_text': _('New Supplier'),
|
||||||
'filters': {'is_supplier': 'true'},
|
'filters': {'is_supplier': 'true'},
|
||||||
'create_url': reverse('supplier-create'),
|
|
||||||
'pagetype': 'suppliers',
|
'pagetype': 'suppliers',
|
||||||
},
|
},
|
||||||
reverse('manufacturer-index'): {
|
reverse('manufacturer-index'): {
|
||||||
'title': _('Manufacturers'),
|
'title': _('Manufacturers'),
|
||||||
'button_text': _('New Manufacturer'),
|
'button_text': _('New Manufacturer'),
|
||||||
'filters': {'is_manufacturer': 'true'},
|
'filters': {'is_manufacturer': 'true'},
|
||||||
'create_url': reverse('manufacturer-create'),
|
|
||||||
'pagetype': 'manufacturers',
|
'pagetype': 'manufacturers',
|
||||||
},
|
},
|
||||||
reverse('customer-index'): {
|
reverse('customer-index'): {
|
||||||
'title': _('Customers'),
|
'title': _('Customers'),
|
||||||
'button_text': _('New Customer'),
|
'button_text': _('New Customer'),
|
||||||
'filters': {'is_customer': 'true'},
|
'filters': {'is_customer': 'true'},
|
||||||
'create_url': reverse('customer-create'),
|
|
||||||
'pagetype': 'customers',
|
'pagetype': 'customers',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,7 +83,6 @@ class CompanyIndex(InvenTreeRoleMixin, ListView):
|
|||||||
'title': _('Companies'),
|
'title': _('Companies'),
|
||||||
'button_text': _('New Company'),
|
'button_text': _('New Company'),
|
||||||
'filters': {},
|
'filters': {},
|
||||||
'create_url': reverse('company-create'),
|
|
||||||
'pagetype': 'companies'
|
'pagetype': 'companies'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,75 @@
|
|||||||
{% load i18n %}
|
{% 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={}) {
|
function loadCompanyTable(table, url, options={}) {
|
||||||
/*
|
/*
|
||||||
* Load company listing data into specified table.
|
* Load company listing data into specified table.
|
||||||
|
@ -109,14 +109,24 @@ function getApiEndpointOptions(url, callback, options) {
|
|||||||
* -
|
* -
|
||||||
*/
|
*/
|
||||||
function constructCreateForm(fields, options) {
|
function constructCreateForm(fields, options) {
|
||||||
|
|
||||||
// Check if default values were provided for any fields
|
// Check if default values were provided for any fields
|
||||||
for (const name in fields) {
|
for (const name in fields) {
|
||||||
|
|
||||||
var field = fields[name];
|
var field = fields[name];
|
||||||
|
|
||||||
if (field.default != null) {
|
var field_options = options.fields[name] || {};
|
||||||
field.value = field.default;
|
|
||||||
|
// 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
|
// Field prefix
|
||||||
fields[field].prefix = field_options.prefix;
|
fields[field].prefix = field_options.prefix;
|
||||||
|
|
||||||
|
// // Field value?
|
||||||
|
// if (fields[field].value == null) {
|
||||||
|
// fields[field].value = field_options.value;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user