[service-api] Add unified service API on `_cw` attribute
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Wed, 11 Apr 2012 14:36:19 +0200
changeset 8367 fc59d2380c48
parent 8366 6599f69bb846
child 8368 062c9c7fe502
[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.
dbapi.py
server/session.py
test/unittest_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):
--- 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
--- 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__':