Improvements for status code generation

- Now includes labels
- Python template generates javascript which is then rendered? I don't even follow it any more
This commit is contained in:
Oliver Walters 2020-04-11 20:48:02 +10:00
parent 5d70f496a5
commit ba7c0bdea0
6 changed files with 81 additions and 24 deletions

View File

@ -50,12 +50,17 @@ function loadBuildTable(table, options) {
sortable: true, sortable: true,
}, },
{ {
field: 'status_text', field: 'status',
title: 'Status', title: 'Status',
sortable: true,
formatter: function(value, row, index, field) {
return buildStatusDisplay(value);
},
}, },
{ {
field: 'creation_date', field: 'creation_date',
title: 'Created', title: 'Created',
sortable: true,
}, },
], ],
}); });

View File

@ -216,8 +216,9 @@ function generateFilterInput(tableKey, filterKey) {
// Return a 'select' input with the available values // Return a 'select' input with the available values
html = `<select class='form-control' id='${id}' name='value'>`; html = `<select class='form-control' id='${id}' name='value'>`;
for (var opt in options) { for (var key in options) {
html += `<option value='${options[opt]}'>${opt}</option>`; option = options[key];
html += `<option value='${key}'>${option.value}</option>`;
} }
html += `</select>`; html += `</select>`;
@ -368,7 +369,6 @@ function getFilterOptionValue(tableKey, filterKey, valueKey) {
var filter = getFilterSettings(tableKey, filterKey); var filter = getFilterSettings(tableKey, filterKey);
var value = String(valueKey); var value = String(valueKey);
// Lookup for boolean options // Lookup for boolean options
@ -381,11 +381,10 @@ function getFilterOptionValue(tableKey, filterKey, valueKey) {
// Iterate through a list of options // Iterate through a list of options
if ('options' in filter) { if ('options' in filter) {
for (var option in filter.options) { for (var key in filter.options) {
var v = String(filter.options[option]);
if (v == valueKey) { if (key == valueKey) {
return option; return filter.options[key].value;
} }
} }

View File

@ -187,7 +187,5 @@ function orderStatusLabel(code, label) {
html += label; html += label;
html += "</span>"; html += "</span>";
console.log(html);
return html; return html;
} }

View File

@ -7,6 +7,8 @@ class StatusCode:
This is used to map a set of integer values to text. This is used to map a set of integer values to text.
""" """
labels = {}
@classmethod @classmethod
def list(cls): def list(cls):
""" """
@ -16,10 +18,18 @@ class StatusCode:
codes = [] codes = []
for key in cls.options.keys(): for key in cls.options.keys():
codes.append({
opt = {
'key': key, 'key': key,
'value': cls.options[key] 'value': cls.options[key]
}) }
label = cls.labels.get(key)
if label:
opt['label'] = label
codes.append(opt)
return codes return codes
@ -61,6 +71,15 @@ class OrderStatus(StatusCode):
RETURNED: _("Returned"), RETURNED: _("Returned"),
} }
labels = {
PENDING: "primary",
PLACED: "primary",
COMPLETE: "success",
CANCELLED: "danger",
LOST: "warning",
RETURNED: "warning",
}
# Open orders # Open orders
OPEN = [ OPEN = [
PENDING, PENDING,
@ -91,6 +110,12 @@ class StockStatus(StatusCode):
LOST: _("Lost"), LOST: _("Lost"),
} }
labels = {
OK: 'success',
ATTENTION: 'warning',
DAMAGED: 'danger',
}
# The following codes correspond to parts that are 'available' or 'in stock' # The following codes correspond to parts that are 'available' or 'in stock'
AVAILABLE_CODES = [ AVAILABLE_CODES = [
OK, OK,
@ -120,6 +145,13 @@ class BuildStatus(StatusCode):
COMPLETE: _("Complete"), COMPLETE: _("Complete"),
} }
labels = {
PENDING: 'primary',
ALLOCATED: 'info',
COMPLETE: 'success',
CANCELLED: 'danger',
}
ACTIVE_CODES = [ ACTIVE_CODES = [
PENDING, PENDING,
ALLOCATED ALLOCATED

View File

@ -0,0 +1,27 @@
var {{ label }}Codes = {
{% for opt in options %}'{{ opt.key }}': {
key: '{{ opt.key }}',
value: '{{ opt.value }}',{% if opt.label %}
label: '{{ opt.label }}',{% endif %}
},{% endfor %}
};
function {{ label }}StatusDisplay(key) {
key = String(key);
var label = {{ label }}Codes[key].label;
var value = {{ label }}Codes[key].value;
if (value == null || value.length == 0) {
value = key;
}
// Label not found, return the original string
if (label == null || label.length == 0) {
return value;
}
return `<span class='label label-${label}'>${value}</span>`;
}

View File

@ -5,6 +5,11 @@
<script type='text/javascript'> <script type='text/javascript'>
{% include "status_codes.html" with label='stock' options=stock_status_codes %}
{% include "status_codes.html" with label='build' options=build_status_codes %}
{% include "status_codes.html" with label='order' options=order_status_codes %}
function getAvailableTableFilters(tableKey) { function getAvailableTableFilters(tableKey) {
tableKey = tableKey.toLowerCase(); tableKey = tableKey.toLowerCase();
@ -23,10 +28,7 @@ function getAvailableTableFilters(tableKey) {
description: '{% trans "Show stock for active parts" %}', description: '{% trans "Show stock for active parts" %}',
}, },
'status': { 'status': {
options: { options: stockCodes,
{% for opt in stock_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %}
},
title: '{% trans "Stock status" %}', title: '{% trans "Stock status" %}',
description: '{% trans "Stock status" %}', description: '{% trans "Stock status" %}',
}, },
@ -38,10 +40,7 @@ function getAvailableTableFilters(tableKey) {
return { return {
status: { status: {
title: '{% trans "Build status" %}', title: '{% trans "Build status" %}',
options: { options: buildCodes,
{% for opt in build_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %}
},
}, },
}; };
@ -52,10 +51,7 @@ function getAvailableTableFilters(tableKey) {
return { return {
status: { status: {
title: '{% trans "Order status" %}', title: '{% trans "Order status" %}',
options: { options: orderCodes,
{% for opt in order_status_codes %}'{{ opt.value }}': '{{ opt.key }}',
{% endfor %}
},
}, },
}; };
} }