new create_entity method, temporarily on base request (later on cw entity manager or as factory method) 3.5
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 20 Aug 2009 17:33:05 +0200
branch3.5
changeset 2919 662f35236d1c
parent 2918 452b4c9ee61d
child 2920 64322aa83a1d
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
__init__.py
--- a/__init__.py	Wed Aug 19 11:48:55 2009 +0200
+++ b/__init__.py	Thu Aug 20 17:33:05 2009 +0200
@@ -121,6 +121,27 @@
         raise KeyError
     def set_entity_cache(self, entity):
         pass
+
+    def create_entity(self, etype, *args, **kwargs):
+        """add a new entity of the given type"""
+        rql = 'INSERT %s X' % etype
+        relations = []
+        restrictions = []
+        cachekey = []
+        for rtype, rvar in args:
+            relations.append('X %s %s' % (rtype, rvar))
+            restrictions.append('%s eid %%(%s)s' % (rvar, rvar))
+            cachekey.append(rvar)
+        for attr in kwargs:
+            if attr in cachekey:
+                continue
+            relations.append('X %s %%(%s)s' % (attr, attr))
+        if relations:
+            rql = '%s: %s' % (rql, ', '.join(relations))
+        if restrictions:
+            rql = '%s WHERE %s' % (rql, ', '.join(restrictions))
+        return self.execute(rql, kwargs, cachekey).get_entity(0, 0)
+
     # url generation methods ##################################################
 
     def build_url(self, *args, **kwargs):