From bc4e62cccdbc1005e272d47593a3141a766c7e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Fri, 20 Dec 2019 23:10:12 +0100 Subject: [PATCH 1/3] Fix get_include_methods function from jsonapi When include_methods param is used in a read call with a single parameter (instead of a list of more than one), the jsonapi does not handle the value parameter properly and transforms it to a list of characters instead. A call like follows: ``` this.ajax_submit({ url: this.get_portal_url() + "/@@API/read", data: { catalog_name: "uid_catalog", UID: widget.attr('data-uid'), include_methods: ["getSomething"], } }).done(function(data) { return deferred.resolveWith(this, [data.objects[0]["getSomething"]]); }); ``` fails. The function `get_include_methods` returns `getSomething` instead of a list `[getSomething]`. Therefore, the function `load_method_values` tries the following calls to the instance: `g`, `e`, `t`, `S`, `o`, ... If the call is like follows: ``` this.ajax_submit({ url: this.get_portal_url() + "/@@API/read", data: { catalog_name: "uid_catalog", UID: widget.attr('data-uid'), include_methods: ["getSomething", "getId"], } }).done(function(data) { return deferred.resolveWith(this, [data.objects[0]["getSomething"]]); }); ``` the list is resolved properly. --- bika/lims/jsonapi/__init__.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bika/lims/jsonapi/__init__.py b/bika/lims/jsonapi/__init__.py index a702cc2f53..5a4fecf02f 100644 --- a/bika/lims/jsonapi/__init__.py +++ b/bika/lims/jsonapi/__init__.py @@ -25,6 +25,7 @@ import json import Missing +import six import sys, traceback @@ -129,9 +130,15 @@ def load_field_values(instance, include_fields): def get_include_methods(request): """Retrieve include_methods values from the request """ - methods = request.get("include_methods", "") - include_methods = [ - x.strip() for x in methods.split(",") if x.strip()] + include_methods = [] + if "include_methods" in request: + methods = request.get("include_methods", "") + include_methods = [ + x.strip() for x in methods.split(",") if x.strip()] + if "include_methods[]" in request: + include_methods = request["include_methods[]"] + if isinstance(include_methods, six.string_types): + include_methods = [include_methods] return include_methods From 6508a996a5a01de9df43174ad01c8726af37dcf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Fri, 20 Dec 2019 23:31:52 +0100 Subject: [PATCH 2/3] Now we are here, make it a bit better --- bika/lims/jsonapi/__init__.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/bika/lims/jsonapi/__init__.py b/bika/lims/jsonapi/__init__.py index 5a4fecf02f..28a3744e5d 100644 --- a/bika/lims/jsonapi/__init__.py +++ b/bika/lims/jsonapi/__init__.py @@ -130,16 +130,15 @@ def load_field_values(instance, include_fields): def get_include_methods(request): """Retrieve include_methods values from the request """ - include_methods = [] - if "include_methods" in request: - methods = request.get("include_methods", "") - include_methods = [ - x.strip() for x in methods.split(",") if x.strip()] - if "include_methods[]" in request: - include_methods = request["include_methods[]"] - if isinstance(include_methods, six.string_types): - include_methods = [include_methods] - return include_methods + include_methods = request.get("include_methods[]") + if not include_methods: + include_methods = request.get("include_methods", []) + + if isinstance(include_methods, six.string_types): + include_methods = include_methods.split(",") + include_methods = map(lambda me: me.strip(), include_methods) + + return filter(None, include_methods) def load_method_values(instance, include_methods): From 5a40e59f24fe61ffc4e40a8d61f89d5c31ca9247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Puiggen=C3=A9?= Date: Fri, 20 Dec 2019 23:32:56 +0100 Subject: [PATCH 3/3] Changelog --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 9046383d78..a955510fa6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -28,6 +28,7 @@ Changelog **Fixed** +- #1493 jsonapi.read omits `include_methods` when a single parameter is used - #1477 Sample edit form - some selection widgets empty - #1478 Clients default CC E-Mails missing in Add Sample - #1479 Fixed too many redirects error: Labclerks viewing verified worksheets