app/soc/logic/site/id_user.py
author Todd Larsen <tlarsen@google.com>
Fri, 12 Sep 2008 02:12:38 +0000
changeset 131 3db97cf7f2c7
parent 112 4d9895fb15bc
child 135 a7ccde9d9eed
permissions -rw-r--r--
...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Basic ID (Google Account) and User (Model) query functions.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  ]
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    25
import re
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    26
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
from google.appengine.api import users
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    28
from google.appengine.ext import db
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
from soc.logic import out_of_band
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
import soc.models.user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    35
def getUserKeyNameFromId(id):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    36
  """Return a Datastore key_name for a User derived from a Google Account.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    37
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    38
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    39
    id: a Google Account (users.User) object
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    40
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    41
  if not id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    42
    return None
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    43
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    44
  return 'User:%s' % id.email()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    45
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    46
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
def getIdIfMissing(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  """Gets Google Account of logged-in user (possibly None) if id is false.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  This is a convenience function that simplifies a lot of view code that
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  accepts an optional id argument from the caller (such as one looked up
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
  already by another view that decides to "forward" the request to this
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
  other view).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    56
    id: a Google Account (users.User) object, or None
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    If id is non-false, it is simply returned; otherwise, the Google Account
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    of currently logged-in user is returned (which could be None if no user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
    is logged in).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
  if not id:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
    # id not initialized, so check if a Google Account is currently logged in
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
    id = users.get_current_user()
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
  return id
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    69
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
def getUserFromId(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
  """Returns User entity for a Google Account, or None if not found.  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    74
    id: a Google Account (users.User) object
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
  """
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    76
  # first, attempt a lookup by User:id key name
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    77
  key_name = getUserKeyNameFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    78
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    79
  if key_name:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    80
    user = soc.models.user.User.get_by_key_name(key_name)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    81
  else:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    82
    user = None
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    83
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    84
  if user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    85
    return user
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    87
  # email address may have changed, so query the id property
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    88
  user = soc.models.user.User.gql('WHERE id = :1', id).get()
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    90
  if user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    91
    return user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    92
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    93
  # last chance: perhaps the User changed their email address at some point
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    94
  user = soc.models.user.User.gql('WHERE former_ids = :1', id).get()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    95
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    96
  return user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    97
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    98
  
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
def getUserIfMissing(user, id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
  """Conditionally returns User entity for a Google Account.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
  This function is used to look up the User entity corresponding to the
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
  supplied Google Account *if* the user parameter is false (usually None).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
  This function is basically a no-op if user already refers to a User
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
  entity.  This is a convenience function that simplifies a lot of view
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
  code that accepts an optional user argument from the caller (such as
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
  one looked up already by another view that decides to "forward" the
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
  HTTP request to this other view).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
    user: None (usually), or an existing User entity
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   112
    id: a Google Account (users.User) object
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
    * user (which may have already been None if passed in that way by the
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
      caller) if id is false or user is non-false
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
    * results of getUserFromId() if user is false and id is non-false
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
  if id and (not user):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
    # Google Account supplied and User uninitialized, so look up User entity
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
    user = getUserFromId(id)
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
  return user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
def doesUserExist(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
  """Returns True if User exists in the Datastore for a Google Account.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
    id: a Google Account object
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   132
  if getUserFromId(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
    return True
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
  else:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
    return False
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   136
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   137
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   138
def isIdDeveloper(id=None):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   139
  """Returns True if Google Account is a Developer with special privileges.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   140
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   141
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   142
    id: a Google Account (users.User) object; if id is not supplied,
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   143
      the current logged-in user is checked using the App Engine Users API.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   144
      THIS ARGUMENT IS CURRENTLY IGNORED AND ONLY THE CURRENTLY LOGGED-IN
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   145
      USER IS CHECKED!
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   146
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   147
  See the TODO in the code below...
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   148
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   149
  if not id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   150
    return users.is_current_user_admin()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   151
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   152
  # TODO(tlarsen): this Google App Engine function only checks the currently
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   153
  #   logged in user.  There needs to be another way to do this, such as a
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   154
  #   field in the User Model...
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   155
  return users.is_current_user_admin()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   156
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   157
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   158
LINKNAME_PATTERN = r'''(?x)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   159
    ^
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   160
    [0-9a-z]   # start with ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   161
    (
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   162
     [0-9a-z]  # additional ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   163
     |         # -OR-
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   164
     _[0-9a-z] # underscore and ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   165
    )*         # zero or more of OR group
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   166
    $
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   167
'''
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   168
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   169
LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   170
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   171
def isLinkNameFormatValid(link_name):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   172
  """Returns True if link_name is in a valid format.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   173
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   174
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   175
    link_name: link name used in URLs to identify user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   176
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   177
  if LINKNAME_REGEX.match(link_name):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   178
    return True
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   179
  return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   180
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   181
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
def getUserFromLinkName(link_name):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
  """Returns User entity for link_name or None if not found.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
    link_name: link name used in URLs to identify user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
  return soc.models.user.User.gql('WHERE link_name = :1', link_name).get()
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
def getUserIfLinkName(link_name):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
  """Returns User entity for supplied link_name if one exists.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
    link_name: link name used in URLs to identify user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
    * None if link_name is false.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
    * User entity that has supplied link_name
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
  Raises:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
    out_of_band.ErrorResponse if link_name is not false, but no User entity
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
    with the supplied link_name exists in the Datastore
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   204
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
  if not link_name:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
    # exit without error, to let view know that link_name was not supplied
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
    return None
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
  link_name_user = getUserFromLinkName(link_name)
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
  if link_name_user:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
    # a User has this link name, so return that corresponding User entity
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
    return link_name_user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   215
  # else: a link name was supplied, but there is no User that has it
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
  raise out_of_band.ErrorResponse(
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
      'There is no user with a "link name" of "%s".' % link_name, status=404)
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   218
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   219
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   220
def doesLinkNameBelongToId(link_name, id=None):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   221
  """Returns True if supplied link name belongs to supplied Google Account.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   222
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   223
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   224
    link_name: link name used in URLs to identify user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   225
    id: a Google Account object; optional, current logged-in user will
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   226
      be used (or False will be returned if no user is logged in)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   227
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   228
  id = getIdIfMissing(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   229
    
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   230
  if not id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   231
    # id not supplied and no Google Account logged in, so link name cannot
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   232
    # belong to an unspecified User
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   233
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   234
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   235
  user = getUserFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   236
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   237
  if not user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   238
    # no User corresponding to id Google Account, so no link name at all 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   239
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   240
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   241
  if user.link_name != link_name:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   242
    # User exists for id, but does not have this link name
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   243
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   244
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   245
  return True  # link_name does actually belong to this Google Account
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   246
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   247
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   248
def updateOrCreateUserFromId(id, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   249
  """Update existing User entity, or create new one with supplied properties.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   250
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   251
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   252
    id: a Google Account object
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   253
    **user_properties: keyword arguments that correspond to User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   254
      properties and their values
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   255
      
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   256
  Returns:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   257
    the User entity corresponding to the Google Account, with any supplied
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   258
    properties changed, or a new User entity now associated with the Google
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   259
    Account and with the supplied properties
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   260
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   261
  # attempt to retrieve the existing User
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   262
  user = getUserFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   263
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   264
  if not user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   265
    # user did not exist, so create one in a transaction
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   266
    key_name = getUserKeyNameFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   267
    user = soc.models.user.User.get_or_insert(
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   268
      key_name, id=id, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   269
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   270
  # there is no way to be sure if get_or_insert() returned a new User or
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   271
  # got an existing one due to a race, so update with user_properties anyway,
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   272
  # in a transaction
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   273
  return updateUserProperties(user, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   274
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   275
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   276
def updateUserProperties(user, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   277
  """Update existing User entity using supplied User properties.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   278
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   279
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   280
    user: a User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   281
    **user_properties: keyword arguments that correspond to User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   282
      properties and their values
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   283
      
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   284
  Returns:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   285
    the original User entity with any supplied properties changed 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   286
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   287
  def update():
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   288
    return _unsafeUpdateUserProperties(user, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   289
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   290
  return db.run_in_transaction(update)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   291
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   292
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   293
def _unsafeUpdateUserProperties(user, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   294
  """(see updateUserProperties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   295
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   296
  Like updateUserProperties(), but not run within a transaction. 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   297
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   298
  properties = user.properties()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   299
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   300
  for prop in properties.values():
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   301
    if prop.name in user_properties:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   302
      if prop.name == 'former_ids':
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   303
        # former_ids cannot be overwritten directly
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   304
        continue
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   305
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   306
      value = user_properties[prop.name]
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   307
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   308
      if prop.name == 'id':
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   309
        old_id = user.id
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   310
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   311
        if value != old_id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   312
          user.former_ids.append(old_id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   313
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   314
      prop.__set__(user, value)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   315
        
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   316
  user.put()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   317
  return user