blob: c79846c317b05fbf5348702560d8f737e2aab656 [file] [log] [blame]
# Copyright 2013 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.
"""MapReduce to finalize and apply program administrators' decisions on
what organizations are accepted into program."""
from google.appengine.ext import db
from google.appengine.ext import ndb
from mapreduce import context as mapreduce_context
from melange.logic import organization as org_logic
from melange.logic import profile as profile_logic
from melange.models import organization as org_model
from melange.request import links
from codein.views.helper import urls as ci_urls
from soc.logic import site as site_logic
# pylint: disable=unused-import
from soc.modules.gci.models.program import GCIProgram
from soc.modules.gsoc.models.program import GSoCProgram
# pylint: enable=unused-import
from summerofcode.views.helper import urls as soc_urls
def process(org_key):
"""Processes a single organization.
Organization status is updated to ACCEPTED or REJECTED if the current
status has been set to PRE_ACCEPTED or PRE_REJECTED, respectively,
by program administrators.
Args:
org_key: Organization key.
"""
context = mapreduce_context.get()
program_key = db.Key(context.mapreduce_spec.mapper.params['program_key'])
org_key = ndb.Key.from_old_key(org_key)
organization = org_key.get()
# do not do anything for organizations participating in another program
if organization.program.to_old_key() != program_key:
return
if program_key.kind() == 'GSoCProgram':
url = links.ABSOLUTE_LINKER.organization(
org_key, soc_urls.UrlNames.ORG_PROFILE_EDIT)
elif program_key.kind() == 'GCIProgram':
url = links.ABSOLUTE_LINKER.organization(
org_key, ci_urls.UrlNames.ORG_PROFILE_EDIT)
else:
raise ValueError('Invalid program type %s' % program_key.kind())
program = db.get(program_key)
site = site_logic.singleton()
org_admins = profile_logic.getOrgAdmins(org_key.get())
# We are "prefetching" the ProgramMessages entity here instead of fetching
# it where it is required i.e. when the message templates are required
# to build the email message body. We do this because we perform the
# operation of fetching the ProgramMessages entity if it exists or create
# it if it doesn't in a Appengine regular "db" transation whereas rest
# of the updating of organization entities happen within an ndb transaction
# because Organization model is an ndb model and such cross API nested
# transactions are incompatible in Appengine.
program_messages = program.getProgramMessages()
@ndb.transactional(xg=True)
def updateOrganizationStatus():
"""Transactionally updates organization status."""
# only organizations defined for the specified program should be processed
organization = org_key.get()
# do not do anything for "finalized" organizations
if organization.status in [
org_model.Status.ACCEPTED,
org_model.Status.REJECTED]:
return organization.status
if organization.status == org_model.Status.PRE_ACCEPTED:
extra_context = {'url': url}
organization = org_logic.setStatus(
organization, program, site, program_messages,
org_model.Status.ACCEPTED, org_admins=org_admins,
extra_context=extra_context)
# organizations that were pre-rejected or those without application
# response are rejected
elif (organization.status == org_model.Status.PRE_REJECTED or
not org_logic.getApplicationResponse(org_key)):
organization = org_logic.setStatus(
organization, program, site, program_messages,
org_model.Status.REJECTED, org_admins=org_admins)
return organization.status
new_status = updateOrganizationStatus()
# reject organization administrators for rejected organizations
if new_status == org_model.Status.REJECTED:
@ndb.transactional
def clearOrgMemberRoles(profile_key):
"""Transactionally clears roles for the specified profile if the currently
processed organization is rejected.
The organization key is removed from mentor_for field. It is also removed
from admin_for field and moved to rejected_for field.
Args:
profile_key: ndb.Key of profile entity.
"""
profile = profile_key.get()
if org_key in profile.admin_for:
profile.admin_for.remove(org_key)
profile.rejected_for = set(profile.rejected_for + [org_key])
if org_key in profile.mentor_for:
profile.mentor_for.remove(org_key)
profile.put()
mentors = profile_logic.queryAllMentorsForOrg(org_key).fetch(1000)
for mentor in mentors:
clearOrgMemberRoles(mentor.key)