2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-02 05:26:45 +00:00

Include URL data in barcode scan endpoint

This commit is contained in:
Oliver Walters 2020-06-11 19:21:38 +10:00
parent 3aecb110a5
commit 2d9b75c6ae
3 changed files with 57 additions and 5 deletions

View File

@ -52,6 +52,7 @@ function makeBarcodeInput(placeholderText='') {
*/ */
var html = ` var html = `
<div id='barcode-error-message'></div>
<form class='js-modal-form' method='post'> <form class='js-modal-form' method='post'>
<div class='form-group'> <div class='form-group'>
<label class='control-label' for='barcode'>Barcode</label> <label class='control-label' for='barcode'>Barcode</label>
@ -72,9 +73,35 @@ function makeBarcodeInput(placeholderText='') {
} }
function showBarcodeError(modal, message, style='danger') {
var html = `<div class='alert alert-block alert-${style}'>`;
html += message;
html += "</div>";
$(modal + ' #barcode-error-message').html(html);
}
function clearBarcodeError(modal, message) {
$(modal + ' #barcode-error-message').html('');
}
function getBarcodeData(modal) { function getBarcodeData(modal) {
return $(modal + ' #barcode').val(); modal = modal || '#modal-form';
var el = $(modal + ' #barcode');
var barcode = el.val();
el.val('');
el.focus();
return barcode;
} }
@ -164,7 +191,26 @@ function barcodeScanDialog() {
{ {
method: 'POST', method: 'POST',
success: function(response, status) { success: function(response, status) {
console.log(response); console.log(response);
if (status == 'success') {
if ('success' in response) {
if ('url' in response) {
// Redirect to the URL!
$(modal).modal('hide');
window.location.href = response.url;
}
} else if ('error' in response) {
showBarcodeError(modal, response.error, 'warning');
} else {
showBarcodeError(modal, "Unknown response from server", 'warning');
}
} else {
showBarcodeError(modal, `Invalid server response.<br>Status code: '${status}'`);
}
}, },
}, },
); );

View File

@ -2,6 +2,7 @@
import hashlib import hashlib
from django.urls import reverse
from django.conf.urls import url from django.conf.urls import url
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -86,6 +87,7 @@ class BarcodeScan(APIView):
if item is not None: if item is not None:
response['stockitem'] = plugin.renderStockItem(item) response['stockitem'] = plugin.renderStockItem(item)
response['url'] = reverse('stock-item-detail', kwargs={'pk': item.id})
match_found = True match_found = True
# Try to associate with a stock location # Try to associate with a stock location
@ -93,6 +95,7 @@ class BarcodeScan(APIView):
if loc is not None: if loc is not None:
response['stocklocation'] = plugin.renderStockLocation(loc) response['stocklocation'] = plugin.renderStockLocation(loc)
response['url'] = reverse('location-detail', kwargs={'pk': loc.id})
match_found = True match_found = True
# Try to associate with a part # Try to associate with a part
@ -100,6 +103,7 @@ class BarcodeScan(APIView):
if part is not None: if part is not None:
response['part'] = plugin.renderPart(part) response['part'] = plugin.renderPart(part)
response['url'] = reverse('part-detail', kwargs={'pk': part.id})
match_found = True match_found = True
response['hash'] = plugin.hash() response['hash'] = plugin.hash()
@ -118,6 +122,7 @@ class BarcodeScan(APIView):
item = StockItem.objects.get(uid=hash) item = StockItem.objects.get(uid=hash)
serializer = StockItemSerializer(item, part_detail=True, location_detail=True, supplier_part_detail=True) serializer = StockItemSerializer(item, part_detail=True, location_detail=True, supplier_part_detail=True)
response['stockitem'] = serializer.data response['stockitem'] = serializer.data
response['url'] = reverse('stock-item-detail', kwargs={'pk': item.id})
match_found = True match_found = True
except StockItem.DoesNotExist: except StockItem.DoesNotExist:
pass pass

View File

@ -390,10 +390,11 @@ $('#stock-add').click(function() {
$("#stock-delete").click(function () { $("#stock-delete").click(function () {
launchModalForm( launchModalForm(
"{% url 'stock-item-delete' item.id %}", "{% url 'stock-item-delete' item.id %}",
{ {
redirect: "{% url 'part-stock' item.part.id %}" redirect: "{% url 'part-stock' item.part.id %}"
}); }
);
}); });
{% endblock %} {% endblock %}