app/soc/logic/site/id_user.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 12 Oct 2008 15:52:19 +0000
changeset 305 972d28056d9d
parent 301 5d6d106afb23
child 309 7190b224c701
permissions -rw-r--r--
Minor style and import fixes Incorperated changes as suggested by Todd and Pawel. Patch by: Sverre Rabbelier Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Basic ID (Google Account) and User (Model) query functions.
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    21
  '"Chen Lunpeng" <forever.clp@gmail.com>',
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  '"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
    23
  ]
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    26
import re
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    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
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    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
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    31
import soc.logic
202
b8b4a83788d4 A key_name controller module to collect all of the name...() functions that
Todd Larsen <tlarsen@google.com>
parents: 184
diff changeset
    32
from soc.logic import key_name
263
9b39d93b677f Make findNearestUsers() code in soc/logic/site/id_user.py more generic and
Todd Larsen <tlarsen@google.com>
parents: 262
diff changeset
    33
from soc.logic import model
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
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
    35
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
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
    37
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    39
def findNearestUsersOffset(width, id=None, link_name=None):
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    40
  """Finds offset of beginning of a range of Users around the nearest User.
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    41
  
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    42
  Args:
230
cb2c7ae5424d Fix typo in findNearestUsersOffset() __doc__ string missed in r661.
Todd Larsen <tlarsen@google.com>
parents: 229
diff changeset
    43
    width: the width of the "found" window around the nearest User found 
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    44
    id: a Google Account (users.User) object, or None
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    45
    link_name: link name input in the Lookup form or None if not supplied.
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    46
    
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    47
  Returns:
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    48
    an offset into the list of Users that is width/2 less than the
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    49
    offset of the first User returned by getNearestUsers(), or zero if
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    50
    that offset would be less than zero
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    51
      OR
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    52
    None if there are no nearest Users or the offset of the beginning of
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    53
    the range cannot be found for some reason 
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    54
  """
263
9b39d93b677f Make findNearestUsers() code in soc/logic/site/id_user.py more generic and
Todd Larsen <tlarsen@google.com>
parents: 262
diff changeset
    55
  return model.findNearestEntitiesOffset(
9b39d93b677f Make findNearestUsers() code in soc/logic/site/id_user.py more generic and
Todd Larsen <tlarsen@google.com>
parents: 262
diff changeset
    56
    width, soc.models.user.User, [('id', id), ('link_name', link_name)])
229
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    57
a46c238be8db Show link to /site/user/list on /site/user/lookup when User is not found.
Todd Larsen <tlarsen@google.com>
parents: 223
diff changeset
    58
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    59
def isIdDeveloper(id=None):
164
afdf502c6cc4 Documentation updates and one typo fix.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 141
diff changeset
    60
  """Returns True if a Google Account is a Developer with special privileges.
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    61
  
138
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    62
  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
    63
  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
    64
  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
    65
  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
    66
  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
    67
  
e1167bdf71a4 Improve the __doc__ string of isIdDeveloper() to explain better how it now
Todd Larsen <tlarsen@google.com>
parents: 137
diff changeset
    68
  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
    69
  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
    70
  
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    71
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    72
    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
    73
      the current logged-in user is checked
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    74
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    75
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    76
  # Get the currently logged in user
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    77
  current_id = users.get_current_user()
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    78
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    79
  if not (id or current_id):
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    80
    # 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
    81
    # 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
    82
    return False
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
    83
305
972d28056d9d Minor style and import fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 301
diff changeset
    84
  if ((not id) or (id == current_id)) and users.is_current_user_admin():
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    85
    # no id supplied, or current logged-in user, and that user is in the
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    86
    # Administration->Developers list in the App Engine console
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    87
    return True
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    88
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    89
  if not id:
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    90
    id = current_id
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
    91
301
5d6d106afb23 Fix not working isIdDeveloper() function after recent commit. There is still confusion within getFormFields method (email is not id), but this change doesn't address that problem.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 299
diff changeset
    92
  user = soc.logic.user_logic.getFromFields(email=id)
137
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    93
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    94
  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
    95
    # 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
    96
    # 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
    97
    # 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
    98
    return False
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
    99
  
0f572149449d Make isIdDeveloper() also able to check an is_developer Boolean property in
Todd Larsen <tlarsen@google.com>
parents: 136
diff changeset
   100
  return user.is_developer
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   101
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   102
262
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   103
def isIdAvailable(new_id, existing_user=None, existing_key_name=None):
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   104
  """Returns True if Google Account is available for use by existing User.
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   105
  
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   106
  Args:
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   107
    new_id: a Google Account (users.User) object with a (possibly) new email
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   108
    existing_user: an existing User entity; default is None, in which case
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   109
      existing_key_name is used to look up the User entity
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   110
    existing_key_name: the key_name of an existing User entity, used
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   111
      when existing_user is not supplied; default is None
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   112
  """
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   113
  if not existing_user:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   114
    existing_user = soc.logic.user_logic.getFromKeyName(existing_key_name)
262
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   115
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   116
  if existing_user:
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   117
    old_email = existing_user.id.email()
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   118
  else:
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   119
    old_email = None
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   120
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   121
  if new_id.email() == old_email:
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   122
    # "new" email is same as existing User wanting it, so it is "available"
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   123
    return True
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   124
  # else: "new" email truly is new to the existing User, so keep checking
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   125
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   126
  if not soc.logic.user_logic.getFromFields(id=new_id):
262
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   127
    # new email address also does not belong to any other User,
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   128
    # so it is available
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   129
    return True
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   130
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   131
  # email does not already belong to this User, but to some other User
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   132
  return False
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   133
52a42831d9d6 Factor out an isIdAvailable() function from EditForm.clean_id() in
Todd Larsen <tlarsen@google.com>
parents: 245
diff changeset
   134
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
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
   136
  """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
   137
    
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   138
  Args:
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   139
    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
   140
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   141
  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
   142
245
b14c2c4d3484 Moved isLinkNameFormatValid function out of id_user module to new common module. This function is going to be used by other form validation functions that require to validate linkname (different kind of Groups, Programs etc).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 230
diff changeset
   143
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   144
def getUserFromLinkNameOrDie(link_name):
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   145
  """Like getUserFromLinkName but expects to find a user
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
  Raises:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   148
    out_of_band.ErrorResponse if no User entity is found
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   149
  """
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   150
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   151
  user = getUserFromLinkName(link_name)
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   153
  if user:
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   154
    return user
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   155
112
4d9895fb15bc Consolidate functions dealing with Google Accounts and look-up of User entities
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   156
  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
   157
      'There is no user with a "link name" of "%s".' % link_name, status=404)
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   158
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
   159
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   160
def doesLinkNameBelongToId(link_name, id):
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   161
  """Returns True if supplied link name belongs to supplied Google Account.
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   162
  
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   163
  Args:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   164
    link_name: link name used in URLs to identify user
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   165
    id: a Google Account object
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   166
  """
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   167
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   168
  if not id:
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   169
    # link name cannot belong to an unspecified User
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   170
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   171
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   172
  user = soc.logic.user_logic.getFromFields(email=id.email())
131
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   173
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   174
  if not user:
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   175
    # no User corresponding to id Google Account, so no link name at all 
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   176
    return False
Todd Larsen <tlarsen@google.com>
parents: 112
diff changeset
   177
299
a1cc853a56e5 Refactor nearly all the soc.logic code to use the Base class
Sverre Rabbelier <srabbelier@gmail.com>
parents: 263
diff changeset
   178
  return user.link_name == link_name