blob: b68d60cdfe2bee6ad20f1cfb6d6f9aab826ba4f0 [file] [log] [blame]
# 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.
"""Seeds or clears the datastore.
"""
import os
import itertools
import datetime
from google.appengine.api import users
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext import ndb
from django import http
from melange.models import education
from melange.models import address
from melange.models import contact
from melange.models import profile as profile_model
from melange.models import user
from melange.models import organization as organization_model
from soc.logic import accounts
from soc.models.document import Document
from soc.models import program as program_model
from soc.models import org_app_survey as org_app_survey_model
from soc.models.site import Site
from soc.models.sponsor import Sponsor
from soc.models.survey import Survey
from soc.models.survey_record import SurveyRecord
from soc.modules.gci.models.organization import GCIOrganization
from soc.modules.gci.models.program import GCIProgram
from soc.modules.gci.models.score import GCIScore
from soc.modules.gci.models.timeline import GCITimeline
from soc.modules.gci.models.profile import GCIStudentInfo
from soc.modules.gci.models.task import DifficultyLevel
from soc.modules.gci.models.task import GCITask
from soc.modules.gsoc.models.profile import GSoCStudentInfo
from soc.modules.gsoc.models.proposal import GSoCProposal
from soc.modules.gsoc.models.project import GSoCProject
from soc.modules.gsoc.models.program import GSoCProgram
from soc.modules.gsoc.models.timeline import GSoCTimeline
from summerofcode.models import organization as soc_org_model
from summerofcode.models import profile as soc_profile
from summerofcode.models import project as project_model
from summerofcode.models import proposal as soc_proposal_model
def seed(request, *args, **kwargs):
"""Seeds the datastore with some default values.
"""
site_properties = {
'key_name': 'site',
'latest_gsoc': 'google/gsoc2014',
'latest_gci': 'google/gci2013',
}
site = Site(**site_properties)
site.put()
account = accounts.getCurrentAccount()
if not account:
# TODO(drewgottlieb): Figure out how to get dev server's user_id hash for a
# given email address. I obtained this hash by copying
# the user_id the dev server generates when I log in as
# this email.
account = users.User(
email='test@example.com', _user_id='185804764220139124118')
user_properties = {
'id': 'test',
'account_id': account.user_id(),
'account': account,
}
current_user = user.User(**user_properties)
current_user.put()
group_properties = {
'key_name': 'google',
'link_id': 'google',
'name': 'Google Inc.',
'short_name': 'Google',
'home_page': 'http://www.google.com',
'email': 'ospo@google.com',
'description': 'This is the profile for Google.',
'contact_street': 'Some Street',
'contact_city': 'Some City',
'contact_country': 'United States',
'contact_postalcode': '12345',
'phone': '15551110000',
'status': 'active',
}
google = Sponsor(**group_properties)
google.put()
# The seeded dates are more or less arbitrary, but are set up in such
# a way as to establish a reasonable default timeline. Dates should
# begin on the same day as the previous period's ending so that no
# gaps are created. Note that the timeline will rotate throughout the
# year since it starts relative to the current time, but this can
# changed by either changing the assignment of now or manually
# through the program admin page.
now = datetime.datetime.now()
week = datetime.timedelta(days=7)
month = datetime.timedelta(days=30)
timeline_properties = {
'key_name': 'google/gsoc2014',
'link_id': 'gsoc2014',
'scope': google,
# Org application period.
'program_start': now,
'accepted_organization_announced_deadline': now + week,
# Student application period; ~14 days.
'student_signup_start': now + week,
'student_signup_end': now + (week * 2),
'application_review_deadline': now + month,
# Community bonding period; ~30 days.
'accepted_students_announced_deadline' : now + month,
'student_application_matched_deadline': now + month,
'form_submission_start': now + month,
'bonding_start' : now + month,
'bonding_end' : now + (month * 2),
# Coding period; ~60 days.
'coding_start' : now + (month * 2),
'coding_end' : now + (month * 4),
# Mentor summit; 2 days, past the end of the coding period.
'mentor_summit_start' : now + (month * 4),
'mentor_summit_end' : now + (month * 4) + datetime.timedelta(2),
# Remainder of the offseason.
'program_end': now + (month * 4) + datetime.timedelta(3),
}
gsoc2014_timeline = GSoCTimeline(**timeline_properties)
gsoc2014_timeline.put()
program_properties = {
'key_name': 'google/gsoc2014',
'link_id': 'gsoc2014',
'program_id': 'gsoc2014',
'sponsor': google,
'scope': google,
'name': 'Google Summer of Code 2014',
'short_name': 'GSoC 2014',
'description': 'This is the program for GSoC 2014.',
'apps_tasks_limit': 42,
'slots': 42,
'timeline': gsoc2014_timeline,
'status': program_model.STATUS_VISIBLE,
}
gsoc2014 = GSoCProgram(**program_properties)
gsoc2014.put()
past = now - datetime.timedelta(365 * 4)
timeline_properties = {
'key_name': 'google/gsoc2010',
'link_id': 'gsoc2010',
'scope': google,
# Org application period.
'program_start': past,
'accepted_organization_announced_deadline': past + week,
# Student application period; ~20 days.
'student_signup_start': past + week,
'student_signup_end': past + (week * 2),
'application_review_deadline': past + month,
# Community bonding period; ~30 days.
'accepted_students_announced_deadline' : past + month,
'student_application_matched_deadline': past + month,
'form_submission_start': past + month,
'bonding_start' : past + month,
'bonding_end' : past + (month * 2),
# Coding period; ~60 days.
'coding_start' : past + (month * 2),
'coding_end' : past + (month * 4),
# Mentor summit; 2 days, past the end of the coding period.
'mentor_summit_start' : past + (month * 4),
'mentor_summit_end' : past + (month * 4) + datetime.timedelta(2),
# Remainder of the offseason.
'program_end': past + (month * 4) + datetime.timedelta(3),
}
gsoc2010_timeline = GSoCTimeline(**timeline_properties)
gsoc2010_timeline.put()
program_properties.update({
'key_name': 'google/gsoc2010',
'link_id': 'gsoc2010',
'program_id': 'gsoc2010',
'name': 'Google Summer of Code 2010',
'description': 'This is the program for GSoC 2010.',
'short_name': 'GSoC 2010',
'timeline': gsoc2010_timeline,
})
gsoc2010 = GSoCProgram(**program_properties)
gsoc2010.put()
# TODO(drew): Replace gsoc2014.prefix with whatever its replacement becomes
# once prefix is removed from program and no longer used in the query for
# OrgAppSurvey in soc.views.helper.RequestData._getProgramWideFields().
org_app_survey_properties = {
'key_name' : '%s/%s/orgapp' % (gsoc2014.prefix, gsoc2014.key().name()),
'program' : gsoc2014,
'title' : 'Org App Survey',
'content' : 'Here is some content.',
'modified_by' : current_user.key.to_old_key(),
'survey_start' : gsoc2014_timeline.program_start,
'survey_end' : gsoc2014_timeline.program_start + datetime.timedelta(9)
}
org_app_survey_model.OrgAppSurvey(**org_app_survey_properties).put()
org_app_start = gsoc2010_timeline.program_start
org_app_end = gsoc2010_timeline.program_end + datetime.timedelta(9)
org_app_survey_properties['key_name'] = ('%s/%s/orgapp' % (
gsoc2010.prefix, gsoc2010.key().name()))
org_app_survey_properties['program'] = gsoc2010
org_app_survey_properties['survey_start'] = org_app_start
org_app_survey_properties['survey_end'] = org_app_end
org_app_survey_model.OrgAppSurvey(**org_app_survey_properties).put()
timeline_properties = {
'key_name': 'google/gci2013',
'link_id': 'gci2013',
'scope': google,
'program_start': now,
'accepted_organization_announced_deadline': now + (week * 2),
'student_signup_start': now + (week * 2),
'student_signup_end': now + month,
'tasks_publicly_visible': now + month,
'task_claim_deadline': now + (month * 2),
'stop_all_work_deadline': now + (month * 4),
'work_review_deadline' : now + (month * 4) + week,
'winners_announced_deadline' : now + (month * 4) + week,
'program_end': now + (month * 4) + week,
}
gci2013_timeline = GCITimeline(**timeline_properties)
gci2013_timeline.put()
program_properties.update({
'key_name': 'google/gci2013',
'link_id': 'gci2013',
'program_id': 'gci2013',
'name': 'Google Code In Contest 2013',
'short_name': 'GCI 2009',
'description': 'This is the program for GCI 2013.',
'timeline': gci2013_timeline,
})
gci2013 = GCIProgram(**program_properties)
gci2013.put()
site.active_program = gci2013
site.put()
current_user.host_for = [
ndb.Key.from_old_key(gsoc2010.key()),
ndb.Key.from_old_key(gsoc2014.key()),
ndb.Key.from_old_key(gci2013.key())]
current_user.put()
group_properties.update({
'key_name': 'google/gci2013/melange',
'link_id': 'melange',
'name': 'Melange Development Team',
'short_name': 'Melange',
'scope': gci2013,
'program': gci2013,
'sponsor': google,
'home_page': 'http://code.google.com/p/soc',
'description': 'Melange, share the love!',
'license_name': 'Apache License',
'ideas': 'http://code.google.com/p/soc/issues',
})
melange = GCIOrganization(**group_properties)
melange.put()
group_properties.update({
'scope': gsoc2014,
'program': gsoc2014,
})
address_properties = address.Address(
street='1 Test St.',
city='Some City',
country='United States',
postal_code='12345')
address_properties.put()
contact_info = contact.Contact(email='test@example.com')
contact_info.put()
gsoc_delta = datetime.timedelta(days=(365 * 18))
profile_properties = {
'id': gsoc2014.key().name() + '/' + current_user.key.id(),
'parent': current_user.key,
'public_name': 'test',
'program': ndb.Key.from_old_key(gsoc2014.key()),
'first_name': 'Test',
'last_name': 'Example',
'contact' : contact_info,
'residential_address' : address_properties,
'shipping_address' : address_properties,
'birth_date' : datetime.date.today() - gsoc_delta,
'program_knowledge' : 'Friend referral',
}
profile = profile_model.Profile(**profile_properties)
ndb_orgs = []
for i in range(15):
group_properties.update({
'key_name': 'google/gsoc2014/org_%d' % i,
'link_id': 'org_%d' % i,
'name': 'Organization %d' % i,
'short_name': 'Org %d' % i,
'description': 'Organization %d!' % i,
})
org_properties = {
'name': 'Organization %d' % i,
'org_id': 'org_%d' % i,
'program': ndb.Key.from_old_key(gsoc2014.key()),
'description': 'Organization %d!' % i,
'logo_url': request.build_absolute_uri(
'/soc/content/%s/images/soc/logo/melange-m-transparent.png' %
os.environ.get('CURRENT_VERSION_ID', '').split('.')[0]),
}
if i < 5:
org_properties['status'] = organization_model.Status.ACCEPTED
org = soc_org_model.SOCOrganization(
id='google/gsoc2014/org_%d' % i, **org_properties)
org.put()
ndb_orgs.append(org)
# Admin (and thus mentor) for the first org
if i == 0:
profile.admin_for.append(org.key)
profile.mentor_for.append(org.key)
profile.put()
# Mentor for the second org
if i == 1:
profile.mentor_for.append(org.key)
profile.put()
profile_properties.update({
'id': gci2013.key().name() + '/' + current_user.key.id(),
'parent': current_user.key,
'program': ndb.Key.from_old_key(gci2013.key()),
'admin_for': [ndb.Key.from_old_key(melange.key())],
'mentor_for': [ndb.Key.from_old_key(melange.key())],
})
melange_admin = profile_model.Profile(**profile_properties)
# TODO: add GCI orgs
melange_admin.put()
task_properties = {
'status': 'Open',
'modified_by': melange_admin.key.to_old_key(),
'subscribers': [melange_admin.key.to_old_key()],
'title': 'Awesomeness',
'created_by': melange_admin.key.to_old_key(),
'created_on': now,
'program': gci2013,
'time_to_complete': 1337,
'modified_on': now,
'org': melange.key(),
'description': '<p>AWESOME</p>',
'difficulty_level': DifficultyLevel.MEDIUM,
'types': ['Code']
}
gci_task = GCITask(**task_properties)
gci_task.put()
student_id = 'student'
# TODO(drewgottlieb): Figure out how to get dev server's user_id hash for a
# given email address. I obtained this hash by copying
# the user_id the dev server generates when I log in as
# this email.
account = users.User(
email=('%s@example.com' % student_id), _user_id='115021216387231191261')
user_properties = {
'id': student_id,
'account_id': account.user_id(),
'account': account,
}
student_user = user.User(**user_properties)
student_user.put()
gci_delta = datetime.timedelta(days=(365 * 14))
contact_properties = contact.Contact(
email='student@email.com',
web_page='http://www.homepage.com/',
blog='http://www.blog.com/',
phone='1650253000')
contact_properties.put()
graduation_year = datetime.date.today() + datetime.timedelta(days=365)
student_data = soc_profile.SOCStudentData(
education=education.Education(
school_id="123",
school_country="United States",
expected_graduation=int(graduation_year.strftime('%Y')),
major='Some Major',
degree=education.Degree.UNDERGRADUATE)
)
student_data.put()
student_properties = {
'id': gsoc2014.key().name() + "/" + student_id,
'parent': student_user.key,
'program': ndb.Key.from_old_key(gsoc2014.key()),
'public_name': 'Student',
'first_name': 'Student',
'last_name': 'Student',
'contact' : contact_properties,
'residential_address' : address_properties,
'shipping_address' : address_properties,
'birth_date': datetime.date.today() - gci_delta,
'tee_size': profile_model.TeeSize.L,
'tee_style': profile_model.TeeStyle.MALE,
'gender' : profile_model.Gender.MALE,
'program_knowledge': 'Friend referral.',
'student_data' : student_data,
}
melange_student = profile_model.Profile(**student_properties)
melange_student.put()
student_id = 'student2'
# TODO(drewgottlieb): Figure out how to get dev server's user_id hash for a
# given email address. I obtained this hash by copying
# the user_id the dev server generates when I log in as
# this email.
account = users.User(
email=('%s@example.com' % student_id), _user_id='100377022012711418814')
user_properties = {
'id': student_id,
'account_id': account.user_id(),
'account': account,
}
student_user2 = user.User(**user_properties)
student_user2.put()
student_properties.update({
'id': gsoc2014.key().name() + "/" + student_id,
'parent': student_user2.key,
'first_name' : 'Student 2',
'last_name' : 'Example'
})
melange_student2 = profile_model.Profile(**student_properties)
melange_student2.put()
pending_proposal_properties = {
'parent': melange_student.key,
'title': 'Test proposal',
'abstract': 'This is an abstract abstract.',
'content': 'This content should make you content.',
'additional_info': 'http://zombo.com',
'mentors': [],
'possible_mentors': [profile.key],
'scores': [soc_proposal_model.Score(
author=profile.key, value=2)],
'organization': ndb_orgs[1].key,
'program': ndb.Key.from_old_key(gsoc2014.key()),
}
pending_melange_proposal = soc_proposal_model.Proposal(
**pending_proposal_properties)
pending_melange_proposal.put()
accepted_proposal_properties = {
'parent': melange_student.key,
'title': 'Accepted test proposal',
'abstract': 'This is an accepted abstract abstract.',
'content': 'This content should make you feel accepting.',
'additional_info': 'http://zombo.com',
'mentors': [profile.key],
'possible_mentors': [profile.key],
'status': soc_proposal_model.Status.ACCEPTED,
'scores': [soc_proposal_model.Score(
author=profile.key, value=4)],
'organization': ndb_orgs[1].key,
'program': ndb.Key.from_old_key(gsoc2014.key()),
}
accepted_melange_proposal = soc_proposal_model.Proposal(
**accepted_proposal_properties)
accepted_melange_proposal.put()
project_properties = {
'parent': melange_student.key.to_old_key(),
'proposal': accepted_melange_proposal.key.to_old_key(),
'title': 'Accepted test project',
'abstract': 'This is an abstract project\'s abstract.',
'status': 'accepted',
'org': ndb_orgs[1].key.to_old_key(),
'mentors': [profile.key.to_old_key()],
'additional_info': 'http://zombo.com',
'program': gsoc2014,
}
melange_project = GSoCProject(**project_properties)
melange_project.put()
ndb_orgs[1].slot_allocation = 1
ndb_orgs[1].put()
project_properties = {
'parent': melange_student.key,
'proposal': accepted_melange_proposal.key,
'title': 'Accepted test project',
'abstract': 'This is an abstract project\'s abstract.',
'status': project_model.Status.ACCEPTED,
'organization': ndb_orgs[1].key,
'mentors': [profile.key],
'additional_info': 'http://zombo.com',
'program': ndb.Key.from_old_key(gsoc2014.key()),
'content': 'This is content of the project',
}
melange_project = project_model.Project(**project_properties)
melange_project.put()
student_data.number_of_projects = 1
student_data.number_of_proposals = 2
student_data.project_for_orgs = [ndb_orgs[1].key]
melange_student.put()
melange_student2.put()
student_id = 'student'
student_properties.update({
'id': gci2013.key().name() + '/' + student_id,
'parent': student_user.key,
'program': ndb.Key.from_old_key(gci2013.key()),
})
gci_student = profile_model.Profile(**student_properties)
gci_student.put()
score_properties = {
'parent': gci_student.key.to_old_key(),
'program': gci2013,
'points': 5,
'tasks': [gci_task.key()]
}
score = GCIScore(**score_properties)
score.put()
document_properties = {
'key_name': 'site/site/home',
'link_id': 'home',
'scope': site,
'prefix': 'site',
'author': current_user.key.to_old_key(),
'title': 'Home Page',
'content': 'This is the Home Page',
'modified_by': current_user.key.to_old_key(),
}
home_document = Document(**document_properties)
home_document.put()
document_properties = {
'key_name': 'user/test/notes',
'link_id': 'notes',
'scope': current_user.key.to_old_key(),
'prefix': 'user',
'author': current_user.key.to_old_key(),
'title': 'My Notes',
'content': 'These are my notes',
'modified_by': current_user.key.to_old_key(),
}
notes_document = Document(**document_properties)
notes_document.put()
site.home = home_document
site.put()
memcache.flush_all()
return http.HttpResponse('Done')
def clear(*args, **kwargs):
"""Removes all entities from the datastore.
"""
# TODO(dbentley): If there are more than 1000 instances of any model,
# this method will not clear all instances. Instead, it should continually
# call .all(), delete all those, and loop until .all() is empty.
entities = itertools.chain(*[
Survey.all(),
SurveyRecord.all(),
GCIOrganization.all(),
GSoCTimeline.all(),
GCITimeline.all(),
GSoCProgram.all(),
GSoCProject.all(),
GSoCProposal.all(),
GCIProgram.all(),
GCIScore.all(),
GSoCStudentInfo.all(),
GCIStudentInfo.all(),
GCITask.all(),
Sponsor.all(),
Site.all(),
Document.all(),
# The below models are all subclasses of ndb.Model and therefore must
# use .query() to return all instances instead of .all().
soc_org_model.SOCOrganization.query(),
profile_model.Profile.query(),
soc_profile.SOCStudentData.query(),
user.User.query(),
address.Address.query(),
contact.Contact.query()
])
try:
for entity in entities:
if isinstance(entity, ndb.Model):
entity.key.delete()
else:
entity.delete()
except db.Timeout:
return http.HttpResponseRedirect('#')
memcache.flush_all()
return http.HttpResponse('Done')
def reseed(*args, **kwargs):
"""Clears and seeds the datastore.
"""
clear(*args, **kwargs)
seed(*args, **kwargs)
return http.HttpResponse('Done')