app/soc/logic/site/id_user.py
changeset 112 4d9895fb15bc
child 131 3db97cf7f2c7
equal deleted inserted replaced
111:f506a22f50db 112:4d9895fb15bc
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Basic ID (Google Account) and User (Model) query functions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Todd Larsen" <tlarsen@google.com>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.api import users
       
    26 
       
    27 from soc.logic import out_of_band
       
    28 
       
    29 import soc.models.user
       
    30 
       
    31 
       
    32 def getIdIfMissing(id):
       
    33   """Gets Google Account of logged-in user (possibly None) if id is false.
       
    34   
       
    35   This is a convenience function that simplifies a lot of view code that
       
    36   accepts an optional id argument from the caller (such as one looked up
       
    37   already by another view that decides to "forward" the request to this
       
    38   other view).
       
    39 
       
    40   Args:
       
    41     id: a Google Account object, or None
       
    42     
       
    43   Returns:
       
    44     If id is non-false, it is simply returned; otherwise, the Google Account
       
    45     of currently logged-in user is returned (which could be None if no user
       
    46     is logged in).
       
    47   """
       
    48   if not id:
       
    49     # id not initialized, so check if a Google Account is currently logged in
       
    50     id = users.get_current_user()
       
    51 
       
    52   return id
       
    53 
       
    54   
       
    55 def getUserFromId(id):
       
    56   """Returns User entity for a Google Account, or None if not found.  
       
    57     
       
    58   Args:
       
    59     id: a Google Account object
       
    60   """
       
    61   return soc.models.user.User.gql('WHERE id = :1', id).get()
       
    62 
       
    63 
       
    64 def getUserIfMissing(user, id):
       
    65   """Conditionally returns User entity for a Google Account.
       
    66   
       
    67   This function is used to look up the User entity corresponding to the
       
    68   supplied Google Account *if* the user parameter is false (usually None).
       
    69   This function is basically a no-op if user already refers to a User
       
    70   entity.  This is a convenience function that simplifies a lot of view
       
    71   code that accepts an optional user argument from the caller (such as
       
    72   one looked up already by another view that decides to "forward" the
       
    73   HTTP request to this other view).
       
    74 
       
    75   Args:
       
    76     user: None (usually), or an existing User entity
       
    77     id: a Google Account object
       
    78     
       
    79   Returns:
       
    80     * user (which may have already been None if passed in that way by the
       
    81       caller) if id is false or user is non-false
       
    82     * results of getUserFromId() if user is false and id is non-false
       
    83   """
       
    84   if id and (not user):
       
    85     # Google Account supplied and User uninitialized, so look up User entity
       
    86     user = getUserFromId(id)
       
    87     
       
    88   return user
       
    89 
       
    90 
       
    91 def doesUserExist(id):
       
    92   """Returns True if User exists in the Datastore for a Google Account.
       
    93     
       
    94   Args:
       
    95     id: a Google Account object
       
    96   """
       
    97   if getUserFromId(id):
       
    98     return True
       
    99   else:
       
   100     return False
       
   101 
       
   102 
       
   103 def getUserFromLinkName(link_name):
       
   104   """Returns User entity for link_name or None if not found.
       
   105     
       
   106   Args:
       
   107     link_name: link name used in URLs to identify user
       
   108   """
       
   109   return soc.models.user.User.gql('WHERE link_name = :1', link_name).get()
       
   110 
       
   111 
       
   112 def getUserIfLinkName(link_name):
       
   113   """Returns User entity for supplied link_name if one exists.
       
   114   
       
   115   Args:
       
   116     link_name: link name used in URLs to identify user
       
   117 
       
   118   Returns:
       
   119     * None if link_name is false.
       
   120     * User entity that has supplied link_name
       
   121 
       
   122   Raises:
       
   123     out_of_band.ErrorResponse if link_name is not false, but no User entity
       
   124     with the supplied link_name exists in the Datastore
       
   125   """
       
   126   if not link_name:
       
   127     # exit without error, to let view know that link_name was not supplied
       
   128     return None
       
   129 
       
   130   link_name_user = getUserFromLinkName(link_name)
       
   131     
       
   132   if link_name_user:
       
   133     # a User has this link name, so return that corresponding User entity
       
   134     return link_name_user
       
   135 
       
   136   # else: a link name was supplied, but there is no User that has it
       
   137   raise out_of_band.ErrorResponse(
       
   138       'There is no user with a "link name" of "%s".' % link_name, status=404)