app/soc/logic/models/base.py
changeset 608 77bffba4e946
parent 606 65d35584ee31
child 634 ad58da0b78e8
equal deleted inserted replaced
607:7e560d8cc035 608:77bffba4e946
    76     taken when a field is updated. The field is not updated if the
    76     taken when a field is updated. The field is not updated if the
    77     method does not return a True value.
    77     method does not return a True value.
    78     """
    78     """
    79 
    79 
    80     return True
    80     return True
       
    81   
       
    82   def _onCreate(self, entity):
       
    83     """Called when an entity has been created.
       
    84     
       
    85     Classes that override this can use it to do any post-creation operations.
       
    86     """
       
    87     
       
    88     pass
       
    89   
       
    90   def _onUpdate(self, entity):
       
    91     """Called when an entity has been updated.
       
    92     
       
    93     Classes that override this can use it to do any post-update operations.
       
    94     """
       
    95     
       
    96     pass
       
    97   
       
    98   def _onDelete(self, entity):
       
    99     """Called when an entity has been deleted.
       
   100     
       
   101     Classes that override this can use it to do any post-deletion operations.
       
   102     """
       
   103     
       
   104     pass
    81 
   105 
    82   def _keyName(self, **kwargs):
   106   def _keyName(self, **kwargs):
    83     """Returns the KeyName constructed from kwargs for this type of entity.
   107     """Returns the KeyName constructed from kwargs for this type of entity.
    84 
   108 
    85     The KeyName is in the following format:
   109     The KeyName is in the following format:
   314     """
   338     """
   315 
   339 
   316     def update():
   340     def update():
   317       return self._unsafeUpdateModelProperties(model, model_properties)
   341       return self._unsafeUpdateModelProperties(model, model_properties)
   318 
   342 
       
   343     entity =  db.run_in_transaction(update)
       
   344     
       
   345     # call the _onUpdate method
       
   346     self._onUpdate(entity)
       
   347     
       
   348     return entity
       
   349   
       
   350   def _silentUpdateModelProperties(self, model, model_properties):
       
   351     """Update existing model entity without calling _onUpdate.
       
   352     
       
   353     Args:
       
   354       model: a model entity
       
   355       model_properties: keyword arguments that correspond to model entity
       
   356         properties and their values
       
   357 
       
   358     Returns:
       
   359       The original model entity with any supplied properties changed.
       
   360     """
       
   361     
       
   362     def update():
       
   363       return self._unsafeUpdateModelProperties(model, model_properties)
       
   364 
   319     return db.run_in_transaction(update)
   365     return db.run_in_transaction(update)
   320 
   366 
   321   def _unsafeUpdateModelProperties(self, model, model_properties):
   367   def _unsafeUpdateModelProperties(self, model, model_properties):
   322     """See updateModelProperties.
   368     """See updateModelProperties.
   323 
   369 
   350       properties changed, or a new entity now associated with the
   396       properties changed, or a new entity now associated with the
   351       supplied key_name and properties.
   397       supplied key_name and properties.
   352     """
   398     """
   353 
   399 
   354     entity = self.getFromKeyName(key_name)
   400     entity = self.getFromKeyName(key_name)
   355 
   401     
   356     if not entity:
   402     create_entity = not entity
       
   403     
       
   404     if create_entity:
   357       # entity did not exist, so create one in a transaction
   405       # entity did not exist, so create one in a transaction
   358       entity = self._model.get_or_insert(key_name, **properties)
   406       entity = self._model.get_or_insert(key_name, **properties)
   359 
   407       
       
   408     
   360     # there is no way to be sure if get_or_insert() returned a new entity or
   409     # there is no way to be sure if get_or_insert() returned a new entity or
   361     # got an existing one due to a race, so update with properties anyway,
   410     # got an existing one due to a race, so update with properties anyway,
   362     # in a transaction
   411     # in a transaction
   363     return self.updateModelProperties(entity, properties)
   412     entity = self._silentUpdateModelProperties(entity, properties)
       
   413     
       
   414     if create_entity:
       
   415       # a new entity has been created call _onCreate
       
   416       self._onCreate(entity)
       
   417     else:
       
   418       # the entity has been updated call _onUpdate
       
   419       self._onUpdate(entity)
       
   420       
       
   421     return entity
   364 
   422 
   365   def updateOrCreateFromFields(self, properties, fields):
   423   def updateOrCreateFromFields(self, properties, fields):
   366     """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
   424     """Like updateOrCreateFromKeyName, but resolves fields to a key_name first.
   367     """
   425     """
   368 
   426 
   386     Args:
   444     Args:
   387       entity: an existing entity in datastore
   445       entity: an existing entity in datastore
   388     """
   446     """
   389 
   447 
   390     entity.delete()
   448     entity.delete()
       
   449     # entity has been deleted call _onDelete
       
   450     self._onDelete(entity)