app/soc/logic/site/id_user.py
author Todd Larsen <tlarsen@google.com>
Thu, 25 Sep 2008 17:17:11 +0000
changeset 202 b8b4a83788d4
parent 184 7c0b42aecd9b
child 223 a18e93e21672
permissions -rw-r--r--
A key_name controller module to collect all of the name...() functions that compose Model entity key names, plus some minor changes to other controller modules to illustrate the proposed use. Patch by: Todd Larsen Review by: Pawel Solyga Review URL: http://codereviews.googleopensourceprograms.com/804 Review URL: http://codereviews.googleopensourceprograms.com/804
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
202
b8b4a83788d4 A key_name controller module to collect all of the name...() functions that
Todd Larsen <tlarsen@google.com>
parents: 184
diff changeset
    30
from soc.logic import key_name
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
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
    32
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
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
    34
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    36
def getUserKeyNameFromId(id):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    37
  """Return a Datastore key_name for a User derived from a Google Account.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    38
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    39
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    40
    id: a Google Account (users.User) object
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    41
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    42
  if not id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    43
    return None
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    44
202
b8b4a83788d4 A key_name controller module to collect all of the name...() functions that
Todd Larsen <tlarsen@google.com>
parents: 184
diff changeset
    45
  return key_name.nameUser(id.email())
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    46
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    47
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    48
def getIdIfMissing(id=None):
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
  """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
    50
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
  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
    52
  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
    53
  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
    54
  other view).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    57
    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
    58
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
    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
    61
    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
    62
    is logged in).
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
  if not id:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
    # 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
    66
    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
    67
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
  return id
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    70
def getUsersForOffsetAndLimit(offset=0, limit=0):
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    71
  """Returns Users entities for given offset and limit or None if not found.
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    72
    
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    73
  Args:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    74
    offset: offset in entities list which defines first entity to return
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    75
    limit: max amount of entities to return
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    76
  """
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    77
  query = db.GqlQuery('SELECT * FROM User ORDER BY id')
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    78
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    79
  # Fetch one more to see if there should be a 'next' link
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    80
  return query.fetch(limit+1, offset)  
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    81
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
def getUserFromId(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
  """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
    84
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  Args:
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    86
    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
    87
  """
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
    88
  return 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
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
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
    91
  """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
    92
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
  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
    94
  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
    95
  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
    96
  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
    97
  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
    98
  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
    99
  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
   100
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
    user: None (usually), or an existing User entity
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   103
    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
   104
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
    * 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
   107
      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
   108
    * 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
   109
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
  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
   111
    # 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
   112
    user = getUserFromId(id)
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
  return user
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
def doesUserExist(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
  """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
   119
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
    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
   122
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
  if getUserFromId(id):
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
    return True
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
  else:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
    return False
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   127
    
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   128
def isIdUser(id=None):
166
4d2cbd0ea977 Fixed too long lines and deleted unused imports in id_user.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 164
diff changeset
   129
  """Returns True if a Google Account has it's User entity in datastore.
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
141
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   131
  Args:
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   132
    id: a Google Account (users.User) object; if id is not supplied,
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   133
      the current logged-in user is checked
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   134
  """
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   135
  id = getIdIfMissing(id)
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   136
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   137
  if not id:
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   138
    # no Google Account was supplied or is logged in
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   139
    return False
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   140
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   141
  user = getUserFromId(id)
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   142
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   143
  if not user:
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   144
    # no User entity for this Google Account
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   145
    return False
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   146
  
e120c24b89e2 Added Melange front page edit view where you can change title, content, feed url. Created SiteSettings and Document models and some logic for them. Added isFeedURLValid function in soc/logic/feed.py. Created some functions for handling datastore updates of different kinds of Models (soc/logic/model.py). Fixed some typos and too long lines of code.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 138
diff changeset
   147
  return True
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   148
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   149
def isIdDeveloper(id=None):
164
afdf502c6cc4 Documentation updates and one typo fix.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
   150
  """Returns True if a Google Account is a Developer with special privileges.
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   151
  
138
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   152
  Since it only works on the current logged-in user, if id matches the
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   153
  current logged-in Google Account, the App Engine Users API function
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   154
  user.is_current_user_admin() is checked.  If that returns False, or
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   155
  id is not the currently logged-in user, the is_developer property of
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   156
  the User entity corresponding to the id Google Account is checked next.
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   157
  
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   158
  This solves the "chicken-and-egg" problem of no User entity having its
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
   159
  is_developer property set, but no one being able to set it.
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   160
  
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   161
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   162
    id: a Google Account (users.User) object; if id is not supplied,
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   163
      the current logged-in user is checked
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   164
  """
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   165
  id = getIdIfMissing(id)
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   166
 
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   167
  if not id:
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   168
    # no Google Account was supplied or is logged in, so an unspecified
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   169
    # User is definitely *not* a Developer
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   170
    return False
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   171
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   172
  if id == users.get_current_user():
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   173
    if users.is_current_user_admin():
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   174
      # supplied id is current logged-in user, and that user is in the
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   175
      # Administration->Developers list in the App Engine console
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   176
      return True
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   177
  
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   178
  user = getUserFromId(id)
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   179
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   180
  if not user:
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   181
    # no User entity for this Google Account, and id is not the currently
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   182
    # logged-in user, so there is no conclusive way to check the
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   183
    # Administration->Developers list in the App Engine console
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   184
    return False
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   185
  
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   186
  return user.is_developer
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   187
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   188
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   189
LINKNAME_PATTERN = r'''(?x)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   190
    ^
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   191
    [0-9a-z]   # start with ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   192
    (
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   193
     [0-9a-z]  # additional ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   194
     |         # -OR-
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   195
     _[0-9a-z] # underscore and ASCII digit or lowercase
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   196
    )*         # zero or more of OR group
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   197
    $
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   198
'''
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   199
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   200
LINKNAME_REGEX = re.compile(LINKNAME_PATTERN)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   201
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   202
def isLinkNameFormatValid(link_name):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   203
  """Returns True if link_name is in a valid format.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   204
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   205
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   206
    link_name: link name used in URLs to identify user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   207
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   208
  if LINKNAME_REGEX.match(link_name):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   209
    return True
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   210
  return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   211
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   212
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
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
   214
  """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
   215
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
    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
   218
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
  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
   220
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   221
def getUserFromKeyName(key_name):
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   222
  """Returns User entity for key_name or None if not found.
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   223
    
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   224
  Args:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   225
    key_name: key name of User entity
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   226
  """
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   227
  return soc.models.user.User.get_by_key_name(key_name)
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
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
   230
  """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
   231
  
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
    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
   234
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
  Returns:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
    * 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
   237
    * 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
   238
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   239
  Raises:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   240
    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
   241
    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
   242
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   243
  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
   244
    # 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
   245
    return None
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   246
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   247
  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
   248
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   249
  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
   250
    # 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
   251
    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
   252
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   253
  # 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
   254
  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
   255
      'There is no user with a "link name" of "%s".' % link_name, status=404)
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   256
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   257
136
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   258
def isLinkNameAvailableForId(link_name, id=None):
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   259
  """Indicates if link name is available for the given Google Account.
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   260
  
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   261
  Args:
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   262
    link_name: link name used in URLs to identify user
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   263
    id: a Google Account object; optional, current logged-in user will
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   264
      be used (or False will be returned if no user is logged in)
136
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   265
      
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   266
  Returns:
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   267
    True: the link name does not exist in the Datastore,
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   268
      so it is currently "available" to any User
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   269
    True: the link name exists and already belongs to the User entity
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   270
      associated with the specified Google Account
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   271
    False: the link name exists and belongs to a User entity other than
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   272
      that associated with the supplied Google Account
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   273
  """
136
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   274
  link_name_exists = doesLinkNameExist(link_name)
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   275
 
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   276
  if not link_name_exists:
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   277
    # if the link name does not exist, it is clearly available for any User
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   278
    return True
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   279
136
a95f511bfcf8 Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents: 135
diff changeset
   280
  return doesLinkNameBelongToId(link_name, id=id)
135
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   281
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   282
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   283
def doesLinkNameExist(link_name=None):
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   284
  """Returns True if link name exists in the Datastore.
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   285
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   286
  Args:
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   287
    link_name: link name used in URLs to identify user
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   288
  """
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   289
  if getUserFromLinkName(link_name):
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   290
    return True
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   291
  else:
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   292
    return False
a7ccde9d9eed Fixed one typo in response_helpers which caused is_admin context variable not to work correctly.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 131
diff changeset
   293
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   294
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   295
def doesLinkNameBelongToId(link_name, id=None):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   296
  """Returns True if supplied link name belongs to supplied Google Account.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   297
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   298
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   299
    link_name: link name used in URLs to identify user
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   300
    id: a Google Account object; optional, current logged-in user will
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   301
      be used (or False will be returned if no user is logged in)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   302
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   303
  id = getIdIfMissing(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   304
    
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   305
  if not id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   306
    # id not supplied and no Google Account logged in, so link name cannot
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   307
    # belong to an unspecified User
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   308
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   309
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   310
  user = getUserFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   311
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   312
  if not user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   313
    # no User corresponding to id Google Account, so no link name at all 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   314
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   315
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   316
  if user.link_name != link_name:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   317
    # User exists for id, but does not have this link name
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   318
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   319
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   320
  return True  # link_name does actually belong to this Google Account
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   321
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   322
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   323
def updateOrCreateUserFromId(id, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   324
  """Update existing User entity, or create new one with supplied properties.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   325
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   326
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   327
    id: a Google Account object
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   328
    **user_properties: keyword arguments that correspond to User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   329
      properties and their values
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   330
      
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   331
  Returns:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   332
    the User entity corresponding to the Google Account, with any supplied
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   333
    properties changed, or a new User entity now associated with the Google
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   334
    Account and with the supplied properties
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   335
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   336
  # attempt to retrieve the existing User
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   337
  user = getUserFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   338
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   339
  if not user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   340
    # user did not exist, so create one in a transaction
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   341
    key_name = getUserKeyNameFromId(id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   342
    user = soc.models.user.User.get_or_insert(
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   343
      key_name, id=id, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   344
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   345
  # 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
   346
  # got an existing one due to a race, so update with user_properties anyway,
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   347
  # in a transaction
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   348
  return updateUserProperties(user, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   349
184
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   350
def updateUserForKeyName(key_name, **user_properties):
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   351
  """Update existing User entity for keyname with supplied properties.
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   352
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   353
  Args:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   354
    key_name: key name of User entity
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   355
    **user_properties: keyword arguments that correspond to User entity
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   356
      properties and their values
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   357
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   358
  Returns:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   359
    the User entity corresponding to the Google Account, with any supplied
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   360
    properties changed, or a new User entity now associated with the Google
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   361
    Account and with the supplied properties
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   362
  """
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   363
  # attempt to retrieve the existing User
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   364
  user = getUserFromKeyName(key_name)
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   365
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   366
  if not user:
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   367
    return None
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   368
  
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   369
  # there is no way to be sure if get_or_insert() returned a new User or
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   370
  # got an existing one due to a race, so update with user_properties anyway,
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   371
  # in a transaction
7c0b42aecd9b Add support for changing User id (Google Account email) in User Profile Developer view. Now user profile developer edit view includes hidden key_name field. Fix typo in user/profile.py. Show former user ids in lookup and edit User Profile Developer views.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 166
diff changeset
   372
  return updateUserProperties(user, **user_properties)
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   373
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   374
def updateUserProperties(user, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   375
  """Update existing User entity using supplied User properties.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   376
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   377
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   378
    user: a User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   379
    **user_properties: keyword arguments that correspond to User entity
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   380
      properties and their values
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   381
      
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   382
  Returns:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   383
    the original User entity with any supplied properties changed 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   384
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   385
  def update():
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   386
    return _unsafeUpdateUserProperties(user, **user_properties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   387
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   388
  return db.run_in_transaction(update)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   389
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   390
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   391
def _unsafeUpdateUserProperties(user, **user_properties):
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   392
  """(see updateUserProperties)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   393
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   394
  Like updateUserProperties(), but not run within a transaction. 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   395
  """
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   396
  properties = user.properties()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   397
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   398
  for prop in properties.values():
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   399
    if prop.name in user_properties:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   400
      if prop.name == 'former_ids':
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   401
        # former_ids cannot be overwritten directly
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   402
        continue
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   403
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   404
      value = user_properties[prop.name]
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   405
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   406
      if prop.name == 'id':
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   407
        old_id = user.id
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   408
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   409
        if value != old_id:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   410
          user.former_ids.append(old_id)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   411
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   412
      prop.__set__(user, value)
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   413
        
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   414
  user.put()
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   415
  return user