entity.py
changeset 4970 1f3d8946ea84
parent 4965 04543ed0bbdc
child 4988 d85f639e9150
equal deleted inserted replaced
4967:236f1fde6dd0 4970:1f3d8946ea84
   223         return id(self)
   223         return id(self)
   224 
   224 
   225     def __cmp__(self, other):
   225     def __cmp__(self, other):
   226         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
   226         raise NotImplementedError('comparison not implemented for %s' % self.__class__)
   227 
   227 
   228     def __setitem__(self, attr, value):
       
   229         """override __setitem__ to update self.edited_attributes.
       
   230 
       
   231         Typically, a before_update_hook could do::
       
   232 
       
   233             entity['generated_attr'] = generated_value
       
   234 
       
   235         and this way, edited_attributes will be updated accordingly
       
   236         """
       
   237         if attr == 'eid':
       
   238             warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead',
       
   239                  DeprecationWarning, stacklevel=2)
       
   240             self.eid = value
       
   241         else:
       
   242             super(Entity, self).__setitem__(attr, value)
       
   243             if hasattr(self, 'edited_attributes'):
       
   244                 self.edited_attributes.add(attr)
       
   245 
       
   246     def __getitem__(self, key):
   228     def __getitem__(self, key):
   247         if key == 'eid':
   229         if key == 'eid':
   248             warn('[3.7] entity["eid"] is deprecated, use entity.eid instead',
   230             warn('[3.7] entity["eid"] is deprecated, use entity.eid instead',
   249                  DeprecationWarning, stacklevel=2)
   231                  DeprecationWarning, stacklevel=2)
   250             return self.eid
   232             return self.eid
   251         return super(Entity, self).__getitem__(key)
   233         return super(Entity, self).__getitem__(key)
   252 
   234 
   253     def setdefault(self, key, default):
   235     def __setitem__(self, attr, value):
       
   236         """override __setitem__ to update self.edited_attributes.
       
   237 
       
   238         Typically, a before_update_hook could do::
       
   239 
       
   240             entity['generated_attr'] = generated_value
       
   241 
       
   242         and this way, edited_attributes will be updated accordingly
       
   243         """
       
   244         if attr == 'eid':
       
   245             warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead',
       
   246                  DeprecationWarning, stacklevel=2)
       
   247             self.eid = value
       
   248         else:
       
   249             super(Entity, self).__setitem__(attr, value)
       
   250             if hasattr(self, 'edited_attributes'):
       
   251                 self.edited_attributes.add(attr)
       
   252                 self.skip_security_attributes.add(attr)
       
   253 
       
   254     def setdefault(self, attr, default):
   254         """override setdefault to update self.edited_attributes"""
   255         """override setdefault to update self.edited_attributes"""
   255         super(Entity, self).setdefault(key, default)
   256         super(Entity, self).setdefault(attr, default)
   256         if hasattr(self, 'edited_attributes'):
   257         if hasattr(self, 'edited_attributes'):
   257             self.edited_attributes.add(key)
   258             self.edited_attributes.add(attr)
       
   259             self.skip_security_attributes.add(attr)
       
   260 
       
   261     def rql_set_value(self, attr, value):
       
   262         """call by rql execution plan when some attribute is modified
       
   263 
       
   264         don't use dict api in such case since we don't want attribute to be
       
   265         added to skip_security_attributes.
       
   266         """
       
   267         super(Entity, self).__setitem__(attr, value)
   258 
   268 
   259     def pre_add_hook(self):
   269     def pre_add_hook(self):
   260         """hook called by the repository before doing anything to add the entity
   270         """hook called by the repository before doing anything to add the entity
   261         (before_add entity hooks have not been called yet). This give the
   271         (before_add entity hooks have not been called yet). This give the
   262         occasion to do weird stuff such as autocast (File -> Image for instance).
   272         occasion to do weird stuff such as autocast (File -> Image for instance).
   865         self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema,
   875         self._cw.execute('DELETE %s X WHERE X eid %%(x)s' % self.e_schema,
   866                          {'x': self.eid}, **kwargs)
   876                          {'x': self.eid}, **kwargs)
   867 
   877 
   868     # server side utilities ###################################################
   878     # server side utilities ###################################################
   869 
   879 
       
   880     @property
       
   881     def skip_security_attributes(self):
       
   882         try:
       
   883             return self._skip_security_attributes
       
   884         except:
       
   885             self._skip_security_attributes = set()
       
   886             return self._skip_security_attributes
       
   887 
   870     def set_defaults(self):
   888     def set_defaults(self):
   871         """set default values according to the schema"""
   889         """set default values according to the schema"""
   872         self._default_set = set()
       
   873         for attr, value in self.e_schema.defaults():
   890         for attr, value in self.e_schema.defaults():
   874             if not self.has_key(attr):
   891             if not self.has_key(attr):
   875                 self[str(attr)] = value
   892                 self[str(attr)] = value
   876                 self._default_set.add(attr)
       
   877 
   893 
   878     def check(self, creation=False):
   894     def check(self, creation=False):
   879         """check this entity against its schema. Only final relation
   895         """check this entity against its schema. Only final relation
   880         are checked here, constraint on actual relations are checked in hooks
   896         are checked here, constraint on actual relations are checked in hooks
   881         """
   897         """