mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 05:26:45 +00:00
Barcode scan fix (#3727)
* Fix barcode scanning in web interface * Improve error handling for barcode scan dialog * JS linting
This commit is contained in:
parent
7568d23670
commit
8bcf72fbb2
@ -34,19 +34,29 @@ class InvenTreeBarcodePlugin(BarcodeMixin, InvenTreePlugin):
|
|||||||
def format_matched_response(self, label, model, instance):
|
def format_matched_response(self, label, model, instance):
|
||||||
"""Format a response for the scanned data"""
|
"""Format a response for the scanned data"""
|
||||||
|
|
||||||
response = {
|
data = {
|
||||||
'pk': instance.pk
|
'pk': instance.pk
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add in the API URL if available
|
# Add in the API URL if available
|
||||||
if hasattr(model, 'get_api_url'):
|
if hasattr(model, 'get_api_url'):
|
||||||
response['api_url'] = f"{model.get_api_url()}{instance.pk}/"
|
data['api_url'] = f"{model.get_api_url()}{instance.pk}/"
|
||||||
|
|
||||||
# Add in the web URL if available
|
# Add in the web URL if available
|
||||||
if hasattr(instance, 'get_absolute_url'):
|
if hasattr(instance, 'get_absolute_url'):
|
||||||
response['web_url'] = instance.get_absolute_url()
|
url = instance.get_absolute_url()
|
||||||
|
data['web_url'] = url
|
||||||
|
else:
|
||||||
|
url = None
|
||||||
|
|
||||||
return {label: response}
|
response = {
|
||||||
|
label: data
|
||||||
|
}
|
||||||
|
|
||||||
|
if url is not None:
|
||||||
|
response['url'] = url
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
class InvenTreeInternalBarcodePlugin(InvenTreeBarcodePlugin):
|
class InvenTreeInternalBarcodePlugin(InvenTreeBarcodePlugin):
|
||||||
|
@ -129,9 +129,25 @@ function postBarcodeData(barcode_data, options={}) {
|
|||||||
data,
|
data,
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
error: function() {
|
error: function(xhr) {
|
||||||
|
|
||||||
enableBarcodeInput(modal, true);
|
enableBarcodeInput(modal, true);
|
||||||
showBarcodeMessage(modal, '{% trans "Server error" %}');
|
|
||||||
|
switch (xhr.status || 0) {
|
||||||
|
case 400:
|
||||||
|
// No match for barcode, most likely
|
||||||
|
console.log(xhr);
|
||||||
|
|
||||||
|
data = xhr.responseJSON || {};
|
||||||
|
showBarcodeMessage(modal, data.error || '{% trans "Server error" %}');
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Any other error code means something went wrong
|
||||||
|
$(modal).modal('hide');
|
||||||
|
|
||||||
|
showApiError(xhr, url);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
success: function(response, status) {
|
success: function(response, status) {
|
||||||
modalEnable(modal, false);
|
modalEnable(modal, false);
|
||||||
@ -166,6 +182,9 @@ function postBarcodeData(barcode_data, options={}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Display a message within the barcode scanning dialog
|
||||||
|
*/
|
||||||
function showBarcodeMessage(modal, message, style='danger') {
|
function showBarcodeMessage(modal, message, style='danger') {
|
||||||
|
|
||||||
var html = `<div class='alert alert-block alert-${style}'>`;
|
var html = `<div class='alert alert-block alert-${style}'>`;
|
||||||
@ -179,7 +198,10 @@ function showBarcodeMessage(modal, message, style='danger') {
|
|||||||
|
|
||||||
|
|
||||||
function showInvalidResponseError(modal, response, status) {
|
function showInvalidResponseError(modal, response, status) {
|
||||||
showBarcodeMessage(modal, `{% trans "Invalid server response" %}<br>{% trans "Status" %}: '${status}'`);
|
showBarcodeMessage(
|
||||||
|
modal,
|
||||||
|
`{% trans "Invalid server response" %}<br>{% trans "Status" %}: '${status}'`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -320,12 +342,11 @@ function barcodeDialog(title, options={}) {
|
|||||||
$(modal).modal('show');
|
$(modal).modal('show');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Perform a barcode scan,
|
||||||
|
* and (potentially) redirect the browser
|
||||||
|
*/
|
||||||
function barcodeScanDialog() {
|
function barcodeScanDialog() {
|
||||||
/*
|
|
||||||
* Perform a barcode scan,
|
|
||||||
* and (potentially) redirect the browser
|
|
||||||
*/
|
|
||||||
|
|
||||||
var modal = '#modal-form';
|
var modal = '#modal-form';
|
||||||
|
|
||||||
@ -333,11 +354,12 @@ function barcodeScanDialog() {
|
|||||||
'{% trans "Scan Barcode" %}',
|
'{% trans "Scan Barcode" %}',
|
||||||
{
|
{
|
||||||
onScan: function(response) {
|
onScan: function(response) {
|
||||||
if ('url' in response) {
|
|
||||||
$(modal).modal('hide');
|
|
||||||
|
|
||||||
// Redirect to the URL!
|
var url = response.url;
|
||||||
window.location.href = response.url;
|
|
||||||
|
if (url) {
|
||||||
|
$(modal).modal('hide');
|
||||||
|
window.location.href = url;
|
||||||
} else {
|
} else {
|
||||||
showBarcodeMessage(
|
showBarcodeMessage(
|
||||||
modal,
|
modal,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user