author | Todd Larsen <tlarsen@google.com> |
Fri, 12 Sep 2008 20:36:17 +0000 | |
changeset 138 | e1167bdf71a4 |
parent 137 | 0f572149449d |
child 141 | e120c24b89e2 |
permissions | -rw-r--r-- |
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 | 25 |
import re |
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
|
26 |
import logging |
131 | 27 |
|
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
28 |
from google.appengine.api import users |
131 | 29 |
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
|
30 |
|
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 | 36 |
def getUserKeyNameFromId(id): |
37 |
"""Return a Datastore key_name for a User derived from a Google Account. |
|
38 |
||
39 |
Args: |
|
40 |
id: a Google Account (users.User) object |
|
41 |
""" |
|
42 |
if not id: |
|
43 |
return None |
|
44 |
||
45 |
return 'User:%s' % id.email() |
|
46 |
||
47 |
||
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
48 |
def getIdIfMissing(id): |
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 | 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 |
|
131 | 70 |
|
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
71 |
def getUserFromId(id): |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
72 |
"""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
|
73 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
74 |
Args: |
131 | 75 |
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
|
76 |
""" |
131 | 77 |
# first, attempt a lookup by User:id key name |
78 |
key_name = getUserKeyNameFromId(id) |
|
79 |
||
80 |
if key_name: |
|
81 |
user = soc.models.user.User.get_by_key_name(key_name) |
|
82 |
else: |
|
83 |
user = None |
|
84 |
||
85 |
if user: |
|
86 |
return user |
|
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
87 |
|
131 | 88 |
# email address may have changed, so query the id property |
89 |
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
|
90 |
|
131 | 91 |
if user: |
92 |
return user |
|
93 |
||
94 |
# last chance: perhaps the User changed their email address at some point |
|
95 |
user = soc.models.user.User.gql('WHERE former_ids = :1', id).get() |
|
96 |
||
97 |
return user |
|
98 |
||
99 |
||
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
100 |
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
|
101 |
"""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
|
102 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
111 |
Args: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
112 |
user: None (usually), or an existing User entity |
131 | 113 |
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
|
114 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
115 |
Returns: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
116 |
* 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
|
117 |
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
|
118 |
* 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
|
119 |
""" |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
120 |
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
|
121 |
# 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
|
122 |
user = getUserFromId(id) |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
123 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
124 |
return user |
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 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
127 |
def doesUserExist(id): |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
128 |
"""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
|
129 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
130 |
Args: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
131 |
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
|
132 |
""" |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
133 |
if getUserFromId(id): |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
134 |
return True |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
135 |
else: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
136 |
return False |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
137 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
138 |
|
131 | 139 |
def isIdDeveloper(id=None): |
140 |
"""Returns True if Google Account is a Developer with special privileges. |
|
141 |
||
138
e1167bdf71a4
Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents:
137
diff
changeset
|
142 |
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
|
143 |
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
|
144 |
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
|
145 |
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
|
146 |
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
|
147 |
|
e1167bdf71a4
Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents:
137
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
|
131 | 151 |
Args: |
152 |
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
|
153 |
the current logged-in user is checked |
131 | 154 |
""" |
137
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
155 |
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
|
156 |
|
131 | 157 |
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
|
158 |
# 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
|
159 |
# 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
|
160 |
return False |
131 | 161 |
|
137
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
162 |
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
|
163 |
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
|
164 |
# 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
|
165 |
# 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
|
166 |
return True |
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
167 |
|
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
168 |
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
|
169 |
|
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
170 |
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
|
171 |
# 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
|
172 |
# 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
|
173 |
# 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
|
174 |
return False |
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
175 |
|
0f572149449d
Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents:
136
diff
changeset
|
176 |
return user.is_developer |
131 | 177 |
|
178 |
||
179 |
LINKNAME_PATTERN = r'''(?x) |
|
180 |
^ |
|
181 |
[0-9a-z] # start with ASCII digit or lowercase |
|
182 |
( |
|
183 |
[0-9a-z] # additional ASCII digit or lowercase |
|
184 |
| # -OR- |
|
185 |
_[0-9a-z] # underscore and ASCII digit or lowercase |
|
186 |
)* # zero or more of OR group |
|
187 |
$ |
|
188 |
''' |
|
189 |
||
190 |
LINKNAME_REGEX = re.compile(LINKNAME_PATTERN) |
|
191 |
||
192 |
def isLinkNameFormatValid(link_name): |
|
193 |
"""Returns True if link_name is in a valid format. |
|
194 |
||
195 |
Args: |
|
196 |
link_name: link name used in URLs to identify user |
|
197 |
""" |
|
198 |
if LINKNAME_REGEX.match(link_name): |
|
199 |
return True |
|
200 |
return False |
|
201 |
||
202 |
||
112
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
203 |
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
|
204 |
"""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
|
205 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
206 |
Args: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
207 |
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
|
208 |
""" |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
209 |
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
|
210 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
211 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
212 |
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
|
213 |
"""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
|
214 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
215 |
Args: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
216 |
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
|
217 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
218 |
Returns: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
219 |
* 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
|
220 |
* 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
|
221 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
222 |
Raises: |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
223 |
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
|
224 |
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
|
225 |
""" |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
226 |
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
|
227 |
# 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
|
228 |
return None |
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
229 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
230 |
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
|
231 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
232 |
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
|
233 |
# 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
|
234 |
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
|
235 |
|
4d9895fb15bc
Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff
changeset
|
236 |
# 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
|
237 |
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
|
238 |
'There is no user with a "link name" of "%s".' % link_name, status=404) |
131 | 239 |
|
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
|
240 |
|
136
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
241 |
def isLinkNameAvailableForId(link_name, id=None): |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
242 |
"""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
|
243 |
|
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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
|
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
249 |
Returns: |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
250 |
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
|
251 |
so it is currently "available" to any User |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
252 |
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
|
253 |
associated with the specified Google Account |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
""" |
136
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
257 |
link_name_exists = doesLinkNameExist(link_name) |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
258 |
|
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
259 |
if not link_name_exists: |
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
260 |
# 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
|
261 |
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
|
262 |
|
136
a95f511bfcf8
Replace checkLinkNameForId() with isLinkNameAvailableForId(), and implement
Todd Larsen <tlarsen@google.com>
parents:
135
diff
changeset
|
263 |
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
|
264 |
|
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
|
265 |
|
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
|
266 |
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
|
267 |
"""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
|
268 |
|
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
|
269 |
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
|
270 |
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
|
271 |
""" |
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
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
|
131 | 277 |
|
278 |
def doesLinkNameBelongToId(link_name, id=None): |
|
279 |
"""Returns True if supplied link name belongs to supplied Google Account. |
|
280 |
||
281 |
Args: |
|
282 |
link_name: link name used in URLs to identify user |
|
283 |
id: a Google Account object; optional, current logged-in user will |
|
284 |
be used (or False will be returned if no user is logged in) |
|
285 |
""" |
|
286 |
id = getIdIfMissing(id) |
|
287 |
||
288 |
if not id: |
|
289 |
# id not supplied and no Google Account logged in, so link name cannot |
|
290 |
# belong to an unspecified User |
|
291 |
return False |
|
292 |
||
293 |
user = getUserFromId(id) |
|
294 |
||
295 |
if not user: |
|
296 |
# no User corresponding to id Google Account, so no link name at all |
|
297 |
return False |
|
298 |
||
299 |
if user.link_name != link_name: |
|
300 |
# User exists for id, but does not have this link name |
|
301 |
return False |
|
302 |
||
303 |
return True # link_name does actually belong to this Google Account |
|
304 |
||
305 |
||
306 |
def updateOrCreateUserFromId(id, **user_properties): |
|
307 |
"""Update existing User entity, or create new one with supplied properties. |
|
308 |
||
309 |
Args: |
|
310 |
id: a Google Account object |
|
311 |
**user_properties: keyword arguments that correspond to User entity |
|
312 |
properties and their values |
|
313 |
||
314 |
Returns: |
|
315 |
the User entity corresponding to the Google Account, with any supplied |
|
316 |
properties changed, or a new User entity now associated with the Google |
|
317 |
Account and with the supplied properties |
|
318 |
""" |
|
319 |
# attempt to retrieve the existing User |
|
320 |
user = getUserFromId(id) |
|
321 |
||
322 |
if not user: |
|
323 |
# user did not exist, so create one in a transaction |
|
324 |
key_name = getUserKeyNameFromId(id) |
|
325 |
user = soc.models.user.User.get_or_insert( |
|
326 |
key_name, id=id, **user_properties) |
|
327 |
||
328 |
# there is no way to be sure if get_or_insert() returned a new User or |
|
329 |
# got an existing one due to a race, so update with user_properties anyway, |
|
330 |
# in a transaction |
|
331 |
return updateUserProperties(user, **user_properties) |
|
332 |
||
333 |
||
334 |
def updateUserProperties(user, **user_properties): |
|
335 |
"""Update existing User entity using supplied User properties. |
|
336 |
||
337 |
Args: |
|
338 |
user: a User entity |
|
339 |
**user_properties: keyword arguments that correspond to User entity |
|
340 |
properties and their values |
|
341 |
||
342 |
Returns: |
|
343 |
the original User entity with any supplied properties changed |
|
344 |
""" |
|
345 |
def update(): |
|
346 |
return _unsafeUpdateUserProperties(user, **user_properties) |
|
347 |
||
348 |
return db.run_in_transaction(update) |
|
349 |
||
350 |
||
351 |
def _unsafeUpdateUserProperties(user, **user_properties): |
|
352 |
"""(see updateUserProperties) |
|
353 |
||
354 |
Like updateUserProperties(), but not run within a transaction. |
|
355 |
""" |
|
356 |
properties = user.properties() |
|
357 |
||
358 |
for prop in properties.values(): |
|
359 |
if prop.name in user_properties: |
|
360 |
if prop.name == 'former_ids': |
|
361 |
# former_ids cannot be overwritten directly |
|
362 |
continue |
|
363 |
||
364 |
value = user_properties[prop.name] |
|
365 |
||
366 |
if prop.name == 'id': |
|
367 |
old_id = user.id |
|
368 |
||
369 |
if value != old_id: |
|
370 |
user.former_ids.append(old_id) |
|
371 |
||
372 |
prop.__set__(user, value) |
|
373 |
||
374 |
user.put() |
|
375 |
return user |