support _cw_unsafe as set_attributes/set_relations stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 15 Oct 2009 15:27:25 +0200
branchstable
changeset 3684 c0a854810942
parent 3683 2e4794c97cf4
child 3685 6f807804f1d0
support _cw_unsafe as set_attributes/set_relations
__init__.py
--- a/__init__.py	Thu Oct 15 12:10:48 2009 +0200
+++ b/__init__.py	Thu Oct 15 15:27:25 2009 +0200
@@ -123,15 +123,19 @@
     def set_entity_cache(self, entity):
         pass
 
-    def create_entity(self, etype, **kwargs):
+    def create_entity(self, etype, _cw_unsafe=False, **kwargs):
         """add a new entity of the given type
-        
+
         Example (in a shell session):
 
         c = create_entity('Company', name=u'Logilab')
         create_entity('Person', works_for=c, firstname=u'John', lastname=u'Doe')
 
         """
+        if _cw_unsafe:
+            execute = self.unsafe_execute
+        else:
+            execute = self.execute
         rql = 'INSERT %s X' % etype
         relations = []
         restrictions = set()
@@ -162,13 +166,13 @@
             rql = '%s: %s' % (rql, ', '.join(relations))
         if restrictions:
             rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
-        created = self.execute(rql, kwargs, cachekey).get_entity(0, 0)
+        created = execute(rql, kwargs, cachekey).get_entity(0, 0)
         for attr, values in pending_relations:
             if attr.startswith('reverse_'):
                 restr = 'Y %s X' % attr[len('reverse_'):]
             else:
                 restr = 'X %s Y' % attr
-            self.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
+            execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
                 restr, ','.join(str(r.eid) for r in values)),
                          {'x': created.eid}, 'x')
         return created