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