Commit d776c85e authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update MyJsonEncoder to add the type lazyT.

parent c3cc6e27
......@@ -8,16 +8,22 @@ import json
from basesvc import BaseSvc
from gluon import current
from gluon.http import HTTP
from gluon.languages import lazyT
from gluon.restricted import RestrictedError
from helper import as_list
DBUI = 'Dbui'
PROC_KEY = '%s.%s'
DBUI = "Dbui"
PROC_KEY = "%s.%s"
class MyJsonEncoder(json.JSONEncoder):
"""Add date, datetime and time to the standard JSON encoder.
"""Add to the standard JSON encoder:
- date
- datetime
- lazyT
- time
def default(self, obj):
......@@ -25,7 +31,10 @@ class MyJsonEncoder(json.JSONEncoder):
if isinstance(obj, (,
return obj.isoformat().replace('T', ' ')
return obj.isoformat().replace("T", " ")
elif isinstance(obj, lazyT):
return str(obj)
return json.JSONEncoder.default(self, obj)
......@@ -47,7 +56,7 @@ class DirectSvc(BaseSvc):
def myfunction(a, b):
The same appraoch can be used for methods.
The same approach can be used for methods.
On the client-side:
......@@ -92,14 +101,14 @@ class DirectSvc(BaseSvc):
or when the function / method crashed.
myrequest = current.request
# no arguments in a Json-encoded raw HTPP request
# no arguments in a JSON-encoded raw HTPP request
if len(myrequest.args) == 0:
return self.route()
self.error(404, 'Object does not exist.')
self.error(404, "Object does not exist.")
def echo(self, x):
"""Return the input arguments to run echo tests.
......@@ -141,7 +150,7 @@ class DirectSvc(BaseSvc):
# The action is equal to Dbui for a function
# and to the name of the class for a method
action = DBUI
if 'im_class' in dir(f):
if "im_class" in dir(f):
action = f.im_class.__name__
k = PROC_KEY % (action, f.__name__)
......@@ -165,7 +174,7 @@ class DirectSvc(BaseSvc):
in the web2py ticket system.
self.dbg('Start directSvc.route')
self.dbg("Start directSvc.route")
# decode transactions, a list of Json-encoded raw HTPP requests:
# {action: "x", method: "y", data: [], type:"rpc", tid:z}
......@@ -177,26 +186,26 @@ class DirectSvc(BaseSvc):
li = []
for arg in transactions:
di = dict(arg)
self.dbg('transaction:', di)
self.dbg("transaction:", di)
k = PROC_KEY % (arg['action'], arg['method'])
if arg['data']:
di['result'] = self.procedures[k](*arg['data'])
k = PROC_KEY % (arg["action"], arg["method"])
if arg["data"]:
di["result"] = self.procedures[k](*arg["data"])
di['result'] = self.procedures[k]()
di["result"] = self.procedures[k]()
# catch exceptions and log them in the ticker system
# inform the user that something went wrong in the server
r = RestrictedError(layer='',
r = RestrictedError(layer="",
self.error(500, 'Internal server error.')
self.error(500, "Internal server error.")
del di['data']
del di["data"]
self.dbg('End', li)
self.dbg("End", li)
return json.dumps(li, cls=MyJsonEncoder)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment