blob: 00170363b642bc37e6f4a4b66bcc61b44aed6d56 [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.
"""Seeder functions for survey model."""
from google.appengine.ext import db
from google.appengine.ext import ndb
from soc.models import org_app_survey as app_survey_model
from soc.modules.gsoc.models import grading_project_survey as grading_project_survey_model
from soc.modules.gsoc.models import grading_project_survey_record as grading_project_survey_record_model
from soc.modules.gsoc.models import grading_record as grading_record_model
from soc.modules.gsoc.models import grading_survey_group as grading_survey_group_model
from soc.modules.gsoc.models import project_survey as project_survey_model
from soc.modules.gsoc.models import project_survey_record as project_survey_record_model
from seeder import profile as profile_seeder
from seeder import user as user_seeder
from seeder import utils as seeder_utils
TEST_APPLICATION_SURVEY_TITLE = 'Application Survey for %s'
TEST_MENTOR_EVALUATION_TITLE = 'Mentor Evaluation (%s) for %s'
TEST_STUDENT_EVALUATION_TITLE = 'Student Evaluation (%s) for %s'
TEST_GRADING_SURVEY_GROUP_NAME = 'survey_group_%s_%s'
TEST_EVALUATION_SCHEMA = (
'[["frm-t1309871149671-item","frm-t1309871322655-item",'
'"frm-t1309871157535-item","frm-t1309871294200-item",'
'"frm-t1310822212610-item"],{"frm-t1309871149671-item":'
'{"field_type":"input_text","required":true,"label":'
'"What%20is%20your%20name%3F"},"frm-t1309871322655-item":'
'{"field_type":"checkbox","required":false,"other":false,'
'"values":[{"value":"Option%203","checked":true},{"value":'
'"Option%202","checked":true},{"value":"Option%204","checked":true}'
',{"value":"Option%201","checked":true}],"label":"'
'Give%20every%20option%20you%20think%20is%20right"},'
'"frm-t1309871157535-item":{"field_type":"textarea","required":'
'false,"label":"Write%20a%20detail%20of%20your%20project%3F"},'
'"frm-t1309871294200-item":{"field_type":"radio","required":'
'false,"other":false,"values":[{"value":"Amongst%20the%20best%20'
'people%20I%27ve%20ever%20worked%20with%20","checked":false},'
'{"value":"Don%27t%20Know","checked":false},{"value":"Yes","checked"'
':false}],"label":"Are%20you%20alright%3F"},"frm-t1310822212610-item"'
':{"field_type":"radio","required":true,"other":true,"values":'
'[{"value":"Wa","checked":true},{"value":"Wa%20Wa","checked":false}]'
',"label":"Testing%20radio%20again%20%3A%29"}}]')
def seedApplicationSurvey(program_key, **kwargs):
"""Seeds a new organization application survey for the specified program.
Args:
program_key: Program key to create a survey for.
kwargs: Optional values for other properties of the seeded entity.
Returns:
Newly seeded survey entity.
"""
user = user_seeder.seedUser()
program = db.get(program_key)
properties = {
'key_name': '%s/%s/orgapp' % (program.prefix, program_key.name()),
'scope': program_key,
'program': program_key,
'modified_by': user.key.to_old_key(),
'created_by': user.key.to_old_key(),
'author': user.key.to_old_key(),
'schema': ('[["item"],{"item":{"field_type":"input_text",'
'"required":false, "label":"test"}}]'),
'survey_start': seeder_utils.pastDateTime(),
'survey_end': seeder_utils.futureDateTime(),
'title': TEST_APPLICATION_SURVEY_TITLE % program.name
}
properties.update(kwargs)
applicationSurvey = app_survey_model.OrgAppSurvey(**properties)
applicationSurvey.put()
return applicationSurvey
def _seedProjectSurvey(model_cls, program, survey_type=None, **kwargs):
user = user_seeder.seedUser(host_for=[program])
survey_type = survey_type or project_survey_model.MIDTERM_EVAL
properties = {
'key_name': '%s/%s/%s' % (
program.prefix, program.key().name(), survey_type),
'link_id': survey_type,
'scope': program,
'program': program,
'modified_by': user.key.to_old_key(),
'created_by': user.key.to_old_key(),
'author': user.key.to_old_key(),
'schema': TEST_EVALUATION_SCHEMA,
'survey_start': seeder_utils.pastDateTime(),
'survey_end': seeder_utils.futureDateTime(),
}
properties.update(kwargs)
survey = model_cls(**properties)
survey.put()
return survey
def seedStudentEvaluation(program, survey_type=None, **kwargs):
"""Seeds a new student evaluation of the specified type for the specified
program.
Args:
program (soc.models.program.Program): Program entity.
survey_type (string): Type of a survey to seed. Must be one of
project_survey_model.SURVEY_TYPES. Midterm evaluation is seeded if
no type is specified.
Returns (soc.modules.gsoc.models.project_survey.ProjectSurvey):
Newly seeded survey entity.
"""
properties = {
'title': TEST_STUDENT_EVALUATION_TITLE % (survey_type, program.name)
}
properties.update(**kwargs)
return _seedProjectSurvey(
project_survey_model.ProjectSurvey, program, survey_type,
**properties)
def seedMentorEvaluation(program, survey_type=None, **kwargs):
"""Seeds a new mentor evaluation of the specified type for the specified
program.
Args:
program (soc.models.program.Program): Program entity.
survey_type (string): Type of a survey to seed. Must be one of
project_survey_model.SURVEY_TYPES. Midterm evaluation is seeded if
no type is specified.
Returns (soc.modules.gsoc.models.grading_project_survey.GradingProjectSurvey):
Newly seeded survey entity.
"""
properties = {
'title': TEST_MENTOR_EVALUATION_TITLE % (survey_type, program.name)
}
properties.update(**kwargs)
return _seedProjectSurvey(
grading_project_survey_model.GradingProjectSurvey, program,
survey_type, **properties)
def seedGradingSurveyGroup(mentor_evaluation, student_evaluation, **kwargs):
"""Seeds a new grading survey group for the specified evaluations.
Evaluation group is a pair pointing to a student evaluation and a mentor
evaluation.
Args:
mentor_evaluation (grading_project_survey_model.GradingProjectSurvey):
Mentor evaluation entity.
student_evaluation (project_survey_model.ProjectSurvey):
Student evaluation entity.
Returns (grading_survey_group_model.GSoCGradingSurveyGroup):
Newly seeded survey group entity.
"""
if mentor_evaluation.survey_type != student_evaluation.survey_type:
raise ValueError(
'Evaluations have different types: %s != %s.' % (
mentor_evaluation.survey_type, student_evaluation.survey_type))
if mentor_evaluation.program != student_evaluation.program:
raise ValueError(
'Evaluations have different programs: %r != %r.' % (
mentor_evaluation.program, student_evaluation.program))
program = mentor_evaluation.program
properties = {
'name': TEST_GRADING_SURVEY_GROUP_NAME % (
program.key().name(), mentor_evaluation.survey_type),
'program': mentor_evaluation.program,
'grading_survey': mentor_evaluation,
'student_survey': student_evaluation,
}
properties.update(kwargs)
survey_group = grading_survey_group_model.GSoCGradingSurveyGroup(**properties)
survey_group.put()
return survey_group
def seedStudentEvaluationRecord(project, survey_key, **kwargs):
"""Seeds a new response for the specified project and student evaluation.
Args:
project (summerofcode.models.project.Project): Project entity.
survey_key (db.Key): Key of project survey entity.
Returns (project_survey_record_model.GSoCProjectSurveyRecord):
Newly seeded student evaluation response.
"""
properties = {
'org': project.organization.to_old_key(),
'project': project.key.to_old_key(),
'survey': survey_key,
'user': project.key.parent().parent().to_old_key(),
}
properties.update(kwargs)
evaluation_response = (
project_survey_record_model.GSoCProjectSurveyRecord(**properties))
evaluation_response.put()
return evaluation_response
def seedMentorEvaluationRecord(
project, survey_key, grade=None, user_key=None, **kwargs):
"""Seeds a new response for the specified project and mentor evaluation.
Args:
project (summerofcode.models.project.Project): Project entity.
survey_key (db.Key): Key of grading project survey entity.
grade (bool): Whether the seeded response should have a passing grade
or not.
user_key (ndb.Key): Key of the user who submits the evaluation response.
Should be a mentor of the project or an administrator of the organization
to which the project belong.
Returns (grading_project_survey_record_model.GSoCGradingProjectSurveyRecord):
Newly seeded mentor evaluation response.
"""
grade = grade or True
user_key = (
user_key or profile_seeder.seedProfile(
project.program.to_old_key(), admin_for=[project.organization])
.key.parent())
properties = {
'grade': grade,
'org': project.organization.to_old_key(),
'project': project.key.to_old_key(),
'survey': survey_key,
'user': user_key.to_old_key(),
}
properties.update(kwargs)
evaluation_response = (
grading_project_survey_record_model
.GSoCGradingProjectSurveyRecord(**properties))
evaluation_response.put()
return evaluation_response
def seedGradingRecord(
survey_group, project, mentor_record=None, student_record=None,
grade_decision=None, **kwargs):
"""Seeds a new grading record for the specified survey group and project and
is based on the specified responses.
Args:
survey_group grading_survey_group_model.GSoCGradingSurveyGroup):
Survey group entity.
project (summerofcode.models.project.Project): Project entity.
mentor_record (GSoCGradingProjectSurveyRecord):
Mentor evaluation response.
student_record (GSoCProjectSurveyRecord): Student evaluation response.
grade_decision (string): Optional grade decision for the seeded record.
Returns (grading_record_model.GSoCGradingRecord):
The newly seeded grading record.
"""
if mentor_record:
referenced_project_key = ndb.Key.from_old_key(
project_survey_record_model.GSoCProjectSurveyRecord.project
.get_value_for_datastore(mentor_record))
if project.key != referenced_project_key:
raise ValueError(
'Mentor response does not correspond to the project: %s != %s.' %
(project_key, referenced_project_key))
if student_record:
referenced_project_key = ndb.Key.from_old_key(
project_survey_record_model.GSoCProjectSurveyRecord.project
.get_value_for_datastore(student_record))
if project.key != referenced_project_key:
raise ValueError(
'Student response does not correspond to the project: %s != %s.' %
(project_key, referenced_project_key))
properties = {
'parent': project.key.to_old_key(),
'grading_survey_group': survey_group,
'mentor_record': mentor_record,
'student_record': student_record,
'grade_decision': grade_decision or grading_record_model.GRADE_UNDECIDED,
}
properties.update(kwargs)
grading_record = grading_record_model.GSoCGradingRecord(**properties)
grading_record.put()
# update the project and student data
grade_decision = properties['grade_decision']
if grade_decision == grading_record_model.GRADE_PASS:
project.passed_evaluations.append(
ndb.Key.from_old_key(grading_record.key()))
project.put()
student = project.key.parent().get()
student.student_data.number_of_passed_evaluations += 1
student.put()
elif grade_decision == grading_record_model.GRADE_FAIL:
project.failed_evaluation = ndb.Key.from_old_key(grading_record.key())
project.put()
student = project.key.parent().get()
student.student_data.number_of_failed_evaluations += 1
student.put()
return grading_record