|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of SENAITE.HEALTH. |
|
4
|
|
|
# |
|
5
|
|
|
# SENAITE.HEALTH is free software: you can redistribute it and/or modify it |
|
6
|
|
|
# under the terms of the GNU General Public License as published by the Free |
|
7
|
|
|
# Software Foundation, version 2. |
|
8
|
|
|
# |
|
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
12
|
|
|
# details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
|
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
|
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17
|
|
|
# |
|
18
|
|
|
# Copyright 2018-2019 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
from bika.lims.browser import BrowserView |
|
22
|
|
|
from bika.lims import bikaMessageFactory as _ |
|
23
|
|
|
import plone |
|
24
|
|
|
import sys |
|
25
|
|
|
import json |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class AjaxHandler(BrowserView): |
|
29
|
|
|
""" Handler for standarized ajax calls to bika.health |
|
30
|
|
|
""" |
|
31
|
|
|
|
|
32
|
|
|
def __call__(self): |
|
33
|
|
|
""" Method invocation: |
|
34
|
|
|
A remote method is invoked by sending a request to a remote service. |
|
35
|
|
|
The request must be a single object serialized using JSON. |
|
36
|
|
|
It has three properties: |
|
37
|
|
|
- service: A String containing the name of the method to be invoked. |
|
38
|
|
|
- params: A dict of objects to pass as arguments to the method. |
|
39
|
|
|
- id: The request id. This can be of any type. It is used to match |
|
40
|
|
|
the response with the request that it is replying to. |
|
41
|
|
|
Response: |
|
42
|
|
|
When the method invocation completes, the service replies with a |
|
43
|
|
|
response. The response is a single object serialized using JSON. |
|
44
|
|
|
It has three properties: |
|
45
|
|
|
- result: The Object returned by the invoked method. Null in case |
|
46
|
|
|
there was an error invoking the method. |
|
47
|
|
|
- error: An Error object if there was an error invoking the method. |
|
48
|
|
|
Null if there was no error. |
|
49
|
|
|
- id: The same id as the request it is responding to. |
|
50
|
|
|
""" |
|
51
|
|
|
plone.protect.CheckAuthenticator(self.request) |
|
52
|
|
|
method = self.request.get('service', '') |
|
53
|
|
|
callid = self.request.get('id') |
|
54
|
|
|
params = self.request.get('params', '{}') |
|
55
|
|
|
params = json.loads(params) |
|
56
|
|
|
|
|
57
|
|
|
error = None |
|
58
|
|
|
result = None |
|
59
|
|
|
if not callid: |
|
60
|
|
|
error = _("No request id defined") |
|
61
|
|
|
|
|
62
|
|
|
if not method: |
|
63
|
|
|
error = _("Service not defined") |
|
64
|
|
|
else: |
|
65
|
|
|
if not hasattr(self, method): |
|
66
|
|
|
error = _("Service '%s' not found for %s") \ |
|
67
|
|
|
% (method, self.__class__.__name__) |
|
68
|
|
|
else: |
|
69
|
|
|
try: |
|
70
|
|
|
# Call method |
|
71
|
|
|
result, error = getattr(self, method)(params) |
|
72
|
|
|
except: |
|
73
|
|
|
exc_value = sys.exc_info()[1] |
|
74
|
|
|
error = _("Failed ajax call: %s") % exc_value |
|
75
|
|
|
result = None |
|
76
|
|
|
|
|
77
|
|
|
response = {'result': result, 'error': error, 'id': callid} |
|
78
|
|
|
return json.dumps(response) |
|
79
|
|
|
|