app/soc/logic/models/base.py
changeset 986 e9611a2288ca
parent 970 8b5611d5b053
child 1106 e14b0995cf29
equal deleted inserted replaced
985:f0b02777f403 986:e9611a2288ca
    89       return 0
    89       return 0
    90 
    90 
    91     depth = self._scope_logic.logic.getScopeDepth()
    91     depth = self._scope_logic.logic.getScopeDepth()
    92     return None if (depth is None) else (depth + 1)
    92     return None if (depth is None) else (depth + 1)
    93 
    93 
    94   def _updateField(self, model, name, value):
    94   def _updateField(self, entity, name, value):
    95     """Hook called when a field is updated.
    95     """Hook called when a field is updated.
    96 
    96 
    97     Base classes should override if any special actions need to be
    97     Base classes should override if any special actions need to be
    98     taken when a field is updated. The field is not updated if the
    98     taken when a field is updated. The field is not updated if the
    99     method does not return a True value.
    99     method does not return a True value.
       
   100 
       
   101     Args:
       
   102       entity: the unaltered entity
       
   103       name: the name of the field to be changed
       
   104       value: the new value
   100     """
   105     """
   101 
   106 
   102     return True
   107     return True
   103   
   108   
   104   def _onCreate(self, entity):
   109   def _onCreate(self, entity):
   336       # In the case result is empty, return None
   341       # In the case result is empty, return None
   337       return None
   342       return None
   338 
   343 
   339     return result
   344     return result
   340 
   345 
   341   def updateModelProperties(self, model, model_properties):
   346   def updateEntityProperties(self, entity, entity_properties):
   342     """Update existing model entity using supplied model properties.
   347     """Update existing entity using supplied properties.
   343 
   348 
   344     Args:
   349     Args:
   345       model: a model entity
   350       model: a model entity
   346       model_properties: keyword arguments that correspond to model entity
   351       model_properties: keyword arguments that correspond to entity
   347         properties and their values
   352         properties and their values
   348 
   353 
   349     Returns:
   354     Returns:
   350       The original model entity with any supplied properties changed.
   355       The original entity with any supplied properties changed.
   351     """
   356     """
   352 
   357 
   353     def update():
   358     def update():
   354       return self._unsafeUpdateModelProperties(model, model_properties)
   359       return self._unsafeUpdateEntityProperties(entity, entity_properties)
   355 
   360 
   356     entity =  db.run_in_transaction(update)
   361     entity = db.run_in_transaction(update)
   357     
   362 
   358     # call the _onUpdate method
   363     # call the _onUpdate method
   359     self._onUpdate(entity)
   364     self._onUpdate(entity)
   360     
   365 
   361     return entity
   366     return entity
   362   
   367 
   363   def _silentUpdateModelProperties(self, model, model_properties):
   368   def _silentUpdateEntityProperties(self, entity, entity_properties):
   364     """Update existing model entity without calling _onUpdate.
   369     """See _unsafeUpdateEntityProperties.
   365     
   370 
   366     Args:
   371     Does not call _onUpdate.
   367       model: a model entity
       
   368       model_properties: keyword arguments that correspond to model entity
       
   369         properties and their values
       
   370 
       
   371     Returns:
       
   372       The original model entity with any supplied properties changed.
       
   373     """
   372     """
   374     
   373     
   375     def update():
   374     def update():
   376       return self._unsafeUpdateModelProperties(model, model_properties)
   375       return self._unsafeUpdateEntityProperties(entity, entity_properties)
   377 
   376 
   378     return db.run_in_transaction(update)
   377     return db.run_in_transaction(update)
   379 
   378 
   380   def _unsafeUpdateModelProperties(self, model, model_properties):
   379   def _unsafeUpdateEntityProperties(self, entity, entity_properties):
   381     """See updateModelProperties.
   380     """See updateEntityProperties.
   382 
   381 
   383     Like updateModelProperties(), but not run within a transaction.
   382     Like updateEntityProperties(), but not run within a transaction.
   384     """
   383     """
   385 
   384 
   386     properties = model.properties()
   385     properties = entity.properties()
   387 
   386 
   388     for prop in properties.values():
   387     for prop in properties.values():
   389       name = prop.name
   388       name = prop.name
   390 
   389 
   391       if not name in self._skip_properties and name in model_properties:
   390       if not name in self._skip_properties and name in entity_properties:
   392         value = model_properties[prop.name]
   391         value = entity_properties[prop.name]
   393 
   392 
   394         if self._updateField(model, name, value):
   393         if self._updateField(entity, name, value):
   395           prop.__set__(model, value)
   394           prop.__set__(entity, value)
   396 
   395 
   397     model.put()
   396     entity.put()
   398     return model
   397     return entity
   399 
   398 
   400   def updateOrCreateFromKeyName(self, properties, key_name):
   399   def updateOrCreateFromKeyName(self, properties, key_name):
   401     """Update existing entity, or create new one with supplied properties.
   400     """Update existing entity, or create new one with supplied properties.
   402 
   401 
   403     Args:
   402     Args:
   420       
   419       
   421     
   420     
   422     # there is no way to be sure if get_or_insert() returned a new entity or
   421     # there is no way to be sure if get_or_insert() returned a new entity or
   423     # got an existing one due to a race, so update with properties anyway,
   422     # got an existing one due to a race, so update with properties anyway,
   424     # in a transaction
   423     # in a transaction
   425     entity = self._silentUpdateModelProperties(entity, properties)
   424     entity = self._silentUpdateEntityProperties(entity, properties)
   426     
   425     
   427     if create_entity:
   426     if create_entity:
   428       # a new entity has been created call _onCreate
   427       # a new entity has been created call _onCreate
   429       self._onCreate(entity)
   428       self._onCreate(entity)
   430     else:
   429     else: