entity.py
changeset 10997 da712d3f0601
parent 10996 dc572d116731
child 11047 bfd11ffa79f7
equal deleted inserted replaced
10996:dc572d116731 10997:da712d3f0601
   516 
   516 
   517         You can also set relations where the entity has 'object' role by
   517         You can also set relations where the entity has 'object' role by
   518         prefixing the relation name by 'reverse_'. Also, relation values may be
   518         prefixing the relation name by 'reverse_'. Also, relation values may be
   519         an entity or eid, a list of entities or eids.
   519         an entity or eid, a list of entities or eids.
   520         """
   520         """
   521         rql, qargs, pendingrels, _attrcache = cls._cw_build_entity_query(kwargs)
   521         rql, qargs, pendingrels, attrcache = cls._cw_build_entity_query(kwargs)
   522         if rql:
   522         if rql:
   523             rql = 'INSERT %s X: %s' % (cls.__regid__, rql)
   523             rql = 'INSERT %s X: %s' % (cls.__regid__, rql)
   524         else:
   524         else:
   525             rql = 'INSERT %s X' % (cls.__regid__)
   525             rql = 'INSERT %s X' % (cls.__regid__)
   526         try:
   526         try:
   527             created = execute(rql, qargs).get_entity(0, 0)
   527             created = execute(rql, qargs).get_entity(0, 0)
   528         except IndexError:
   528         except IndexError:
   529             raise Exception('could not create a %r with %r (%r)' %
   529             raise Exception('could not create a %r with %r (%r)' %
   530                             (cls.__regid__, rql, qargs))
   530                             (cls.__regid__, rql, qargs))
       
   531         created._cw_update_attr_cache(attrcache)
   531         cls._cw_handle_pending_relations(created.eid, pendingrels, execute)
   532         cls._cw_handle_pending_relations(created.eid, pendingrels, execute)
   532         return created
   533         return created
   533 
   534 
   534     def __init__(self, req, rset=None, row=None, col=0):
   535     def __init__(self, req, rset=None, row=None, col=0):
   535         AppObject.__init__(self, req, rset=rset, row=row, col=col)
   536         AppObject.__init__(self, req, rset=rset, row=row, col=col)
   560         return super(Entity, self).__hash__()
   561         return super(Entity, self).__hash__()
   561 
   562 
   562     def _cw_update_attr_cache(self, attrcache):
   563     def _cw_update_attr_cache(self, attrcache):
   563         trdata = self._cw.transaction_data
   564         trdata = self._cw.transaction_data
   564         uncached_attrs = trdata.get('%s.storage-special-process-attrs' % self.eid, set())
   565         uncached_attrs = trdata.get('%s.storage-special-process-attrs' % self.eid, set())
       
   566         uncached_attrs.update(trdata.get('%s.dont-cache-attrs' % self.eid, set()))
   565         for attr in uncached_attrs:
   567         for attr in uncached_attrs:
   566             attrcache.pop(attr, None)
   568             attrcache.pop(attr, None)
   567             self.cw_attr_cache.pop(attr, None)
   569             self.cw_attr_cache.pop(attr, None)
   568         self.cw_attr_cache.update(attrcache)
   570         self.cw_attr_cache.update(attrcache)
   569 
   571 
   577         the content of the file at the specified path. We do not want
   579         the content of the file at the specified path. We do not want
   578         the "filepath" value to be cached.
   580         the "filepath" value to be cached.
   579 
   581 
   580         """
   582         """
   581         trdata = self._cw.transaction_data
   583         trdata = self._cw.transaction_data
   582         trdata.setdefault('%s.storage-special-process-attrs' % self.eid, set()).add(attr)
   584         trdata.setdefault('%s.dont-cache-attrs' % self.eid, set()).add(attr)
       
   585         if repo_side:
       
   586             trdata.setdefault('%s.storage-special-process-attrs' % self.eid, set()).add(attr)
   583 
   587 
   584     def __json_encode__(self):
   588     def __json_encode__(self):
   585         """custom json dumps hook to dump the entity's eid
   589         """custom json dumps hook to dump the entity's eid
   586         which is not part of dict structure itself
   590         which is not part of dict structure itself
   587         """
   591         """
   820             execute(rql, {'x': self.eid, 'y': ceid})
   824             execute(rql, {'x': self.eid, 'y': ceid})
   821             self.cw_clear_relation_cache(rschema.type, 'object')
   825             self.cw_clear_relation_cache(rschema.type, 'object')
   822 
   826 
   823     # data fetching methods ###################################################
   827     # data fetching methods ###################################################
   824 
   828 
       
   829     @cached
   825     def as_rset(self): # XXX .cw_as_rset
   830     def as_rset(self): # XXX .cw_as_rset
   826         """returns a resultset containing `self` information"""
   831         """returns a resultset containing `self` information"""
   827         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   832         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   828                          {'x': self.eid}, [(self.cw_etype,)])
   833                          {'x': self.eid}, [(self.cw_etype,)])
   829         rset.req = self._cw
   834         rset.req = self._cw
  1310             if ' WHERE ' in rql:
  1315             if ' WHERE ' in rql:
  1311                 rql += ', X eid %(x)s'
  1316                 rql += ', X eid %(x)s'
  1312             else:
  1317             else:
  1313                 rql += ' WHERE X eid %(x)s'
  1318                 rql += ' WHERE X eid %(x)s'
  1314             self._cw.execute(rql, qargs)
  1319             self._cw.execute(rql, qargs)
       
  1320         # update current local object _after_ the rql query to avoid
       
  1321         # interferences between the query execution itself and the cw_edited /
       
  1322         # skip_security machinery
       
  1323         self._cw_update_attr_cache(attrcache)
  1315         self._cw_handle_pending_relations(self.eid, pendingrels, self._cw.execute)
  1324         self._cw_handle_pending_relations(self.eid, pendingrels, self._cw.execute)
  1316         # XXX update relation cache
  1325         # XXX update relation cache
  1317 
  1326 
  1318     def cw_delete(self, **kwargs):
  1327     def cw_delete(self, **kwargs):
  1319         assert self.has_eid(), self.eid
  1328         assert self.has_eid(), self.eid