entity.py
changeset 7216 6053bf221fa9
parent 7206 2723c52a0795
parent 7213 7644e68c4e9f
child 7223 7a58581d842c
equal deleted inserted replaced
7211:cacff15f847d 7216:6053bf221fa9
   252 
   252 
   253         Example (in a shell session):
   253         Example (in a shell session):
   254 
   254 
   255         >>> companycls = vreg['etypes'].etype_class(('Company')
   255         >>> companycls = vreg['etypes'].etype_class(('Company')
   256         >>> personcls = vreg['etypes'].etype_class(('Person')
   256         >>> personcls = vreg['etypes'].etype_class(('Person')
   257         >>> c = companycls.cw_instantiate(req.execute, name=u'Logilab')
   257         >>> c = companycls.cw_instantiate(session.execute, name=u'Logilab')
   258         >>> personcls.cw_instantiate(req.execute, firstname=u'John', lastname=u'Doe',
   258         >>> p = personcls.cw_instantiate(session.execute, firstname=u'John', lastname=u'Doe',
   259         ...                          works_for=c)
   259         ...                              works_for=c)
   260 
   260 
       
   261         You can also set relation where the entity has 'object' role by
       
   262         prefixing the relation by 'reverse_'.
   261         """
   263         """
   262         rql = 'INSERT %s X' % cls.__regid__
   264         rql = 'INSERT %s X' % cls.__regid__
   263         relations = []
   265         relations = []
   264         restrictions = set()
   266         restrictions = set()
   265         pending_relations = []
   267         pending_relations = []
   274             rschema = eschema.subjrels[attr] if role == 'subject' else eschema.objrels[attr]
   276             rschema = eschema.subjrels[attr] if role == 'subject' else eschema.objrels[attr]
   275             if not rschema.final and isinstance(value, (tuple, list, set, frozenset)):
   277             if not rschema.final and isinstance(value, (tuple, list, set, frozenset)):
   276                 if len(value) == 1:
   278                 if len(value) == 1:
   277                     value = iter(value).next()
   279                     value = iter(value).next()
   278                 else:
   280                 else:
       
   281                     # prepare IN clause
   279                     del kwargs[attr]
   282                     del kwargs[attr]
   280                     pending_relations.append( (attr, value) )
   283                     pending_relations.append( (attr, value) )
   281                     continue
   284                     continue
   282             if hasattr(value, 'eid'): # non final relation
   285             if hasattr(value, 'eid'): # non final relation
   283                 rvar = attr.upper()
   286                 rvar = attr.upper()
   284                 # XXX safer detection of object relation
   287                 if role == 'object':
   285                 if attr.startswith('reverse_'):
   288                     relations.append('%s %s X' % (rvar, attr))
   286                     relations.append('%s %s X' % (rvar, attr[len('reverse_'):]))
       
   287                 else:
   289                 else:
   288                     relations.append('X %s %s' % (attr, rvar))
   290                     relations.append('X %s %s' % (attr, rvar))
   289                 restriction = '%s eid %%(%s)s' % (rvar, attr)
   291                 restriction = '%s eid %%(%s)s' % (rvar, attr)
   290                 if not restriction in restrictions:
   292                 if not restriction in restrictions:
   291                     restrictions.add(restriction)
   293                     restrictions.add(restriction)
   939     def set_attributes(self, **kwargs): # XXX cw_set_attributes
   941     def set_attributes(self, **kwargs): # XXX cw_set_attributes
   940         _check_cw_unsafe(kwargs)
   942         _check_cw_unsafe(kwargs)
   941         assert kwargs
   943         assert kwargs
   942         assert self.cw_is_saved(), "should not call set_attributes while entity "\
   944         assert self.cw_is_saved(), "should not call set_attributes while entity "\
   943                "hasn't been saved yet"
   945                "hasn't been saved yet"
   944         relations = []
   946         relations = ['X %s %%(%s)s' % (key, key) for key in kwargs]
   945         for key in kwargs:
       
   946             relations.append('X %s %%(%s)s' % (key, key))
       
   947         # and now update the database
   947         # and now update the database
   948         kwargs['x'] = self.eid
   948         kwargs['x'] = self.eid
   949         self._cw.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations),
   949         self._cw.execute('SET %s WHERE X eid %%(x)s' % ','.join(relations),
   950                          kwargs)
   950                          kwargs)
   951         kwargs.pop('x')
   951         kwargs.pop('x')