blob: 562c37d007986d39e43d66f3b84297a161c2ed92 [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 seeder import connection as connection_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, **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.
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)
properties = {
'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': datetime.date(1990, 1, 1),
'residential_address': residential_address,
'tee_style': profile_model.TeeStyle.MALE,
'tee_size': profile_model.TeeSize.M,
'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