Added a getFromKeyNameOr404 to base.Logic
authorSverre Rabbelier <srabbelier@gmail.com>
Mon, 09 Mar 2009 23:56:14 +0000
changeset 1780 210ab3d5d6ea
parent 1779 cdd11aa8dbc7
child 1781 f8e85447fbdf
Added a getFromKeyNameOr404 to base.Logic This also fixes a bug introduced in r2437 where this method was used although it didn't exist yet. Patch by: Sverre Rabbelier
app/soc/logic/models/base.py
--- a/app/soc/logic/models/base.py	Mon Mar 09 20:43:46 2009 +0000
+++ b/app/soc/logic/models/base.py	Mon Mar 09 23:56:14 2009 +0000
@@ -222,6 +222,23 @@
 
     return self._model.get_by_key_name(key_name)
 
+  def getFromKeyNameOr404(self, key_name):
+    """Like getFromKeyName but expects to find an entity.
+
+    Raises:
+      out_of_band.Error if no entity is found
+    """
+
+    entity = self.getFromKeyName(key_name)
+
+    if entity:
+      return entity
+
+    msg = ugettext('There is no "%(name)s" named %(key_name)s.') % {
+        'name': self._name, 'key_name': key_name}
+
+    raise out_of_band.Error(msg, status=404)
+
   def getFromKeyFields(self, fields):
     """Returns the entity for the specified key names, or None if not found.