| # Copyright 2010 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. |
| |
| """Utils for manipulating profile data.""" |
| |
| import datetime |
| import json |
| import os |
| |
| from oauth2client import appengine |
| from oauth2client import client |
| |
| from google.appengine.api import users |
| |
| from datetime import timedelta |
| |
| |
| DEFAULT_EMAIL = 'test@example.com' |
| |
| DEFAULT_MAX_AGE = 100 |
| DEFAULT_MIN_AGE = 0 |
| |
| def generateEligibleStudentBirthDate(program): |
| min_age = program.student_min_age or DEFAULT_MIN_AGE |
| max_age = program.student_max_age or DEFAULT_MAX_AGE |
| eligible_age = (min_age + max_age) / 2 |
| return datetime.datetime.date( |
| datetime.datetime.today() - timedelta(days=eligible_age * 365)) |
| |
| |
| def generateIneligibleStudentBirthDate(program): |
| if program.student_min_age: |
| days = (program.student_min_age - 1) * 365 |
| return datetime.datetime.date( |
| datetime.datetime.today() - timedelta(days=days)) |
| elif program.student_max_age: |
| days = (program.student_max_age + 1) * 365 |
| return datetime.datetime.date( |
| datetime.datetime.today() - timedelta(days=days)) |
| else: |
| raise ValueError('Neither min age nor max age is defined for the program.') |
| |
| |
| # TODO(daniel): Change name to login and remove the function above |
| def loginNDB(user, is_admin=False): |
| """Logs in the specified user by setting 'USER_ID' environmental variables. |
| |
| Args: |
| user: user entity. |
| is_admin: A bool specifying whether the user is an administrator |
| of the application or not. |
| """ |
| os.environ['USER_ID'] = user.account_id |
| os.environ['USER_IS_ADMIN'] = '1' if is_admin else '0' |
| |
| |
| class FakeCredentials(client.Credentials): |
| """Minimal implementation of the Credentials class. |
| |
| When stored in the datastore as the credentials property on a |
| appengine.CredentialsModel entity, the oauth2client library will consider |
| the user to be logged in. |
| """ |
| |
| def __init__(self, invalid, access_token, client_id): |
| self.invalid = invalid |
| self.access_token = access_token |
| self.client_id = client_id |
| |
| def authorize(self, http): |
| pass |
| |
| @classmethod |
| def from_json(cls, s): |
| data = json.loads(s) |
| invalid = data['invalid'] |
| access_token = data['access_token'] |
| client_id = data['client_id'] |
| return FakeCredentials(invalid, access_token, client_id) |
| |
| |
| def oauthAuthorizeNDB(valid=True, access_token=None, client_id=None): |
| """Creates oauth credentials for the currently logged in user. |
| """ |
| user = users.get_current_user() |
| credentials = FakeCredentials( |
| invalid=not valid, access_token=access_token, client_id=client_id) |
| credentials_model = appengine.CredentialsModel.get_or_insert(user.user_id()) |
| credentials_model.credentials = credentials |
| credentials_model.put() |
| |
| |
| def logout(): |
| """Logs out the current user by clearing the 'USER_EMAIL' |
| and 'USER_ID' environment variables. |
| """ |
| del os.environ['USER_EMAIL'] |
| del os.environ['USER_ID'] |
| |
| |
| def signInToGoogleAccount(email, user_id=None): |
| """Signs in an email address for the account that is logged in by setting |
| 'USER_EMAIL' and 'USER_ID' environmental variables. |
| |
| The Google account associated with the specified email will be considered |
| currently logged in, after this function terminates. |
| |
| Args: |
| email: the user email as a string, e.g.: 'test@example.com' |
| user_id: the user id as a string |
| """ |
| os.environ['USER_EMAIL'] = email |
| os.environ['USER_ID'] = user_id or '' |