blob: 6978704c1afde4ca549aa57995d6a39be9745b9f [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.
""" Utilities to manipulate proposal data."""
import datetime
from google.appengine.ext import ndb
from melange.models import connection as connection_model
from seeder import profile as profile_seeder
from summerofcode.models import proposal as proposal_model
from tests import org_utils
TEST_ABSTRACT = 'Test Abstract'
TEST_TITLE = 'Test Title'
TEST_CONTENT = 'Test Content %s'
TEST_COMMENT_CONTENT = 'Test Comment Content'
def seedProposal(
student_key, program_key, org_key=None, mentor_key=None,
rank=None, **kwargs):
"""Seeds a new proposal entity.
Args:
student_key: Key of the student who is an author of the proposal.
program_key: Key of the program to which the proposal applies.
org_key: Key of the organization to which the proposal is submitted.
mentor_key: Key of the mentor assigned to the proposal.
rank: Optional int to specify rank of the proposal.
Returns:
The newly seeded proposal entity.
"""
org_key = org_key or org_utils.seedSOCOrganization(program_key).key
mentor_key = mentor_key or profile_seeder.seedProfile(
program_key, mentor_for=[org_key]).key
content = kwargs.pop('content', TEST_CONTENT % datetime.datetime.utcnow())
properties = {
'parent': student_key,
'program': ndb.Key.from_old_key(program_key),
'organization': org_key,
'mentors': [mentor_key],
'abstract': TEST_ABSTRACT,
'title': TEST_TITLE,
}
properties.update(**kwargs)
proposal = proposal_model.Proposal(**properties)
proposal.put()
revision_properties = {
'parent': proposal.key,
'content': content,
}
revision = proposal_model.ProposalRevision(id='0', **revision_properties)
proposal.latest_revision = revision.key
ndb.put_multi([proposal, revision])
if proposal.status != proposal_model.Status.WITHDRAWN:
student = student_key.get()
student.student_data.number_of_proposals += 1
student.student_data.preference_of_proposals.insert(
rank - 1 if rank is not None
else len(student.student_data.preference_of_proposals),
proposal.key)
student.put()
return proposal
def seedComment(proposal, author_key=None, **kwargs):
"""Seeds a new comment for the specified proposal.
Args:
proposal: proposal_model.Proposal for which to seed a comment.
author_key: ndb.Key of the profile of an author of the comment. A new
profile is seeded, if no key is provided.
Returns:
The newly seeded ConnectionMessage entity.
"""
author_key = (
author_key or
profile_seeder.seedProfile(proposal.program.to_old_key()).key)
properties = {
'parent': proposal.key,
'content': TEST_COMMENT_CONTENT,
'author': author_key,
'is_private': False,
}
properties.update(**kwargs)
comment = connection_model.ConnectionMessage(**properties)
comment.put()
return comment
def seedRevision(proposal, **kwargs):
"""Seeds a new revision for the specified proposal.
Args:
proposal (summerofcode.models.proposal.Proposal): Proposal entity.
Returns (summerofcode.models.proposal.ProposalRevision):
The newly seeded proposal revision entity.
"""
# key of the new revision is constructed
revision_key = ndb.Key(
proposal_model.ProposalRevision._get_kind(),
proposal_model.getNextRevisionId(proposal.latest_revision.id()),
parent=proposal.key)
properties = {'content': TEST_CONTENT % datetime.datetime.utcnow()}
properties.update(**kwargs)
revision = proposal_model.ProposalRevision(key=revision_key, **properties)
proposal.latest_revision = revision_key
ndb.put_multi([proposal, revision])