From aa6a50a6e7bbbfc7f29cb1f439c98a4955c932b0 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Fri, 24 Mar 2017 23:57:27 -0300 Subject: [PATCH] Simplify JS-Python interface for cancel_ops --- kfet/static/kfet/js/history.js | 15 +++++++++------ kfet/views.py | 12 ++++-------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/kfet/static/kfet/js/history.js b/kfet/static/kfet/js/history.js index 7a842822..e23d4c6a 100644 --- a/kfet/static/kfet/js/history.js +++ b/kfet/static/kfet/js/history.js @@ -59,11 +59,16 @@ class History { $(document).on('keydown', function(e) { if (e.keyCode == 46) { //DEL key ; we delete the selected operations (if any) - var to_cancel = []; + var to_cancel = {'transfers': [], 'opes': [],}; that._$container.find('.ope.ui-selected').each(function() { - to_cancel.push($(this).parent().attr('id')) ; + var [type, id] = $(this).parent().attr('id').split('-'); + + if (type === 'transfer') + to_cancel['transfers'].push(id); + else + to_cancel['opes'].push(id); }); - if (to_cancel.length > 0) + if (to_cancel['opes'].length > 0 || to_cancel['transfers'].length > 0) that.cancel_operations(to_cancel); } }); @@ -86,13 +91,11 @@ class History { var focus_next = undefined; } - var data = { 'operations': to_cancel }; - $.ajax({ dataType: "json", url : Urls['kfet.kpsul.cancel_operations'](), method : "POST", - data : data, + data : to_cancel, beforeSend: function ($xhr) { $xhr.setRequestHeader("X-CSRFToken", csrftoken); if (password != '') diff --git a/kfet/views.py b/kfet/views.py index 73edc4bd..c901bff7 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1134,14 +1134,10 @@ def kpsul_cancel_operations(request): # Checking if BAD REQUEST (opes_pk not int or not existing) try: # Set pour virer les doublons - opes_post = set(map(lambda s: int(s.split('-')[1]), - filter(lambda s: s.split('-')[0] == 'purchase' - or s.split('-')[0] == 'specialope', - request.POST.getlist('operations[]', [])))) - transfers_post = \ - set(map(lambda s: int(s.split('-')[1]), - filter(lambda s: s.split('-')[0] == 'transfer', - request.POST.getlist('operations[]', [])))) + opes_post = set(map(lambda s: int(s), + request.POST.getlist('opes[]', []))) + transfers_post = set(map(lambda s: int(s), + request.POST.getlist('transfers[]', []))) except ValueError: return JsonResponse(data, status=400)