| # 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. |
| |
| """MapReduce to finalize and apply decisions on what students |
| are accepted into program.""" |
| |
| import logging |
| |
| 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 soc.logic import site as site_logic |
| from soc.modules.gsoc.models import program as program_model |
| |
| from summerofcode.logic import profile as profile_logic |
| from summerofcode.logic import proposal as proposal_logic |
| from summerofcode.models import proposal as proposal_model |
| |
| # pylint: disable=unused-import |
| from summerofcode.models.organization import SOCOrganization |
| # pylint: enable=unused-import |
| |
| |
| def process(profile_key): |
| """Sends acceptance or rejection emails to the specified profile, if it is |
| a student for the requested program. |
| |
| Args: |
| profile_key: db.Key of the profile entity. |
| """ |
| profile_key = ndb.Key.from_old_key(profile_key) |
| |
| context = mapreduce_context.get() |
| program_key = db.Key(context.mapreduce_spec.mapper.params['program_key']) |
| |
| profile = profile_key.get() |
| if profile.program.to_old_key() == program_key and profile.is_student: |
| query = proposal_logic.queryProposalsForStudent(profile_key) |
| all_proposals = query.fetch(1000) |
| |
| accepted_proposals = [ |
| proposal for proposal in all_proposals |
| if proposal.status == proposal_model.Status.ACCEPTED] |
| rejected_proposals = [ |
| proposal for proposal in all_proposals |
| if proposal.status == proposal_model.Status.REJECTED] |
| if accepted_proposals or rejected_proposals: |
| program = program_model.GSoCProgram.get(profile.program.to_old_key()) |
| program_messages = program.getProgramMessages() |
| site = site_logic.singleton() |
| |
| for accepted_proposal in accepted_proposals: |
| proposal_item = proposal_logic.ProposalItem( |
| accepted_proposal, accepted_proposal.organization.get(), |
| org_logic.getOrganizationMessages(accepted_proposal.organization)) |
| |
| logging.info( |
| 'Sending acceptance email to %s for proposal %s', |
| profile_key.id(), accepted_proposal.key.id()) |
| proposal_logic.dispatchAcceptedProposalEmail( |
| profile, program, program_messages, site, proposal_item) |
| |
| logging.info( |
| 'Sending accepted student welcome email to %s', profile_key.id()) |
| profile_logic.dispatchAcceptedStudentWelcomeEmail( |
| profile, program, program_messages, site) |
| |
| if not accepted_proposals: |
| # there is at least one rejected proposal |
| proposal_items = [] |
| for rejected_proposal in rejected_proposals: |
| proposal_items.append( |
| proposal_logic.ProposalItem( |
| rejected_proposal, rejected_proposal.organization.get(), |
| org_logic.getOrganizationMessages( |
| rejected_proposal.organization))) |
| |
| logging.info('Sending rejection email to %s', profile_key.id()) |
| proposal_logic.dispatchRejectedProposalEmail( |
| profile, program, program_messages, site, proposal_items) |