--- a/app/soc/logic/models/survey_record.py Thu Jul 02 12:40:42 2009 +0200
+++ b/app/soc/logic/models/survey_record.py Thu Jul 02 12:43:06 2009 +0200
@@ -22,15 +22,13 @@
]
-from google.appengine.ext import db
-
-from soc.logic.models import work
+from soc.logic.models import expando_base
from soc.models.survey_record import SurveyRecord
from soc.models.grading_project_survey_record import GradingProjectSurveyRecord
from soc.models.project_survey_record import ProjectSurveyRecord
-class Logic(work.Logic):
+class Logic(expando_base.Logic):
"""Logic methods for listing results for Surveys.
"""
@@ -40,46 +38,27 @@
"""
super(Logic, self).__init__(model=model, base_model=base_model,
- scope_logic=scope_logic)
+ scope_logic=scope_logic, id_based=True)
- def updateSurveyRecord(self, user, survey, survey_record, fields):
- """ Create a new survey record, or get an existing one.
+ def updateOrCreateFromFields(self, entity, properties, clear_dynamic=False):
+ """ Create a new SurveyRecord, or update an existing one.
params:
- user = user taking survey
- survey = survey entity
- survey_record = existing record, if one exists
- fields = submitted responses to survey fields
+ entity = existing SurveyRecord, if one exists
+ properties = the properties to be set
+ clear_dynamic = iff True removes all dynamic properties before updating
"""
- if survey_record:
- create = False
- for prop in survey_record.dynamic_properties():
- delattr(survey_record, prop)
+ if entity:
+ if clear_dynamic:
+ # remove all dynamic properties before we update
+ for prop in entity.dynamic_properties():
+ delattr(entity, prop)
+ self.updateEntityProperties(entity, properties)
else:
- create = True
- Record = self.getModel()
- survey_record = Record(user=user, survey=survey)
-
- schema = eval(survey.survey_content.schema)
+ entity = super(Logic, self).updateOrCreateFromFields(properties)
- for name, value in fields.items():
- # TODO(ajaksu) logic below can be improved now we have different models
- if name == 'project':
- project = student_project.StudentProject.get(value)
- survey_record.project = project
- elif name == 'grade':
- survey_record.grade = GRADES[value]
- else:
- pick_multi = name in schema and schema[name]['type'] == 'pick_multi'
- if pick_multi and hasattr(fields, 'getlist'): # it's a multidict
- setattr(survey_record, name, ','.join(fields.getlist(name)))
- else:
- setattr(survey_record, name, value)
-
- # if creating evaluation record, set SurveyRecordGroup
- db.put(survey_record)
- return survey_record
+ return entity
class ProjectLogic(Logic):
@@ -91,7 +70,7 @@
"""Defines the name, key_name and model for this entity.
"""
- super(Logic, self).__init__(model=model, base_model=base_model,
+ super(ProjectLogic, self).__init__(model=model, base_model=base_model,
scope_logic=scope_logic)
@@ -104,7 +83,7 @@
"""Defines the name, key_name and model for this entity.
"""
- super(Logic, self).__init__(model=model, base_model=base_model,
+ super(GradingProjectLogic, self).__init__(model=model, base_model=base_model,
scope_logic=scope_logic)