entity.py
branchstable
changeset 9013 b4bcabf55e77
parent 8992 55a1567d92a0
child 9188 0677e03077fb
equal deleted inserted replaced
9012:2cf127d4f5fd 9013:b4bcabf55e77
    24 
    24 
    25 from logilab.common import interface
    25 from logilab.common import interface
    26 from logilab.common.decorators import cached
    26 from logilab.common.decorators import cached
    27 from logilab.common.deprecation import deprecated
    27 from logilab.common.deprecation import deprecated
    28 from logilab.common.registry import yes
    28 from logilab.common.registry import yes
    29 from logilab.mtconverter import TransformData, TransformError, xml_escape
    29 from logilab.mtconverter import TransformData, xml_escape
    30 
    30 
    31 from rql.utils import rqlvar_maker
    31 from rql.utils import rqlvar_maker
    32 from rql.stmts import Select
    32 from rql.stmts import Select
    33 from rql.nodes import (Not, VariableRef, Constant, make_relation,
    33 from rql.nodes import (Not, VariableRef, Constant, make_relation,
    34                        Relation as RqlRelation)
    34                        Relation as RqlRelation)
    35 
    35 
    36 from cubicweb import Unauthorized, typed_eid, neg_role
    36 from cubicweb import Unauthorized, neg_role
    37 from cubicweb.utils import support_args
    37 from cubicweb.utils import support_args
    38 from cubicweb.rset import ResultSet
    38 from cubicweb.rset import ResultSet
    39 from cubicweb.appobject import AppObject
    39 from cubicweb.appobject import AppObject
    40 from cubicweb.req import _check_cw_unsafe
       
    41 from cubicweb.schema import (RQLVocabularyConstraint, RQLConstraint,
    40 from cubicweb.schema import (RQLVocabularyConstraint, RQLConstraint,
    42                              GeneratedConstraint)
    41                              GeneratedConstraint)
    43 from cubicweb.rqlrewrite import RQLRewriter
    42 from cubicweb.rqlrewrite import RQLRewriter
    44 
    43 
    45 from cubicweb.uilib import soup2xhtml
    44 from cubicweb.uilib import soup2xhtml
   553 
   552 
   554     def __repr__(self):
   553     def __repr__(self):
   555         return '<Entity %s %s %s at %s>' % (
   554         return '<Entity %s %s %s at %s>' % (
   556             self.e_schema, self.eid, list(self.cw_attr_cache), id(self))
   555             self.e_schema, self.eid, list(self.cw_attr_cache), id(self))
   557 
   556 
   558     def __cmp__(self, other):
   557     def __lt__(self, other):
       
   558         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
       
   559 
       
   560     def __eq__(self, other):
   559         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
   561         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
   560 
   562 
   561     def _cw_update_attr_cache(self, attrcache):
   563     def _cw_update_attr_cache(self, attrcache):
   562         # if context is a repository session, don't consider dont-cache-attrs as
   564         # if context is a repository session, don't consider dont-cache-attrs as
   563         # the instance already hold modified values and loosing them could
   565         # the instance already hold modified values and loosing them could
   625     def has_eid(self): # XXX cw_has_eid
   627     def has_eid(self): # XXX cw_has_eid
   626         """return True if the entity has an attributed eid (False
   628         """return True if the entity has an attributed eid (False
   627         meaning that the entity has to be created
   629         meaning that the entity has to be created
   628         """
   630         """
   629         try:
   631         try:
   630             typed_eid(self.eid)
   632             int(self.eid)
   631             return True
   633             return True
   632         except (ValueError, TypeError):
   634         except (ValueError, TypeError):
   633             return False
   635             return False
   634 
   636 
   635     def cw_is_saved(self):
   637     def cw_is_saved(self):
   791         execute = self._cw.execute
   793         execute = self._cw.execute
   792         skip_copy_for = {'subject': set(), 'object': set()}
   794         skip_copy_for = {'subject': set(), 'object': set()}
   793         for rtype in self.skip_copy_for:
   795         for rtype in self.skip_copy_for:
   794             skip_copy_for['subject'].add(rtype)
   796             skip_copy_for['subject'].add(rtype)
   795             warn('[3.14] skip_copy_for on entity classes (%s) is deprecated, '
   797             warn('[3.14] skip_copy_for on entity classes (%s) is deprecated, '
   796                  'use cw_skip_for instead with list of couples (rtype, role)' % self.__regid__,
   798                  'use cw_skip_for instead with list of couples (rtype, role)' % self.cw_etype,
   797                  DeprecationWarning)
   799                  DeprecationWarning)
   798         for rtype, role in self.cw_skip_copy_for:
   800         for rtype, role in self.cw_skip_copy_for:
   799             assert role in ('subject', 'object'), role
   801             assert role in ('subject', 'object'), role
   800             skip_copy_for[role].add(rtype)
   802             skip_copy_for[role].add(rtype)
   801         for rschema in self.e_schema.subject_relations():
   803         for rschema in self.e_schema.subject_relations():
   843 
   845 
   844     @cached
   846     @cached
   845     def as_rset(self): # XXX .cw_as_rset
   847     def as_rset(self): # XXX .cw_as_rset
   846         """returns a resultset containing `self` information"""
   848         """returns a resultset containing `self` information"""
   847         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   849         rset = ResultSet([(self.eid,)], 'Any X WHERE X eid %(x)s',
   848                          {'x': self.eid}, [(self.__regid__,)])
   850                          {'x': self.eid}, [(self.cw_etype,)])
   849         rset.req = self._cw
   851         rset.req = self._cw
   850         return rset
   852         return rset
   851 
   853 
   852     def _cw_to_complete_relations(self):
   854     def _cw_to_complete_relations(self):
   853         """by default complete final relations to when calling .complete()"""
   855         """by default complete final relations to when calling .complete()"""
  1285         You can also set relations where the entity has 'object' role by
  1287         You can also set relations where the entity has 'object' role by
  1286         prefixing the relation name by 'reverse_'.  Also, relation values may be
  1288         prefixing the relation name by 'reverse_'.  Also, relation values may be
  1287         an entity or eid, a list of entities or eids, or None (meaning that all
  1289         an entity or eid, a list of entities or eids, or None (meaning that all
  1288         relations of the given type from or to this object should be deleted).
  1290         relations of the given type from or to this object should be deleted).
  1289         """
  1291         """
  1290         _check_cw_unsafe(kwargs)
       
  1291         assert kwargs
  1292         assert kwargs
  1292         assert self.cw_is_saved(), "should not call set_attributes while entity "\
  1293         assert self.cw_is_saved(), "should not call set_attributes while entity "\
  1293                "hasn't been saved yet"
  1294                "hasn't been saved yet"
  1294         rql, qargs, pendingrels, attrcache = self._cw_build_entity_query(kwargs)
  1295         rql, qargs, pendingrels, attrcache = self._cw_build_entity_query(kwargs)
  1295         if rql:
  1296         if rql:
  1395     def __iter__(self):
  1396     def __iter__(self):
  1396         return iter(self.cw_attr_cache)
  1397         return iter(self.cw_attr_cache)
  1397 
  1398 
  1398     @deprecated('[3.10] use entity.cw_attr_cache[attr]')
  1399     @deprecated('[3.10] use entity.cw_attr_cache[attr]')
  1399     def __getitem__(self, key):
  1400     def __getitem__(self, key):
  1400         if key == 'eid':
       
  1401             warn('[3.7] entity["eid"] is deprecated, use entity.eid instead',
       
  1402                  DeprecationWarning, stacklevel=2)
       
  1403             return self.eid
       
  1404         return self.cw_attr_cache[key]
  1401         return self.cw_attr_cache[key]
  1405 
  1402 
  1406     @deprecated('[3.10] use entity.cw_attr_cache.get(attr[, default])')
  1403     @deprecated('[3.10] use entity.cw_attr_cache.get(attr[, default])')
  1407     def get(self, key, default=None):
  1404     def get(self, key, default=None):
  1408         return self.cw_attr_cache.get(key, default)
  1405         return self.cw_attr_cache.get(key, default)
  1422 
  1419 
  1423         and this way, cw_edited will be updated accordingly. Also, add
  1420         and this way, cw_edited will be updated accordingly. Also, add
  1424         the attribute to skip_security since we don't want to check security
  1421         the attribute to skip_security since we don't want to check security
  1425         for such attributes set by hooks.
  1422         for such attributes set by hooks.
  1426         """
  1423         """
  1427         if attr == 'eid':
  1424         try:
  1428             warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead',
  1425             self.cw_edited[attr] = value
  1429                  DeprecationWarning, stacklevel=2)
  1426         except AttributeError:
  1430             self.eid = value
  1427             self.cw_attr_cache[attr] = value
  1431         else:
       
  1432             try:
       
  1433                 self.cw_edited[attr] = value
       
  1434             except AttributeError:
       
  1435                 self.cw_attr_cache[attr] = value
       
  1436 
  1428 
  1437     @deprecated('[3.10] use del entity.cw_edited[attr]')
  1429     @deprecated('[3.10] use del entity.cw_edited[attr]')
  1438     def __delitem__(self, attr):
  1430     def __delitem__(self, attr):
  1439         """override __delitem__ to update self.cw_edited on cleanup of
  1431         """override __delitem__ to update self.cw_edited on cleanup of
  1440         undesired changes introduced in the entity's dict. For example, see the
  1432         undesired changes introduced in the entity's dict. For example, see the