3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Basic ID (Google Account) and User (Model) query functions.
21 '"Chen Lunpeng" <forever.clp@gmail.com>',
22 '"Todd Larsen" <tlarsen@google.com>',
26 from google
.appengine
.api
import users
28 from soc
.logic
import model
29 from soc
.logic
import models
30 from soc
.logic
import out_of_band
32 import soc
.models
.user
35 def findNearestUsersOffset(width
, id=None, link_name
=None):
36 """Finds offset of beginning of a range of Users around the nearest User.
39 width: the width of the "found" window around the nearest User found
40 id: a Google Account (users.User) object, or None
41 link_name: link name input in the Lookup form or None if not supplied.
44 an offset into the list of Users that is width/2 less than the
45 offset of the first User returned by getNearestUsers(), or zero if
46 that offset would be less than zero
48 None if there are no nearest Users or the offset of the beginning of
49 the range cannot be found for some reason
51 return model
.findNearestEntitiesOffset(
52 width
, soc
.models
.user
.User
, [('id', id), ('link_name', link_name
)])
55 def isIdDeveloper(id=None):
56 """Returns True if a Google Account is a Developer with special privileges.
58 Since it only works on the current logged-in user, if id matches the
59 current logged-in Google Account, the App Engine Users API function
60 user.is_current_user_admin() is checked. If that returns False, or
61 id is not the currently logged-in user, the is_developer property of
62 the User entity corresponding to the id Google Account is checked next.
64 This solves the "chicken-and-egg" problem of no User entity having its
65 is_developer property set, but no one being able to set it.
68 id: a Google Account (users.User) object; if id is not supplied,
69 the current logged-in user is checked
72 # Get the currently logged in user
73 current_id
= users
.get_current_user()
75 if not (id or current_id
):
76 # no Google Account was supplied or is logged in, so an unspecified
77 # User is definitely *not* a Developer
80 if ((not id) or (id == current_id
)) and users
.is_current_user_admin():
81 # no id supplied, or current logged-in user, and that user is in the
82 # Administration->Developers list in the App Engine console
88 user
= models
.user
.logic
.getFromFields(email
=id.email())
91 # no User entity for this Google Account, and id is not the currently
92 # logged-in user, so there is no conclusive way to check the
93 # Administration->Developers list in the App Engine console
96 return user
.is_developer
99 def isIdAvailable(new_id
, existing_user
=None, existing_key_name
=None):
100 """Returns True if Google Account is available for use by existing User.
103 new_id: a Google Account (users.User) object with a (possibly) new email
104 existing_user: an existing User entity; default is None, in which case
105 existing_key_name is used to look up the User entity
106 existing_key_name: the key_name of an existing User entity, used
107 when existing_user is not supplied; default is None
109 if not existing_user
:
110 existing_user
= models
.user
.logic
.getFromKeyName(existing_key_name
)
113 old_email
= existing_user
.id.email()
117 if new_id
.email() == old_email
:
118 # "new" email is same as existing User wanting it, so it is "available"
120 # else: "new" email truly is new to the existing User, so keep checking
122 if not models
.user
.logic
.getFromFields(id=new_id
):
123 # new email address also does not belong to any other User,
127 # email does not already belong to this User, but to some other User
131 def getUserFromLinkName(link_name
):
132 """Returns User entity for link_name or None if not found.
135 link_name: link name used in URLs to identify user
137 return soc
.models
.user
.User
.gql('WHERE link_name = :1', link_name
).get()
140 def getUserFromLinkNameOr404(link_name
):
141 """Like getUserFromLinkName but expects to find a user
144 out_of_band.ErrorResponse if no User entity is found
147 user
= getUserFromLinkName(link_name
)
152 raise out_of_band
.ErrorResponse(
153 'There is no user with a "link name" of "%s".' % link_name
, status
=404)
156 def doesLinkNameBelongToId(link_name
, id):
157 """Returns True if supplied link name belongs to supplied Google Account.
160 link_name: link name used in URLs to identify user
161 id: a Google Account object
165 # link name cannot belong to an unspecified User
168 user
= models
.user
.logic
.getFromFields(email
=id.email())
171 # no User corresponding to id Google Account, so no link name at all
174 return user
.link_name
== link_name