app/soc/logic/models/survey_record.py
author Daniel Diniz <ajaksu@gmail.com>
Thu, 02 Jul 2009 10:33:58 +0200
changeset 2478 985fd974e095
parent 2453 9b8f3240e3bf
child 2482 0e544df4d925
permissions -rw-r--r--
Clean up and other fixes for Survey modules. Reviewed by: Lennard de Rijk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""SurveyRecord (Model) query functions.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    25
from google.appengine.ext import db
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    26
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from soc.logic.models import work
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from soc.models.survey_record import SurveyRecord
2446
0cf8f034f52d Extracted the ProjectSurvey and ProjectGradingSurvey records to a new module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2443
diff changeset
    29
from soc.models.grading_project_survey_record import GradingProjectSurveyRecord
0cf8f034f52d Extracted the ProjectSurvey and ProjectGradingSurvey records to a new module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2443
diff changeset
    30
from soc.models.project_survey_record import ProjectSurveyRecord
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
class Logic(work.Logic):
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
  """Logic methods for listing results for Surveys.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  """
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
  def __init__(self, model=SurveyRecord,
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
               base_model=None, scope_logic=None):
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
    """Defines the name, key_name and model for this entity.
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
    """
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
    super(Logic, self).__init__(model=model, base_model=base_model,
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
                                scope_logic=scope_logic)
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    45
  def updateSurveyRecord(self, user, survey, survey_record, fields):
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    46
    """ Create a new survey record, or get an existing one.
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    47
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    48
    params:
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    49
      user = user taking survey
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    50
      survey = survey entity
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    51
      survey_record = existing record, if one exists
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    52
      fields = submitted responses to survey fields
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    53
    """
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    54
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    55
    if survey_record:
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    56
      create = False
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    57
      for prop in survey_record.dynamic_properties():
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    58
        delattr(survey_record, prop)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    59
    else:
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    60
      create = True
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    61
      Record = self.getModel()
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    62
      survey_record = Record(user=user, survey=survey)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    63
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    64
    schema = eval(survey.survey_content.schema)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    65
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    66
    for name, value in fields.items():
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    67
      # TODO(ajaksu) logic below can be improved now we have different models
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    68
      if name == 'project':
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    69
        project = student_project.StudentProject.get(value)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    70
        survey_record.project = project
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    71
      elif name == 'grade':
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    72
        survey_record.grade = GRADES[value]
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    73
      else:
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    74
        pick_multi = name in schema and schema[name]['type'] == 'pick_multi'
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    75
        if pick_multi and hasattr(fields, 'getlist'): # it's a multidict
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    76
          setattr(survey_record, name, ','.join(fields.getlist(name)))
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    77
        else:
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    78
          setattr(survey_record, name, value)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    79
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    80
    # if creating evaluation record, set SurveyRecordGroup
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    81
    db.put(survey_record)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    82
    return survey_record
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    83
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    85
class ProjectLogic(Logic):
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    86
  """Logic class for ProjectSurveyRecord.
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    87
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    88
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    89
  def __init__(self, model=ProjectSurveyRecord,
2453
9b8f3240e3bf Set the correct base_model and scope_logic for extended Survey logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2446
diff changeset
    90
               base_model=SurveyRecord, scope_logic=None):
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    91
    """Defines the name, key_name and model for this entity.
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    92
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    93
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    94
    super(Logic, self).__init__(model=model, base_model=base_model,
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    95
                                scope_logic=scope_logic)
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    96
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    97
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    98
class GradingProjectLogic(ProjectLogic):
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    99
  """Logic class for GradingProjectSurveyRecord
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   100
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   101
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   102
  def __init__(self, model=GradingProjectSurveyRecord,
2453
9b8f3240e3bf Set the correct base_model and scope_logic for extended Survey logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2446
diff changeset
   103
               base_model=ProjectSurveyRecord, scope_logic=None):
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   104
    """Defines the name, key_name and model for this entity.
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   105
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   106
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   107
    super(Logic, self).__init__(model=model, base_model=base_model,
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   108
                                scope_logic=scope_logic)
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   109
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   110
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
logic = Logic()
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   112
project_logic = ProjectLogic()
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   113
grading_logic = GradingProjectLogic()