<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Basic ID (Google Account) and User (Model) query functions.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


import re
import logging

from google.appengine.api import users
from google.appengine.ext import db

from soc.logic import out_of_band

import soc.models.user


def getUserKeyNameFromId(id):
  """Return a Datastore key_name for a User derived from a Google Account.
  
  Args:
    id: a Google Account (users.User) object
  """
  if not id:
    return None

  return 'User:%s' % id.email()


def getIdIfMissing(id):
  """Gets Google Account of logged-in user (possibly None) if id is false.
  
  This is a convenience function that simplifies a lot of view code that
  accepts an optional id argument from the caller (such as one looked up
  already by another view that decides to "forward" the request to this
  other view).

  Args:
    id: a Google Account (users.User) object, or None
    
  Returns:
    If id is non-false, it is simply returned; otherwise, the Google Account
    of currently logged-in user is returned (which could be None if no user
    is logged in).
  """
  if not id:
    # id not initialized, so check if a Google Account is currently logged in
    id = users.get_current_user()

  return id


def getUserFromId(id):
  """Returns User entity for a Google Account, or None if not found.  
    
  Args:
    id: a Google Account (users.User) object
  """
  # first, attempt a lookup by User:id key name
  key_name = getUserKeyNameFromId(id)
  
  if key_name:
    user = soc.models.user.User.get_by_key_name(key_name)
  else:
    user = None
  
  if user:
    return user

  # email address may have changed, so query the id property
  user = soc.models.user.User.gql('WHERE id = :1', id).get()

  if user:
    return user

  # last chance: perhaps the User changed their email address at some point
  user = soc.models.user.User.gql('WHERE former_ids = :1', id).get()

  return user

  
def getUserIfMissing(user, id):
  """Conditionally returns User entity for a Google Account.
  
  This function is used to look up the User entity corresponding to the
  supplied Google Account *if* the user parameter is false (usually None).
  This function is basically a no-op if user already refers to a User
  entity.  This is a convenience function that simplifies a lot of view
  code that accepts an optional user argument from the caller (such as
  one looked up already by another view that decides to "forward" the
  HTTP request to this other view).

  Args:
    user: None (usually), or an existing User entity
    id: a Google Account (users.User) object
    
  Returns:
    * user (which may have already been None if passed in that way by the
      caller) if id is false or user is non-false
    * results of getUserFromId() if user is false and id is non-false
  """
  if id and (not user):
    # Google Account supplied and User uninitialized, so look up User entity
    user = getUserFromId(id)
    
  return user


def doesUserExist(id):
  """Returns True if User exists in the Datastore for a Google Account.
    
  Args:
    id: a Google Account object
  """
  if getUserFromId(id):
    return True
  else:
    return False


def isIdDeveloper(id=None):
  """Returns True if Google Account is a Developer with special privileges.
  
  using the App Engine Users API.
  
  Args:
    id: a Google Account (users.User) object; if id is not supplied,
      the current logged-in user is checked
  """
  id = getIdIfMissing(id)
 
  if not id:
    # no Google Account was supplied or is logged in, so an unspecified
    # User is definitely *not* a Developer
    return False
<commit_msg>Improve the __doc__ string of isIdDeveloper() to explain better how it now functions.<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Basic ID (Google Account) and User (Model) query functions.
"""

__authors__ = [
  '"<NAME>" <<EMAIL>>',
  ]


import re
import logging

from google.appengine.api import users
from google.appengine.ext import db

from soc.logic import out_of_band

import soc.models.user


def getUserKeyNameFromId(id):
  """Return a Datastore key_name for a User derived from a Google Account.
  
  Args:
    id: a Google Account (users.User) object
  """
  if not id:
    return None

  return 'User:%s' % id.email()


def getIdIfMissing(id):
  """Gets Google Account of logged-in user (possibly None) if id is false.
  
  This is a convenience function that simplifies a lot of view code that
  accepts an optional id argument from the caller (such as one looked up
  already by another view that decides to "forward" the request to this
  other view).

  Args:
    id: a Google Account (users.User) object, or None
    
  Returns:
    If id is non-false, it is simply returned; otherwise, the Google Account
    of currently logged-in user is returned (which could be None if no user
    is logged in).
  """
  if not id:
    # id not initialized, so check if a Google Account is currently logged in
    id = users.get_current_user()

  return id


def getUserFromId(id):
  """Returns User entity for a Google Account, or None if not found.  
    
  Args:
    id: a Google Account (users.User) object
  """
  # first, attempt a lookup by User:id key name
  key_name = getUserKeyNameFromId(id)
  
  if key_name:
    user = soc.models.user.User.get_by_key_name(key_name)
  else:
    user = None
  
  if user:
    return user

  # email address may have changed, so query the id property
  user = soc.models.user.User.gql('WHERE id = :1', id).get()

  if user:
    return user

  # last chance: perhaps the User changed their email address at some point
  user = soc.models.user.User.gql('WHERE former_ids = :1', id).get()

  return user

  
def getUserIfMissing(user, id):
  """Conditionally returns User entity for a Google Account.
  
  This function is used to look up the User entity corresponding to the
  supplied Google Account *if* the user parameter is false (usually None).
  This function is basically a no-op if user already refers to a User
  entity.  This is a convenience function that simplifies a lot of view
  code that accepts an optional user argument from the caller (such as
  one looked up already by another view that decides to "forward" the
  HTTP request to this other view).

  Args:
    user: None (usually), or an existing User entity
    id: a Google Account (users.User) object
    
  Returns:
    * user (which may have already been None if passed in that way by the
      caller) if id is false or user is non-false
    * results of getUserFromId() if user is false and id is non-false
  """
  if id and (not user):
    # Google Account supplied and User uninitialized, so look up User entity
    user = getUserFromId(id)
    
  return user


def doesUserExist(id):
  """Returns True if User exists in the Datastore for a Google Account.
    
  Args:
    id: a Google Account object
  """
  if getUserFromId(id):
    return True
  else:
    return False


def isIdDeveloper(id=None):
  """Returns True if Google Account is a Developer with special privileges.
  
  Since it only works on the current logged-in user, if id matches the
  current logged-in Google Account, the App Engine Users API function
  user.is_current_user_admin() is checked.  If that returns False, or
  id is not the currently logged-in user, the is_developer property of
  the User entity corresponding to the id Google Account is checked next.
  
  This solves the "chicken-and-egg" problem of no User entity having its
  is_developer property set, but no one being able to set it.
  
  Args:
    id: a Google Account (users.User) object; if id is not supplied,
      the current logged-in user is checked
  """
  id = getIdIfMissing(id)
 
  if not id:
    # no Google Account was supplied or is logged in, so an unspecified
    # User is definitely *not* a Developer
    return False

{'fraction_non_alphanumeric': 0.06989604069896041, 'fraction_numerical': 0.0026542800265428003, 'mean_word_length': 3.285781990521327, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3490259', 'n_tokens_mistral': 2634, 'n_tokens_neox': 2560, 'n_words': 1250}