app/soc/logic/models/survey_record.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 12 Sep 2009 01:22:03 +0200
changeset 2897 c0e78185444c
parent 2590 f0d0c9e7b2d4
permissions -rw-r--r--
Remove __melange_queries logging It causes the local instance to become very slow, as it has to pickle all queries. A better solution would be to store them in the per request value store and then add middleware to store that data at end-of-request.
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
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    25
from soc.logic.models import expando_base
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
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
    27
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
    28
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
    29
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    31
class Logic(expando_base.Logic):
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
  """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
    33
  """
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
  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
    36
               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
    37
    """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
    38
    """
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
    super(Logic, self).__init__(model=model, base_model=base_model,
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    41
                                scope_logic=scope_logic, id_based=True)
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    43
  def updateOrCreateFromFields(self, entity, properties, clear_dynamic=False):
2515
a3513142ae01 Several stylefixes due to received reviews.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2500
diff changeset
    44
    """Create a new SurveyRecord, or update an existing one.
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    45
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    46
    params:
2515
a3513142ae01 Several stylefixes due to received reviews.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2500
diff changeset
    47
      entity: existing SurveyRecord, if one exists
a3513142ae01 Several stylefixes due to received reviews.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2500
diff changeset
    48
      properties: the properties to be set
a3513142ae01 Several stylefixes due to received reviews.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2500
diff changeset
    49
      clear_dynamic: iff True removes all dynamic properties before updating
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    50
    """
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    51
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    52
    if entity:
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    53
      if clear_dynamic:
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    54
        # remove all dynamic properties before we update
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    55
        for prop in entity.dynamic_properties():
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    56
          delattr(entity, prop)
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    57
      self.updateEntityProperties(entity, properties)
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    58
    else:
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    59
      entity = super(Logic, self).updateOrCreateFromFields(properties)
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    60
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    61
    return entity
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
    62
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    64
class ProjectLogic(Logic):
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    65
  """Logic class for ProjectSurveyRecord.
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    66
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    67
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    68
  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
    69
               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
    70
    """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
    71
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    72
2482
0e544df4d925 Added updateOrCreateFromFields method to SurveyRecordLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2478
diff changeset
    73
    super(ProjectLogic, self).__init__(model=model, base_model=base_model,
2590
f0d0c9e7b2d4 Fix indention and add missing dot in docstring in survey models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2515
diff changeset
    74
                                       scope_logic=scope_logic)
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    75
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    76
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    77
class GradingProjectLogic(ProjectLogic):
2590
f0d0c9e7b2d4 Fix indention and add missing dot in docstring in survey models.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2515
diff changeset
    78
  """Logic class for GradingProjectSurveyRecord.
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    79
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    80
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    81
  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
    82
               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
    83
    """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
    84
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    85
2500
ecc16ffe174b Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents: 2482
diff changeset
    86
    super(GradingProjectLogic, self).__init__(model=model,
ecc16ffe174b Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents: 2482
diff changeset
    87
                                              base_model=base_model,
ecc16ffe174b Style fixes and a forgotten parameter in Survey(Record) logic.
Daniel Diniz <ajaksu@gmail.com>
parents: 2482
diff changeset
    88
                                              scope_logic=scope_logic)
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    89
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    90
2439
7fac0da44bbf Put ResultsLogic in its own Logic module named survey_record.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
logic = Logic()
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    92
project_logic = ProjectLogic()
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
    93
grading_logic = GradingProjectLogic()