app/soc/logic/models/base.py
changeset 499 d22e4fe8e64b
parent 487 1a7591ff0051
child 512 aae25d2b4464
equal deleted inserted replaced
498:1cd81063a4c6 499:d22e4fe8e64b
    32 from soc.logic import dicts
    32 from soc.logic import dicts
    33 from soc.logic import out_of_band
    33 from soc.logic import out_of_band
    34 
    34 
    35 
    35 
    36 class Error(Exception):
    36 class Error(Exception):
    37   """Base class for all exceptions raised by this module."""
    37   """Base class for all exceptions raised by this module.
       
    38   """
    38   pass
    39   pass
    39 
    40 
    40 
    41 
    41 class Logic:
    42 class Logic:
    42   """Base logic for entity classes.
    43   """Base logic for entity classes.
    71     """
    72     """
    72 
    73 
    73     return True
    74     return True
    74 
    75 
    75   def _keyName(self, **kwargs):
    76   def _keyName(self, **kwargs):
    76     """Returns the KeyName constructed from kwargs for this type of entity
    77     """Returns the KeyName constructed from kwargs for this type of entity.
    77 
    78 
    78     The KeyName is in the following format:
    79     The KeyName is in the following format:
    79     entity.name:<key_value1>:<key_value2>:...:<key_valueN>
    80     entity.name:<key_value1>:<key_value2>:...:<key_valueN>
    80     """
    81     """
    81 
    82 
   112     """ 
   113     """ 
   113     return '%s.%s' % (self._model.__module__,
   114     return '%s.%s' % (self._model.__module__,
   114                       self.getModelClassName()) 
   115                       self.getModelClassName()) 
   115 
   116 
   116   def getKeyValues(self, entity):
   117   def getKeyValues(self, entity):
   117     """Exctracts the key values from entity and returns them
   118     """Exctracts the key values from entity and returns them.
   118 
   119 
   119     Args:
   120     Args:
   120       entity: the entity from which to extract the key values
   121       entity: the entity from which to extract the key values
   121     """
   122     """
   122 
   123 
   123     raise NotImplementedError
   124     raise NotImplementedError
   124 
   125 
   125   def getSuffixValues(self, entity):
   126   def getSuffixValues(self, entity):
   126     """Returns an array with the public values of the Key Fields
   127     """Returns an array with the public values of the Key Fields.
   127 
   128 
   128     Defaults to getKeyValues
   129     Defaults to getKeyValues
   129     If any of the Key Fields should not be made public, this method
   130     If any of the Key Fields should not be made public, this method
   130     should return their public counterparts instead.
   131     should return their public counterparts instead.
   131 
   132 
   134     """
   135     """
   135 
   136 
   136     return self.getKeyValues(entity)
   137     return self.getKeyValues(entity)
   137 
   138 
   138   def getKeyValuesFromFields(self, fields):
   139   def getKeyValuesFromFields(self, fields):
   139     """Exctracts the key values from a dict and returns them
   140     """Exctracts the key values from a dict and returns them.
   140 
   141 
   141     Args:
   142     Args:
   142       fields: the dict from which to extract the key values
   143       fields: the dict from which to extract the key values
   143     """
   144     """
   144 
   145 
   145     raise NotImplementedError
   146     raise NotImplementedError
   146 
   147 
   147   def getKeyFieldNames(self):
   148   def getKeyFieldNames(self):
   148     """Returns an array with the names of the Key Fields 
   149     """Returns an array with the names of the Key Fields.
   149     """
   150     """
   150 
   151 
   151     raise NotImplementedError
   152     raise NotImplementedError
   152 
   153 
   153   def getKeySuffix(self, entity):
   154   def getKeySuffix(self, entity):
   154     """Returns a suffix for the specified entity or None if no entity specified
   155     """Returns a suffix for the specified entity or None if no entity specified.
   155 
   156 
   156     Args:
   157     Args:
   157       entity: the entity for which to get the suffix
   158       entity: the entity for which to get the suffix
   158     """
   159     """
   159 
   160 
   164     suffix = '/'.join(key_values)
   165     suffix = '/'.join(key_values)
   165 
   166 
   166     return suffix
   167     return suffix
   167 
   168 
   168   def getKeyFieldsFromDict(self, dictionary):
   169   def getKeyFieldsFromDict(self, dictionary):
   169     """Does any required massaging and filtering of dictionary
   170     """Does any required massaging and filtering of dictionary.
   170 
   171 
   171     The resulting dictionary contains just the key names, and has any
   172     The resulting dictionary contains just the key names, and has any
   172     required translations/modifications performed.
   173     required translations/modifications performed.
   173 
   174 
   174     Args:
   175     Args:
   216       * None if a field is false.
   217       * None if a field is false.
   217       * Entity for supplied fields
   218       * Entity for supplied fields
   218 
   219 
   219     Raises:
   220     Raises:
   220       out_of_band.ErrorResponse if link name is not false, but no entity
   221       out_of_band.ErrorResponse if link name is not false, but no entity
   221       with the supplied link name exists in the Datastore
   222       with the supplied link name exists in the Datastore.
   222     """
   223     """
   223 
   224 
   224     if not all(fields.values()):
   225     if not all(fields.values()):
   225       # exit without error, to let view know that link_name was not supplied
   226       # exit without error, to let view know that link_name was not supplied
   226       return None
   227       return None
   268 
   269 
   269     query = self._model.all()
   270     query = self._model.all()
   270     return query.fetch(limit, offset)
   271     return query.fetch(limit, offset)
   271 
   272 
   272   def getForFields(self, properties, unique=False, limit=1000, offset=0):
   273   def getForFields(self, properties, unique=False, limit=1000, offset=0):
   273     """Returns all entities that have the specified properties
   274     """Returns all entities that have the specified properties.
   274 
   275 
   275     Args:
   276     Args:
   276       properties: the properties that the entity should have
   277       properties: the properties that the entity should have
   277       unique: if set, only the first item from the resultset will be returned
   278       unique: if set, only the first item from the resultset will be returned
   278       limit: max amount of entities to return
   279       limit: max amount of entities to return
   430       model: a model entity
   431       model: a model entity
   431       model_properties: keyword arguments that correspond to model entity
   432       model_properties: keyword arguments that correspond to model entity
   432         properties and their values
   433         properties and their values
   433 
   434 
   434     Returns:
   435     Returns:
   435       the original model entity with any supplied properties changed
   436       The original model entity with any supplied properties changed.
   436     """
   437     """
   437 
   438 
   438     def update():
   439     def update():
   439       return self._unsafeUpdateModelProperties(model, model_properties)
   440       return self._unsafeUpdateModelProperties(model, model_properties)
   440 
   441 
   441     return db.run_in_transaction(update)
   442     return db.run_in_transaction(update)
   442 
   443 
   443   def _unsafeUpdateModelProperties(self, model, model_properties):
   444   def _unsafeUpdateModelProperties(self, model, model_properties):
   444     """(see updateModelProperties)
   445     """See updateModelProperties.
   445 
   446 
   446     Like updateModelProperties(), but not run within a transaction.
   447     Like updateModelProperties(), but not run within a transaction.
   447     """
   448     """
   448 
   449 
   449     properties = model.properties()
   450     properties = model.properties()