Fixes for JSON API

- Set content type
- Stringify JSON data
- Finish API to stocktake multiple parts
This commit is contained in:
Oliver 2018-05-07 23:25:47 +10:00
parent 1363fa9f1f
commit b6944620dd
3 changed files with 71 additions and 6 deletions

View File

@ -23,6 +23,7 @@ function inventreeGet(url, filters={}, options={}) {
type: 'GET',
data: filters,
dataType: 'json',
contentType: 'application/json',
success: function(response) {
console.log('Success GET data at ' + url);
if (options.success) {
@ -62,8 +63,9 @@ function inventreeUpdate(url, data={}, options={}) {
},
url: url,
type: method,
data: data,
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/json',
success: function(response, status) {
response['_status_code'] = status;
console.log('UPDATE object to ' + url + ' - result = ' + status);

View File

@ -16,7 +16,7 @@ function moveStockItems(items, options) {
inventreeUpdate("/api/stock/move/",
{
location: location,
parts: parts
'parts[]': parts
},
{
success: function(response) {
@ -97,8 +97,67 @@ function countStockItems(items, options) {
return;
}
var tbl = "<table class='table table-striped table-condensed' id='stocktake-table'></table>";
openModal(modal);
modalSetTitle(modal, 'Stocktake');
modalSetTitle(modal, 'Stocktake ' + items.length + ' items');
$(modal).find('.modal-form-content').html(tbl);
$(modal).find('#stocktake-table').bootstrapTable({
data: items,
columns: [
{
checkbox: true,
},
{
field: 'part.name',
title: 'Part',
},
{
field: 'location.name',
title: 'Location',
},
{
field: 'quantity',
title: 'Quantity',
}
]
});
$(modal).on('click', '#modal-form-submit', function() {
var selections = $(modal).find('#stocktake-table').bootstrapTable('getSelections');
var stocktake = [];
console.log('Performing stocktake on ' + selections.length + ' items');
for (i = 0; i<selections.length; i++) {
var item = {
pk: selections[i].pk,
quantity: selections[i].quantity,
};
stocktake.push(item);
}
inventreeUpdate("/api/stock/stocktake/",
{
'items[]': stocktake,
},
{
success: function(response) {
closeModal(modal);
if (options.success) {
options.success();
}
},
error: function(error) {
alert(error);
},
method: 'post',
});
});
}
function deleteStockItems(items, options) {

View File

@ -59,12 +59,13 @@ class StockStocktake(APIView):
def post(self, request, *args, **kwargs):
data = request.data
if not 'items[]' in request.data:
raise ValidationError({'items[]:' 'Request must contain list of items'})
items = []
# Ensure each entry is valid
for entry in data:
for entry in request.data['items[]']:
if not 'pk' in entry:
raise ValidationError({'pk': 'Each entry must contain pk field'})
if not 'quantity' in entry:
@ -80,6 +81,9 @@ class StockStocktake(APIView):
except ValueError:
raise ValidationError({'quantity': 'Quantity must be an integer'})
if item['quantity'] < 0:
raise ValidationError({'quantity': 'Quantity must be >= 0'})
items.append(item)
for item in items:
@ -111,7 +115,7 @@ class StockMove(APIView):
if not u'parts[]' in data:
raise ValidationError({'parts[]': 'Parts list must be specified'})
part_list = data.getlist(u'parts[]')
part_list = data.get(u'parts[]')
parts = []