Factor out the getForAccount logic in user
authorSverre Rabbelier <srabbelier@gmail.com>
Thu, 26 Feb 2009 16:54:44 +0000
changeset 1521 bb086732bf7f
parent 1520 ac407f5b10dc
child 1522 983b126f5aca
Factor out the getForAccount logic in user Also normalize the account before retrieving it. Patch by: Sverre Rabbelier
app/soc/logic/models/user.py
--- a/app/soc/logic/models/user.py	Thu Feb 26 16:54:08 2009 +0000
+++ b/app/soc/logic/models/user.py	Thu Feb 26 16:54:44 2009 +0000
@@ -26,6 +26,7 @@
 from google.appengine.ext import db
 
 from soc.cache import sidebar
+from soc.logic import accounts
 from soc.logic.helper import notifications
 from soc.logic.models import base
 from soc.logic.models.site import logic as site_logic
@@ -72,10 +73,26 @@
     if not account:
       return None
 
-    user = self.getForFields({'account': account, 'status':'valid'}, 
-        unique=True)
+    return self.getForAccount(account)
+
+  def getForAccount(self, account):
+    """Retrieves the user entity for the specified account.
+
+    If there is no user logged in, or they have no valid associated User
+    entity, None is returned.
+    """
 
-    return user
+    if not account:
+      raise base.InvalidArgumentError
+
+    account = accounts.normalizeAccount(account)
+
+    fields = {
+        'account': account,
+        'status':'valid',
+        }
+
+    return self.getForFields(filter=fields, unique=True)
 
   def agreesToSiteToS(self, entity):
     """Returns indication of User's answer to the site-wide Terms of Service.