Add decorator on RPC

This commit is contained in:
Cédric Krier committed 2023-08-16 10:39:16 +02:00
1 parent fe66322ab1
commit 9aa4a8583b
4 files changed
+19 -5

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Add decorator on RPC
* Remove translations with empty source
* Run doc tests with different globals
* Create user applications as validated from user preferences
+5 -1
View File
@@ -4,7 +4,7 @@
RPC
===
.. class:: RPC([readonly[, instantiate[, result[, check_access[, unique[, fresh_session[, cache]]]]]]])
.. class:: RPC([readonly[, instantiate[, [decorator, result[, check_access[, unique[, fresh_session[, cache]]]]]]]])
Define the behavior of Remote Procedure Call.
@@ -18,6 +18,10 @@ Instance attributes are:
The position or the slice of the argument to be instanciated
.. attribute:: RPC.decorator
The function to decorate the called procedure with
.. attribute:: RPC.result
The function to transform the result
+1 -1
View File
@@ -190,7 +190,7 @@ def _dispatch(request, pool, *args, **kwargs):
c_args, c_kwargs, transaction.context, transaction.timestamp \
= rpc.convert(obj, *args, **kwargs)
transaction.context['_request'] = request.context
meth = getattr(obj, method)
meth = rpc.decorate(getattr(obj, method))
if (rpc.instantiate is None
or not is_instance_method(obj, method)):
result = rpc.result(meth(*c_args, **c_kwargs))
+12 -3
View File
@@ -12,19 +12,23 @@ class RPC(object):
readonly: The transaction mode
instantiate: The position or the slice of the arguments to be instanciated
decorator: A function to decorate the procedure with
result: The function to transform the result
check_access: If access right must be checked
fresh_session: If a fresh session is required
unique: Check instances are unique
'''
__slots__ = ('readonly', 'instantiate', 'result', 'check_access',
'fresh_session', 'unique', 'cache')
__slots__ = (
'readonly', 'instantiate', 'decorator', 'result',
'check_access', 'fresh_session', 'unique', 'cache')
def __init__(self, readonly=True, instantiate=None, result=None,
def __init__(
self, readonly=True, instantiate=None, decorator=None, result=None,
check_access=True, fresh_session=False, unique=True, cache=None):
self.readonly = readonly
self.instantiate = instantiate
self.decorator = decorator
if result is None:
def result(r):
return r
@@ -83,6 +87,11 @@ class RPC(object):
context['_check_access'] = True
return args, kwargs, context, timestamp
def decorate(self, func):
if self.decorator:
func = self.decorator(func)
return func
class RPCCache:
__slots__ = ('duration',)