blob: e544bd160fda6319f2b96278b44b689da585387f [file] [log] [blame]
# Copyright 2014 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.
"""Seeder functions for profile model."""
import datetime
from google.appengine.ext import ndb
from melange.models import address as address_model
from melange.models import connection as connection_model
from melange.models import contact as contact_model
from melange.models import profile as profile_model
from melange.utils import time as time_utils
from seeder import connection as connection_seeder
from seeder import education as education_seeder
from seeder import user as user_seeder
TEST_PUBLIC_NAME = 'Public Name'
TEST_FIRST_NAME = 'First'
TEST_LAST_NAME = 'Last'
TEST_STREET = 'Street'
TEST_CITY = 'City'
TEST_COUNTRY = 'United States'
TEST_POSTAL_CODE = '90000'
TEST_PROVINCE = 'California'
def seedProfile(program_key, model=profile_model.Profile,
user=None, mentor_for=None, admin_for=None, birth_date=None, **kwargs):
"""Seeds a new profile.
Args:
program_key: Program key for which the profile is seeded.
model: Model class of which a new profile should be seeded.
user: User entity corresponding to the profile.
mentor_for: List of organizations keys for which the profile should be
registered as a mentor.
admin_for: List of organizations keys for which the profile should be
registered as organization administrator.
birth_date: datetime.date object that defines birth date of the profile.
Returns:
A newly seeded Profile entity.
"""
user = user or user_seeder.seedUser()
mentor_for = mentor_for or []
admin_for = admin_for or []
residential_address = address_model.Address(
street=TEST_STREET, city=TEST_CITY, province=TEST_PROVINCE,
country=TEST_COUNTRY, postal_code=TEST_POSTAL_CODE)
properties = {'email': '%s@example.com' % user.user_id}
contact_properties = dict(
(k, v) for k, v in kwargs.iteritems()
if k in contact_model.Contact._properties)
properties.update(**contact_properties)
contact = contact_model.Contact(**properties)
birth_date = birth_date or datetime.date(1990, 1, 1)
age = time_utils.getAge(birth_date)
properties = {
'age': age,
'program': ndb.Key.from_old_key(program_key),
'status': profile_model.Status.ACTIVE,
'public_name': TEST_PUBLIC_NAME,
'first_name': TEST_FIRST_NAME,
'last_name': TEST_LAST_NAME,
'birth_date': birth_date,
'residential_address': residential_address,
'tee_style': profile_model.TeeStyle.MALE,
'tee_size': profile_model.TeeSize.M,
'program_knowledge': 'friend',
'mentor_for': list(set(mentor_for + admin_for)),
'admin_for': admin_for,
'contact': contact,
}
properties.update(**kwargs)
profile = model(id='%s/%s' % (program_key.name(), user.key.id()),
parent=user.key, **properties)
profile.put()
org_keys = list(set(mentor_for + admin_for))
for org_key in org_keys:
if org_key in admin_for:
org_role = connection_model.ORG_ADMIN_ROLE
else:
org_role = connection_model.MENTOR_ROLE
connection_properties = {
'user_role': connection_model.ROLE,
'org_role': org_role
}
connection_seeder.seedConnection(
profile.key, org_key, **connection_properties)
return profile
def seedStudentData(**kwargs):
"""Seeds a new student data.
Returns:
A newly seeded StudentData entity.
"""
properties = {'education': education_seeder.seedEducation()}
properties.update(**kwargs)
return profile_model.StudentData(**properties)
def seedStudent(program, student_data_properties=None, user=None, **kwargs):
"""Seeds a new profile that is registered as a student.
Args:
program: Program entity for which the profile is seeded.
student_data_properties: Optional properties of the student data to seed.
user: User entity corresponding to the profile.
Returns:
A newly seeded Profile entity.
"""
profile = seedProfile(program.key(), user=user, **kwargs)
student_data_properties = student_data_properties or {}
profile.student_data = seedStudentData(**student_data_properties)
profile.put()
return profile