42 return None |
42 return None |
43 |
43 |
44 return 'User:%s' % id.email() |
44 return 'User:%s' % id.email() |
45 |
45 |
46 |
46 |
47 def getIdIfMissing(id): |
47 def getIdIfMissing(id=None): |
48 """Gets Google Account of logged-in user (possibly None) if id is false. |
48 """Gets Google Account of logged-in user (possibly None) if id is false. |
49 |
49 |
50 This is a convenience function that simplifies a lot of view code that |
50 This is a convenience function that simplifies a lot of view code that |
51 accepts an optional id argument from the caller (such as one looked up |
51 accepts an optional id argument from the caller (such as one looked up |
52 already by another view that decides to "forward" the request to this |
52 already by another view that decides to "forward" the request to this |
64 # id not initialized, so check if a Google Account is currently logged in |
64 # id not initialized, so check if a Google Account is currently logged in |
65 id = users.get_current_user() |
65 id = users.get_current_user() |
66 |
66 |
67 return id |
67 return id |
68 |
68 |
|
69 def getUsersForOffsetAndLimit(offset=0, limit=0): |
|
70 """Returns Users entities for given offset and limit or None if not found. |
|
71 |
|
72 Args: |
|
73 offset: offset in entities list which defines first entity to return |
|
74 limit: max amount of entities to return |
|
75 """ |
|
76 query = db.GqlQuery('SELECT * FROM User ORDER BY id') |
|
77 |
|
78 # Fetch one more to see if there should be a 'next' link |
|
79 return query.fetch(limit+1, offset) |
69 |
80 |
70 def getUserFromId(id): |
81 def getUserFromId(id): |
71 """Returns User entity for a Google Account, or None if not found. |
82 """Returns User entity for a Google Account, or None if not found. |
72 |
83 |
73 Args: |
84 Args: |
74 id: a Google Account (users.User) object |
85 id: a Google Account (users.User) object |
75 """ |
86 """ |
76 # first, attempt a lookup by User:id key name |
87 return soc.models.user.User.gql('WHERE id = :1', id).get() |
77 key_name = getUserKeyNameFromId(id) |
88 |
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 |
|
86 |
|
87 # email address may have changed, so query the id property |
|
88 user = soc.models.user.User.gql('WHERE id = :1', id).get() |
|
89 |
|
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 |
|
99 def getUserIfMissing(user, id): |
89 def getUserIfMissing(user, id): |
100 """Conditionally returns User entity for a Google Account. |
90 """Conditionally returns User entity for a Google Account. |
101 |
91 |
102 This function is used to look up the User entity corresponding to the |
92 This function is used to look up the User entity corresponding to the |
103 supplied Google Account *if* the user parameter is false (usually None). |
93 supplied Google Account *if* the user parameter is false (usually None). |
225 Args: |
215 Args: |
226 link_name: link name used in URLs to identify user |
216 link_name: link name used in URLs to identify user |
227 """ |
217 """ |
228 return soc.models.user.User.gql('WHERE link_name = :1', link_name).get() |
218 return soc.models.user.User.gql('WHERE link_name = :1', link_name).get() |
229 |
219 |
|
220 def getUserFromKeyName(key_name): |
|
221 """Returns User entity for key_name or None if not found. |
|
222 |
|
223 Args: |
|
224 key_name: key name of User entity |
|
225 """ |
|
226 return soc.models.user.User.get_by_key_name(key_name) |
230 |
227 |
231 def getUserIfLinkName(link_name): |
228 def getUserIfLinkName(link_name): |
232 """Returns User entity for supplied link_name if one exists. |
229 """Returns User entity for supplied link_name if one exists. |
233 |
230 |
234 Args: |
231 Args: |
347 # there is no way to be sure if get_or_insert() returned a new User or |
344 # 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, |
345 # got an existing one due to a race, so update with user_properties anyway, |
349 # in a transaction |
346 # in a transaction |
350 return updateUserProperties(user, **user_properties) |
347 return updateUserProperties(user, **user_properties) |
351 |
348 |
|
349 def updateUserForKeyName(key_name, **user_properties): |
|
350 """Update existing User entity for keyname with supplied properties. |
|
351 |
|
352 Args: |
|
353 key_name: key name of User entity |
|
354 **user_properties: keyword arguments that correspond to User entity |
|
355 properties and their values |
|
356 |
|
357 Returns: |
|
358 the User entity corresponding to the Google Account, with any supplied |
|
359 properties changed, or a new User entity now associated with the Google |
|
360 Account and with the supplied properties |
|
361 """ |
|
362 # attempt to retrieve the existing User |
|
363 user = getUserFromKeyName(key_name) |
|
364 |
|
365 if not user: |
|
366 return None |
|
367 |
|
368 # there is no way to be sure if get_or_insert() returned a new User or |
|
369 # got an existing one due to a race, so update with user_properties anyway, |
|
370 # in a transaction |
|
371 return updateUserProperties(user, **user_properties) |
352 |
372 |
353 def updateUserProperties(user, **user_properties): |
373 def updateUserProperties(user, **user_properties): |
354 """Update existing User entity using supplied User properties. |
374 """Update existing User entity using supplied User properties. |
355 |
375 |
356 Args: |
376 Args: |