# HG changeset patch # User Pierre-Yves David # Date 1334147779 -7200 # Node ID fc59d2380c48e140764a01011e906b64e4481fed # Parent 6599f69bb846dabd0083cf0e35ed291e68af25e2 [service-api] Add unified service API on `_cw` attribute This will allow writting generic entity code that could run on both client and repo side. This addition have been discussed with Sylvain Sylvain Thenault. diff -r 6599f69bb846 -r fc59d2380c48 dbapi.py --- a/dbapi.py Wed Apr 11 14:43:51 2012 +0200 +++ b/dbapi.py Wed Apr 11 14:36:19 2012 +0200 @@ -345,6 +345,11 @@ self.pgettext = lambda x, y: unicode(y) self.debug('request default language: %s', self.lang) + # server-side service call ################################################# + + def call_service(self, regid, async=False, **kwargs): + return self.cnx.call_service(regid, async, **kwargs) + # entities cache management ############################################### def entity_cache(self, eid): diff -r 6599f69bb846 -r fc59d2380c48 server/session.py --- a/server/session.py Wed Apr 11 14:43:51 2012 +0200 +++ b/server/session.py Wed Apr 11 14:36:19 2012 +0200 @@ -845,6 +845,12 @@ else: self.data[key] = value + # server-side service call ################################################# + + def call_service(self, regid, async=False, **kwargs): + return self.repo.call_service(self.id, regid, async, **kwargs) + + # request interface ####################################################### @property diff -r 6599f69bb846 -r fc59d2380c48 test/unittest_dbapi.py --- a/test/unittest_dbapi.py Wed Apr 11 14:43:51 2012 +0200 +++ b/test/unittest_dbapi.py Wed Apr 11 14:36:19 2012 +0200 @@ -84,16 +84,17 @@ req.user.cw_adapt_to('IBreadCrumbs') # don't crash def test_call_service(self): - req = self.request() ServiceClass = self.vreg['services']['test_service'][0] - ret_value = req.cnx.call_service('test_service', msg='coucou') - self.assertEqual('coucou', ServiceClass.passed_here.pop()) - self.assertEqual('babar', ret_value) + for _cw in (self.request(), self.session): + ret_value = _cw.call_service('test_service', msg='coucou') + self.assertEqual('coucou', ServiceClass.passed_here.pop()) + self.assertEqual('babar', ret_value) with self.login('anon') as ctm: - with self.assertRaises(NoSelectableObject): - self.request().cnx.call_service('test_service', msg='toto') - self.rollback() - self.assertEqual([], ServiceClass.passed_here) + for _cw in (self.request(), self.session): + with self.assertRaises(NoSelectableObject): + _cw.call_service('test_service', msg='toto') + self.rollback() + self.assertEqual([], ServiceClass.passed_here) if __name__ == '__main__':