app/soc/logic/models/base.py
changeset 592 be98a2f5d8a2
parent 565 b0c5d58f9d3e
child 596 7dd98eeba61b
equal deleted inserted replaced
591:530fa94faffe 592:be98a2f5d8a2
   205     else:
   205     else:
   206       entity = None
   206       entity = None
   207 
   207 
   208     return entity
   208     return entity
   209 
   209 
   210   def getIfFields(self, fields):
   210 
   211     """Returns entity for supplied link ID if one exists.
   211   def getFromFieldsOr404(self, **fields):
   212 
   212     """Like getFromFields but expects to find an entity.
   213     Args:
       
   214       fields: the fields of the entity that uniquely identifies it
       
   215 
       
   216     Returns:
       
   217       * None if a field is false.
       
   218       * Entity for supplied fields
       
   219 
   213 
   220     Raises:
   214     Raises:
   221       out_of_band.Error if link ID is not false, but no entity
   215       out_of_band.Error if no User entity is found
   222       with the supplied link ID exists in the Datastore.
   216     """
   223     """
       
   224 
       
   225     if not all(fields.values()):
       
   226       # exit without error, to let view know that link_id was not supplied
       
   227       return None
       
   228 
   217 
   229     entity = self.getFromFields(**fields)
   218     entity = self.getFromFields(**fields)
   230 
   219 
   231     if entity:
   220     if entity:
   232       # an entity exist for this link_id, so return that entity
       
   233       return entity
   221       return entity
   234 
   222 
   235     format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
   223     format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
   236 
   224 
   237     msg_pairs = [format_text % {'key': key, 'value': value}
   225     msg_pairs = [format_text % {'key': key, 'value': value}
   241 
   229 
   242     msg = ugettext_lazy(
   230     msg = ugettext_lazy(
   243       'There is no "%(name)s" where %(pairs)s.') % {
   231       'There is no "%(name)s" where %(pairs)s.') % {
   244         'name': self._name, 'pairs': joined_pairs}
   232         'name': self._name, 'pairs': joined_pairs}
   245 
   233 
   246 
       
   247     # else: fields were supplied, but there is no Entity that has it
       
   248     raise out_of_band.Error(msg, status=404)
   234     raise out_of_band.Error(msg, status=404)
       
   235 
       
   236 
       
   237   def getIfFields(self, fields):
       
   238     """Like getFromFieldsOr404 but returns none if not all fields are set
       
   239 
       
   240     Raises:
       
   241       out_of_band.Error if no User entity is found and all fields were set
       
   242     """
       
   243 
       
   244     if not all(fields.values()):
       
   245       return None
       
   246 
       
   247     return self.getFromFieldsOr404(**fields)
       
   248 
   249 
   249 
   250   def getKeyNameForFields(self, fields):
   250   def getKeyNameForFields(self, fields):
   251     """Return a Datastore key_name for a Entity from the specified fields.
   251     """Return a Datastore key_name for a Entity from the specified fields.
   252 
   252 
   253     Args:
   253     Args: