blob: b0fcb045d704b991c399c20596a028ffbac49825 [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.
"""MapReduce to create new entities of 'ProposalRevision' model from existing
Proposal entities and add 'latest_revision' property to 'Proposal' model. This
is a one-time script which will separate out the content from the Proposal
entity to a separate ProposalRevision entity which will be responsible for
handling revisions of proposals henceforth.
"""
from google.appengine.ext import ndb
from mapreduce import operation
from summerofcode.models import proposal as proposal_model
@ndb.transactional
def createProposalRevisionsForProposal(proposal_key):
"""Transaction method which creates 'ProposalRevision' entitites for the
specified proposal and then updates the 'Proposal' entity to store the key
of the created 'ProposalRevision'.
Args:
proposal_key: Key of Proposal entity.
"""
proposal = proposal_key.get()
proposal_revision_flat = list(proposal_key.flat())
proposal_revision_flat.append(proposal_model.ProposalRevision)
proposal_revision_flat.append('0')
proposal_revision_key = ndb.Key(flat=proposal_revision_flat)
proposal_revision = proposal_model.ProposalRevision(
key=proposal_revision_key, content=proposal.content)
proposal.latest_revision = proposal_revision_key
ndb.put_multi([proposal_revision, proposal])
def process(proposal_key):
"""Processes a single proposal by creating a 'ProposalRevision' entity
and updating the 'Proposal' entity to store the key of 'ProposalRevision'
entity.
This MR is intended to run on the current Proposal entities that do not
support ProposalRevision entities yet. It creates the ProposalRevision
entities for the existing Proposal entities. The steps to create
ProposalRevision entities are as follows:
- Creates 'ProposalRevision' entity for the specified proposal
- Updates 'latest_revision' property of 'Proposal' entity
- Copies content from 'Proposal' entity to the respective
'ProposalRevision' entity
Args:
proposal_key: Key of Proposal entity in urlsafe form.
"""
proposal_key = ndb.Key(urlsafe=str(proposal_key))
revision = proposal_model.ProposalRevision.query(
ancestor=proposal_key).get()
if not revision:
createProposalRevisionsForProposal(proposal_key)
operation.counters.Increment('ProposalRevision entities created')