entity.py
branchstable
changeset 4989 0e6dca27422c
parent 4988 d85f639e9150
child 4990 3bf481cf51cb
equal deleted inserted replaced
4988:d85f639e9150 4989:0e6dca27422c
   233         return super(Entity, self).__getitem__(key)
   233         return super(Entity, self).__getitem__(key)
   234 
   234 
   235     def __setitem__(self, attr, value):
   235     def __setitem__(self, attr, value):
   236         """override __setitem__ to update self.edited_attributes.
   236         """override __setitem__ to update self.edited_attributes.
   237 
   237 
   238         Typically, a before_update_hook could do::
   238         Typically, a before_[update|add]_hook could do::
   239 
   239 
   240             entity['generated_attr'] = generated_value
   240             entity['generated_attr'] = generated_value
   241 
   241 
   242         and this way, edited_attributes will be updated accordingly
   242         and this way, edited_attributes will be updated accordingly. Also, add
       
   243         the attribute to skip_security since we don't want to check security
       
   244         for such attributes set by hooks.
   243         """
   245         """
   244         if attr == 'eid':
   246         if attr == 'eid':
   245             warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead',
   247             warn('[3.7] entity["eid"] = value is deprecated, use entity.eid = value instead',
   246                  DeprecationWarning, stacklevel=2)
   248                  DeprecationWarning, stacklevel=2)
   247             self.eid = value
   249             self.eid = value
   248         else:
   250         else:
   249             super(Entity, self).__setitem__(attr, value)
   251             super(Entity, self).__setitem__(attr, value)
   250             if hasattr(self, 'edited_attributes'):
   252             if hasattr(self, 'edited_attributes'):
   251                 self.edited_attributes.add(attr)
   253                 self.edited_attributes.add(attr)
   252                 self.skip_security_attributes.add(attr)
   254                 self.skip_security_attributes.add(attr)
       
   255 
       
   256     def __delitem__(self, attr):
       
   257         """override __delitem__ to update self.edited_attributes on cleanup of
       
   258         undesired changes introduced in the entity's dict. For example, see the
       
   259         code snippet below from the `forge` cube:
       
   260 
       
   261         .. sourcecode:: python
       
   262 
       
   263             edited = self.entity.edited_attributes
       
   264             has_load_left = 'load_left' in edited
       
   265             if 'load' in edited and self.entity.load_left is None:
       
   266                 self.entity.load_left = self.entity['load']
       
   267             elif not has_load_left and edited:
       
   268                 # cleanup, this may cause undesired changes
       
   269                 del self.entity['load_left']
       
   270 
       
   271         """
       
   272         super(Entity, self).__delitem__(attr)
       
   273         if hasattr(self, 'edited_attributes'):
       
   274             self.edited_attributes.remove(attr)
   253 
   275 
   254     def setdefault(self, attr, default):
   276     def setdefault(self, attr, default):
   255         """override setdefault to update self.edited_attributes"""
   277         """override setdefault to update self.edited_attributes"""
   256         super(Entity, self).setdefault(attr, default)
   278         super(Entity, self).setdefault(attr, default)
   257         if hasattr(self, 'edited_attributes'):
   279         if hasattr(self, 'edited_attributes'):