| # 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 converting profile model.""" |
| |
| from google.appengine.ext import ndb |
| |
| # This MapReduce requires this model to have been imported. |
| # pylint: disable=unused-import |
| from melange.models.profile import Profile |
| # pylint: enable=unused-import |
| |
| |
| @ndb.transactional |
| def convertProfileRolesTxn(profile_key): |
| """Converts lists of organizations for which the specified profile has |
| role in a transaction. |
| |
| Args: |
| profile_key: Profile key. |
| """ |
| # only update profiles for Code In programs |
| if 'gci' not in profile_key.name(): |
| return |
| |
| profile = ndb.Key.from_old_key(profile_key).get() |
| |
| # only update projects |
| new_mentor_for = [] |
| for org_key in profile.mentor_for: |
| new_mentor_for.append(ndb.Key('Organization', org_key.id())) |
| profile.mentor_for = new_mentor_for |
| |
| new_admin_for = [] |
| for org_key in profile.admin_for: |
| new_admin_for.append(ndb.Key('Organization', org_key.id())) |
| profile.admin_for = new_admin_for |
| |
| profile.put() |
| |
| |
| @ndb.transactional |
| def convertWinnerForTxn(profile_key): |
| """Converts the name of organization model for winner_for property which |
| holds a key to organization for which the specified profile is a winner. |
| |
| Existing references have 'GCIOrganization' model whereas after DB->NDB |
| conversion the name was simplified to 'Organization'. |
| |
| It applies to Code In profiles only. |
| |
| Args: |
| profile_key: Profile key. |
| """ |
| # only update profiles for Code In programs |
| if 'gci' not in profile_key.name(): |
| return |
| |
| profile = ndb.Key.from_old_key(profile_key).get() |
| |
| if profile.is_student and profile.student_data.winner_for: |
| profile.winner_for = ndb.Key( |
| 'Organization', profile.student_data.winner_for.id()) |
| profile.put() |