| # Copyright 2011 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. |
| |
| """GSoCProposal 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.score import GSoCScore |
| from soc.modules.gsoc.models.proposal import GSoCProposal |
| |
| from summerofcode.models import proposal as proposal_model |
| |
| |
| @ndb.transactional |
| def convertProposal(old_proposal, old_scores): |
| proposal_key = old_proposal.key() |
| if old_proposal.is_converted: |
| logging.error('Proposal already converted: %s/%s', |
| proposal_key.parent().name(), proposal_key.id()) |
| return False |
| |
| parent_key = ndb.Key( |
| 'User', proposal_key.parent().parent().name(), |
| 'Profile', proposal_key.parent().name()) |
| |
| visibility = ( |
| proposal_model.Visibility.PUBLIC |
| if old_proposal.is_publicly_visible |
| else proposal_model.Visibility.ORG_MEMBER) |
| |
| old_mentor_key = GSoCProposal.mentor.get_value_for_datastore(old_proposal) |
| if old_mentor_key: |
| mentors = [ |
| ndb.Key( |
| 'User', old_mentor_key.parent().name(), |
| 'Profile', old_mentor_key.name())] |
| else: |
| mentors = [] |
| |
| possible_mentors = [] |
| for possible_mentor in possible_mentors: |
| possible_mentors.append( |
| ndb.Key( |
| 'User', possible_mentor.parent().name(), |
| 'Profile', possible_mentor.name())) |
| |
| scores = [] |
| for old_score in old_scores: |
| old_author_key = GSoCScore.author.get_value_for_datastore(old_score) |
| author_key = ndb.Key( |
| 'User', old_author_key.parent().name(), |
| 'Profile', old_author_key.name()) |
| scores.append( |
| proposal_model.Score(value=old_score.value, author=author_key)) |
| |
| if old_proposal.status == 'pending': |
| status = proposal_model.Status.PENDING |
| elif old_proposal.status == 'accepted': |
| status = proposal_model.Status.ACCEPTED |
| elif old_proposal.status in ['ignored', 'rejected', 'invalid']: |
| status = proposal_model.Status.REJECTED |
| elif old_proposal.status == 'withdrawn': |
| status = proposal_model.Status.WITHDRAWN |
| else: |
| raise ValueError('Unknown proposal status: %s' % old_proposal.status) |
| |
| old_org_key = GSoCProposal.org.get_value_for_datastore(old_proposal) |
| if not old_org_key: |
| logging.error('Proposal with no org: %s/%s', |
| proposal_key.parent().name(), proposal_key.id()) |
| return False |
| else: |
| organization = ndb.Key.from_old_key(old_org_key) |
| |
| program = ndb.Key.from_old_key( |
| GSoCProposal.program.get_value_for_datastore(old_proposal)) |
| |
| properties = { |
| 'abstract': old_proposal.abstract, |
| 'accept_as_project': old_proposal.accept_as_project, |
| 'additional_info': old_proposal.additional_info, |
| 'content': old_proposal.content, |
| 'created_on': old_proposal.created_on, |
| 'is_editable_post_deadline': old_proposal.is_editable_post_deadline, |
| 'is_ignored': old_proposal.status == 'ignored', |
| 'mentors': mentors, |
| 'modified_on': old_proposal.last_modified_on, |
| 'organization': organization, |
| 'parent': parent_key, |
| 'possible_mentors': possible_mentors, |
| 'program': program, |
| 'scores': scores, |
| 'status': status, |
| 'title': old_proposal.title, |
| 'visibility': visibility, |
| } |
| new_proposal = proposal_model.Proposal(**properties) |
| new_proposal.put() |
| |
| return True |
| |
| |
| @db.transactional |
| def markAsConverted(old_proposal): |
| old_proposal.is_converted = True |
| old_proposal.put() |
| |
| |
| def process(proposal_key): |
| old_proposal = db.get(proposal_key) |
| old_scores = GSoCScore.all().ancestor(old_proposal).fetch(1000) |
| result = convertProposal(old_proposal, old_scores) |
| if not result: |
| yield operation.counters.Increment( |
| '%s/%s' % (proposal_key.parent().name(), proposal_key.id())) |
| else: |
| markAsConverted(old_proposal) |