app/soc/logic/model.py
changeset 141 e120c24b89e2
child 254 e88beba437a3
equal deleted inserted replaced
140:c3d098d6fafa 141:e120c24b89e2
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Helpers functions for updating different kinds of models in datastore.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Pawel Solyga" <pawel.solyga@gmail.com>',
       
    22   ]
       
    23 
       
    24 from google.appengine.ext import db
       
    25 
       
    26 def updateModelProperties(model, **model_properties):
       
    27   """Update existing model entity using supplied model properties.
       
    28 
       
    29   Args:
       
    30     model: a model entity
       
    31     **model_properties: keyword arguments that correspond to model entity
       
    32       properties and their values
       
    33 
       
    34   Returns:
       
    35     the original model entity with any supplied properties changed 
       
    36   """
       
    37   def update():
       
    38     return _unsafeUpdateModelProperties(model, **model_properties)
       
    39 
       
    40   return db.run_in_transaction(update)
       
    41 
       
    42 
       
    43 def _unsafeUpdateModelProperties(model, **model_properties):
       
    44   """(see updateModelProperties)
       
    45 
       
    46   Like updateModelProperties(), but not run within a transaction. 
       
    47   """
       
    48   properties = model.properties()
       
    49 
       
    50   for prop in properties.values():
       
    51     if prop.name in model_properties:
       
    52       value = model_properties[prop.name]
       
    53       prop.__set__(model, value)
       
    54 
       
    55   model.put()
       
    56   return model