Fixed an outstanding TODO for tlarsen
authorSverre Rabbelier <srabbelier@gmail.com>
Wed, 26 Nov 2008 23:54:40 +0000
changeset 592 be98a2f5d8a2
parent 591 530fa94faffe
child 593 01f8c7aabb7e
Fixed an outstanding TODO for tlarsen A nice side-effect is that accounts now does not depend on out_of_band, sadly that does not fix the cyclic dependency, but it is a nice side-effect nonetheless.
app/soc/logic/accounts.py
app/soc/logic/models/base.py
--- a/app/soc/logic/accounts.py	Wed Nov 26 21:34:11 2008 +0000
+++ b/app/soc/logic/accounts.py	Wed Nov 26 23:54:40 2008 +0000
@@ -26,7 +26,6 @@
 from google.appengine.api import users
 
 from soc.logic import models
-from soc.views import out_of_band
 
 import soc.models.user
 import soc.logic.models.user
@@ -110,20 +109,3 @@
 
   # email does not already belong to this User, but to some other User
   return False
-
-
-# TODO(tlarsen): make this generic for any Linkable and move elsewhere
-def getUserFromLinkIdOr404(link_id):
-  """Like getUserFromLinkId but expects to find a user.
-
-  Raises:
-    out_of_band.Error if no User entity is found
-  """
-  user = models.user.logic.getForFields({'link_id': link_id},
-                                        unique=True)
-
-  if user:
-    return user
-
-  raise out_of_band.Error(
-      'There is no user with a "link ID" of "%s".' % link_id, status=404)
--- a/app/soc/logic/models/base.py	Wed Nov 26 21:34:11 2008 +0000
+++ b/app/soc/logic/models/base.py	Wed Nov 26 23:54:40 2008 +0000
@@ -207,29 +207,17 @@
 
     return entity
 
-  def getIfFields(self, fields):
-    """Returns entity for supplied link ID if one exists.
 
-    Args:
-      fields: the fields of the entity that uniquely identifies it
-
-    Returns:
-      * None if a field is false.
-      * Entity for supplied fields
+  def getFromFieldsOr404(self, **fields):
+    """Like getFromFields but expects to find an entity.
 
     Raises:
-      out_of_band.Error if link ID is not false, but no entity
-      with the supplied link ID exists in the Datastore.
+      out_of_band.Error if no User entity is found
     """
 
-    if not all(fields.values()):
-      # exit without error, to let view know that link_id was not supplied
-      return None
-
     entity = self.getFromFields(**fields)
 
     if entity:
-      # an entity exist for this link_id, so return that entity
       return entity
 
     format_text = ugettext_lazy('"%(key)s" is "%(value)s"')
@@ -243,9 +231,21 @@
       'There is no "%(name)s" where %(pairs)s.') % {
         'name': self._name, 'pairs': joined_pairs}
 
+    raise out_of_band.Error(msg, status=404)
 
-    # else: fields were supplied, but there is no Entity that has it
-    raise out_of_band.Error(msg, status=404)
+
+  def getIfFields(self, fields):
+    """Like getFromFieldsOr404 but returns none if not all fields are set
+
+    Raises:
+      out_of_band.Error if no User entity is found and all fields were set
+    """
+
+    if not all(fields.values()):
+      return None
+
+    return self.getFromFieldsOr404(**fields)
+
 
   def getKeyNameForFields(self, fields):
     """Return a Datastore key_name for a Entity from the specified fields.