mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 13:05:42 +00:00
Implement a generic API endpoint for enumeration of status codes (#4543)
* Implement a generic API endpoint for enumeration of status codes * Adds endpoint for PurchaseOrderStatus * Add more endpoints (sales order / return orer) * Add endpoints for StockStatus and StockTrackingCode * Support build status * Use the attribute name as the dict key * Refactored status codes in javascript - Now accessible by "name" (instead of integer key) - Will make javascript code much more readable * Bump API version
This commit is contained in:
@ -15,10 +15,51 @@
|
||||
stockStatusDisplay,
|
||||
*/
|
||||
|
||||
{% include "status_codes.html" with label='stock' options=StockStatus.list %}
|
||||
{% include "status_codes.html" with label='stockHistory' options=StockHistoryCode.list %}
|
||||
{% include "status_codes.html" with label='build' options=BuildStatus.list %}
|
||||
{% include "status_codes.html" with label='purchaseOrder' options=PurchaseOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='salesOrder' options=SalesOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='returnOrder' options=ReturnOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='returnOrderLineItem' options=ReturnOrderLineStatus.list %}
|
||||
|
||||
/*
|
||||
* Generic function to render a status label
|
||||
*/
|
||||
function renderStatusLabel(key, codes, options={}) {
|
||||
|
||||
let text = null;
|
||||
let label = null;
|
||||
|
||||
// Find the entry which matches the provided key
|
||||
for (var name in codes) {
|
||||
let entry = codes[name];
|
||||
|
||||
if (entry.key == key) {
|
||||
text = entry.value;
|
||||
label = entry.label;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!text) {
|
||||
console.error(`renderStatusLabel could not find match for code ${key}`);
|
||||
}
|
||||
|
||||
// Fallback for color
|
||||
label = label || 'bg-dark';
|
||||
|
||||
if (!text) {
|
||||
text = key;
|
||||
}
|
||||
|
||||
let classes = `badge rounded-pill ${label}`;
|
||||
|
||||
if (options.classes) {
|
||||
classes += ` ${options.classes}`;
|
||||
}
|
||||
|
||||
return `<span class='${classes}'>${text}</span>`;
|
||||
}
|
||||
|
||||
|
||||
{% include "status_codes.html" with label='stock' data=StockStatus.list %}
|
||||
{% include "status_codes.html" with label='stockHistory' data=StockHistoryCode.list %}
|
||||
{% include "status_codes.html" with label='build' data=BuildStatus.list %}
|
||||
{% include "status_codes.html" with label='purchaseOrder' data=PurchaseOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='salesOrder' data=SalesOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='returnOrder' data=ReturnOrderStatus.list %}
|
||||
{% include "status_codes.html" with label='returnOrderLineItem' data=ReturnOrderLineStatus.list %}
|
||||
|
@ -1,11 +1,15 @@
|
||||
{% load report %}
|
||||
|
||||
/*
|
||||
* Status codes for the {{ label }} model.
|
||||
* Generated from the values specified in "status_codes.py"
|
||||
*/
|
||||
const {{ label }}Codes = {
|
||||
{% for opt in options %}'{{ opt.key }}': {
|
||||
key: '{{ opt.key }}',
|
||||
value: '{{ opt.value }}',{% if opt.color %}
|
||||
label: 'bg-{{ opt.color }}',{% endif %}
|
||||
{% for entry in data %}
|
||||
'{{ entry.name }}': {
|
||||
key: {{ entry.key }},
|
||||
value: '{{ entry.label }}',{% if entry.color %}
|
||||
label: 'bg-{{ entry.color }}',{% endif %}
|
||||
},
|
||||
{% endfor %}
|
||||
};
|
||||
@ -13,33 +17,7 @@ const {{ label }}Codes = {
|
||||
/*
|
||||
* Render the status for a {{ label }} object.
|
||||
* Uses the values specified in "status_codes.py"
|
||||
* This function is generated by the "status_codes.html" template
|
||||
*/
|
||||
function {{ label }}StatusDisplay(key, options={}) {
|
||||
|
||||
key = String(key);
|
||||
|
||||
var value = null;
|
||||
var label = null;
|
||||
|
||||
if (key in {{ label }}Codes) {
|
||||
value = {{ label }}Codes[key].value;
|
||||
label = {{ label }}Codes[key].label;
|
||||
}
|
||||
|
||||
// Fallback option for label
|
||||
label = label || 'bg-dark';
|
||||
|
||||
if (value == null || value.length == 0) {
|
||||
value = key;
|
||||
label = '';
|
||||
}
|
||||
|
||||
var classes = `badge rounded-pill ${label}`;
|
||||
|
||||
if (options.classes) {
|
||||
classes += ' ' + options.classes;
|
||||
}
|
||||
|
||||
return `<span class='${classes}'>${value}</span>`;
|
||||
return renderStatusLabel(key, {{ label }}Codes, options);
|
||||
}
|
||||
|
Reference in New Issue
Block a user