entity.py
changeset 7314 d3104c697adb
parent 7304 66fa1b7ac784
parent 7308 ff8d6ae076c4
child 7377 d8083b2ae4d6
equal deleted inserted replaced
7309:22403d5c3da4 7314:d3104c697adb
   956 
   956 
   957     def set_relations(self, **kwargs): # XXX cw_set_relations
   957     def set_relations(self, **kwargs): # XXX cw_set_relations
   958         """add relations to the given object. To set a relation where this entity
   958         """add relations to the given object. To set a relation where this entity
   959         is the object of the relation, use 'reverse_'<relation> as argument name.
   959         is the object of the relation, use 'reverse_'<relation> as argument name.
   960 
   960 
   961         Values may be an entity, a list of entities, or None (meaning that all
   961         Values may be an entity or eid, a list of entities or eids, or None
   962         relations of the given type from or to this object should be deleted).
   962         (meaning that all relations of the given type from or to this object
       
   963         should be deleted).
   963         """
   964         """
   964         # XXX update cache
   965         # XXX update cache
   965         _check_cw_unsafe(kwargs)
   966         _check_cw_unsafe(kwargs)
   966         for attr, values in kwargs.iteritems():
   967         for attr, values in kwargs.iteritems():
   967             if attr.startswith('reverse_'):
   968             if attr.startswith('reverse_'):
   972                 self._cw.execute('DELETE %s WHERE X eid %%(x)s' % restr,
   973                 self._cw.execute('DELETE %s WHERE X eid %%(x)s' % restr,
   973                                  {'x': self.eid})
   974                                  {'x': self.eid})
   974                 continue
   975                 continue
   975             if not isinstance(values, (tuple, list, set, frozenset)):
   976             if not isinstance(values, (tuple, list, set, frozenset)):
   976                 values = (values,)
   977                 values = (values,)
       
   978             eids = []
       
   979             for val in values:
       
   980                 try:
       
   981                     eids.append(str(val.eid))
       
   982                 except AttributeError:
       
   983                     try:
       
   984                         eids.append(str(typed_eid(val)))
       
   985                     except (ValueError, TypeError):
       
   986                         raise Exception('expected an Entity or eid, got %s' % val)
   977             self._cw.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
   987             self._cw.execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % (
   978                 restr, ','.join(str(r.eid) for r in values)),
   988                     restr, ','.join(eids)), {'x': self.eid})
   979                              {'x': self.eid})
       
   980 
   989 
   981     def cw_delete(self, **kwargs):
   990     def cw_delete(self, **kwargs):
   982         assert self.has_eid(), self.eid
   991         assert self.has_eid(), self.eid
   983         self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema,
   992         self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema,
   984                          {'x': self.eid}, **kwargs)
   993                          {'x': self.eid}, **kwargs)