app/soc/logic/models/survey_record.py
changeset 2442 dd1f94c3594c
parent 2439 7fac0da44bbf
child 2443 2e86dbd47907
equal deleted inserted replaced
2441:a24b2b4af87d 2442:dd1f94c3594c
    20 __authors__ = [
    20 __authors__ = [
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22   ]
    22   ]
    23 
    23 
    24 
    24 
       
    25 from google.appengine.ext import db
       
    26 
    25 from soc.logic.models import work
    27 from soc.logic.models import work
    26 from soc.models.survey_record import SurveyRecord
    28 from soc.models.survey_record import SurveyRecord
       
    29 from soc.models.survey_record import ProjectSurveyRecord
       
    30 from soc.models.survey_record import GradingProjectSurveyRecord
    27 
    31 
    28 
    32 
    29 class Logic(work.Logic):
    33 class Logic(work.Logic):
    30   """Logic methods for listing results for Surveys.
    34   """Logic methods for listing results for Surveys.
    31   """
    35   """
    36     """
    40     """
    37 
    41 
    38     super(Logic, self).__init__(model=model, base_model=base_model,
    42     super(Logic, self).__init__(model=model, base_model=base_model,
    39                                 scope_logic=scope_logic)
    43                                 scope_logic=scope_logic)
    40 
    44 
       
    45   def updateSurveyRecord(self, user, survey, survey_record, fields):
       
    46     """ Create a new survey record, or get an existing one.
       
    47 
       
    48     params:
       
    49       user = user taking survey
       
    50       survey = survey entity
       
    51       survey_record = existing record, if one exists
       
    52       fields = submitted responses to survey fields
       
    53     """
       
    54 
       
    55     if survey_record:
       
    56       create = False
       
    57       for prop in survey_record.dynamic_properties():
       
    58         delattr(survey_record, prop)
       
    59     else:
       
    60       create = True
       
    61       Record = self.getModel()
       
    62       survey_record = Record(user=user, survey=survey)
       
    63 
       
    64     schema = eval(survey.survey_content.schema)
       
    65 
       
    66     for name, value in fields.items():
       
    67       # TODO(ajaksu) logic below can be improved now we have different models
       
    68       if name == 'project':
       
    69         project = student_project.StudentProject.get(value)
       
    70         survey_record.project = project
       
    71       elif name == 'grade':
       
    72         survey_record.grade = GRADES[value]
       
    73       else:
       
    74         pick_multi = name in schema and schema[name]['type'] == 'pick_multi'
       
    75         if pick_multi and hasattr(fields, 'getlist'): # it's a multidict
       
    76           setattr(survey_record, name, ','.join(fields.getlist(name)))
       
    77         else:
       
    78           setattr(survey_record, name, value)
       
    79 
       
    80     # if creating evaluation record, set SurveyRecordGroup
       
    81     db.put(survey_record)
       
    82     return survey_record
       
    83 
    41 
    84 
    42 logic = Logic()
    85 logic = Logic()
       
    86 # TODO separate project and grading logic into own class to overwrite methods
       
    87 project_logic = Logic(model=ProjectSurveyRecord)
       
    88 grading_logic = Logic(model=GradingProjectSurveyRecord)
       
    89 
       
    90 
       
    91 def updateSurveyRecord(user, survey, survey_record, fields):
       
    92   """Create a new survey record, or get an existing one.
       
    93 
       
    94   params:
       
    95     user = user taking survey
       
    96     survey = survey entity
       
    97     survey_record = existing record, if one exists
       
    98     fields = submitted responses to survey fields
       
    99   """
       
   100 
       
   101   # TODO(ajaksu) We should use class information here, but being careful about
       
   102   # compatibility with existent records should the class change.
       
   103   if hasattr(survey_record, 'grade'):
       
   104     record_logic = grading_logic
       
   105   elif hasattr(survey_record, 'project'):
       
   106     record_logic = grading_logic
       
   107   else:
       
   108     record_logic = logic
       
   109 
       
   110   return record_logic.updateSurveyRecord(user, survey, survey_record, fields)