blob: 89097dcc2600e88670ad10d83373ea973755e1ab [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.
"""Project updating mapreduce."""
import logging
from google.appengine.ext import db
from google.appengine.ext import ndb
from mapreduce import operation
from soc.modules.gsoc.models.project import GSoCProject
from summerofcode.models import project as project_model
@ndb.transactional
def convertProject(old_project):
project_key = old_project.key()
if old_project.new_project:
logging.error('Project already converted: %s/%s',
project_key.parent().name(), project_key.id())
return None
parent_key = ndb.Key(
'User', project_key.parent().parent().name(),
'Profile', project_key.parent().name())
if old_project.status == 'accepted':
status = project_model.Status.ACCEPTED
elif old_project.status == 'failed':
status = project_model.Status.FAILED
elif old_project.status in ['withdrawn', 'invalid']:
status = project_model.Status.WITHDRAWN
else:
raise ValueError('Unknown project status: %s' % old_project.status)
old_org_key = GSoCProject.org.get_value_for_datastore(old_project)
if not old_org_key:
logging.error('Project with no org: %s/%s',
project_key.parent().name(), project_key.id())
return None
else:
organization = ndb.Key.from_old_key(old_org_key)
mentors = []
for old_mentor_key in old_project.mentors:
mentors.append(ndb.Key.from_old_key(old_mentor_key))
program_key = ndb.Key.from_old_key(
GSoCProject.program.get_value_for_datastore(old_project))
properties = {
'title': old_project.title,
'abstract': old_project.abstract,
'content': old_project.public_info,
'additional_info': old_project.additional_info,
'is_featuring_allowed': old_project.is_featured,
'mentors': mentors,
'status': status,
'organization': organization,
'program': program_key,
'parent': parent_key,
}
new_project = project_model.Project(**properties)
new_project.put()
return new_project
@db.transactional
def setNewProject(old_project, new_project):
old_project.new_project = new_project.key.urlsafe()
old_project.put()
def process(project_key):
old_project = db.get(project_key)
new_project = convertProject(old_project)
# if a new project is not created (for some reason), let us record that
# so that we can retrieve the entity and manually investigate what
# the problem is.
if not new_project:
yield operation.counters.Increment(
'%s/%s' % (old_project.key().parent().name(), old_project.key().id()))
else:
setNewProject(old_project, new_project)