app/soc/logic/models/survey.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 03 Jul 2009 12:00:53 +0200
changeset 2497 996f381d458c
parent 2480 0079e1038740
child 2500 ecc16ffe174b
permissions -rw-r--r--
Added _getSurveyRecordFor method to Survey View. This method is used by the Take view to get the right SurveyRecord for the current request. Subclassed Views that want to fetch their records in another way can override this method.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     2
#
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     4
#
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     8
#
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    10
#
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    16
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    17
"""Survey (Model) query functions.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    18
"""
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    19
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    20
__authors__ = [
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    21
  '"Daniel Diniz" <ajaksu@gmail.com>',
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    22
  '"James Levy" <jamesalexanderlevy@gmail.com>',
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    23
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    24
  ]
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    25
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    26
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    27
import logging
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    28
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    29
from google.appengine.ext import db
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    30
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    31
from soc.cache import sidebar
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    32
from soc.logic.models import linkable as linkable_logic
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    33
from soc.logic.models import survey_record as survey_record_logic
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    34
from soc.logic.models.user import logic as user_logic
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    35
from soc.logic.models import work
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    36
from soc.models.program import Program
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    37
from soc.models import student_project
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    38
from soc.models.survey import Survey
2444
6276c3340c30 Separated two survey types out of the main survey model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2443
diff changeset
    39
from soc.models.grading_project_survey import GradingProjectSurvey
6276c3340c30 Separated two survey types out of the main survey model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2443
diff changeset
    40
from soc.models.project_survey import ProjectSurvey
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    41
from soc.models.survey import SurveyContent
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    42
from soc.models.survey_record import SurveyRecord
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    43
from soc.models.survey_record_group import SurveyRecordGroup
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    44
from soc.models.work import Work
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    45
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    46
#TODO(James): Ensure this facilitates variable # of surveys 
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    47
GRADES = {'pass': True, 'fail': False}
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    48
PROJECT_STATUSES = {
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    49
'accepted': {True: 'mid_term_passed', False: 'mid_term_failed'},
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    50
'mid_term_passed': {True: 'passed', False: 'final_failed'}
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    51
}
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    52
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    53
class Logic(work.Logic):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    54
  """Logic methods for the Survey model.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    55
  """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    56
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    57
  def __init__(self, model=Survey, base_model=Work,
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    58
               scope_logic=linkable_logic,
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    59
               record_logic=survey_record_logic.logic):
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    60
    """Defines the name, key_name and model for this entity.
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    61
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    62
    params:
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    63
      record_logic: SurveyRecordLogic (or subclass) instance for this Survey
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    64
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    65
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    66
    self.record_logic = record_logic
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
    67
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    68
    super(Logic, self).__init__(model=model, base_model=base_model,
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    69
                                scope_logic=scope_logic)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    70
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    71
  def createSurvey(self, survey_fields, schema, survey_content=False):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    72
    """Create a new survey from prototype.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    73
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    74
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    75
      survey_fields = dict of survey field items (see SurveyContent model)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    76
      schema = metadata about survey fields (SurveyContent.schema)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    77
      survey_content = existing SurveyContent entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    78
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    79
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    80
    if not survey_content:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    81
      survey_content = SurveyContent()
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    82
    else:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    83
      # wipe clean existing dynamic properties if they exist
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    84
      for prop in survey_content.dynamic_properties():
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    85
        delattr(survey_content, prop)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    86
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    87
    for name, value in survey_fields.items():
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    88
      setattr(survey_content, name, value)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    89
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    90
    survey_content.schema = str(schema)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    91
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    92
    db.put(survey_content)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    93
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    94
    return survey_content
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
    95
2460
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
    96
  def getSurveyForContent(self, survey_content):
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
    97
    """Returns the Survey belonging to the given SurveyContent.
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
    98
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
    99
    params:
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   100
      survey_content: the SurveyContent to retrieve the Survey for.
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   101
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   102
    returns:
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   103
      Survey or subclass if possible else None.
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   104
    """
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   105
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   106
    fields = {'survey_content': survey_content}
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   107
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   108
    return self.getForFields(fields, unique=True)
b8b0c631bcfe Added getSurveyForContent method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2459
diff changeset
   109
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   110
  def getRecordLogic(self):
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   111
    """Returns SurveyRecordLogic that belongs to this SurveyLogic.
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   112
    """
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   113
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   114
    return self.record_logic
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   115
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   116
  def getUserRole(self, user, survey, project):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   117
    """Gets the role of a user for a project, used for SurveyRecordGroup.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   118
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   119
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   120
      user: user taking survey
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   121
      survey: survey entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   122
      project: student project for this user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   123
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   124
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   125
    if survey.taking_access == 'mentor evaluation':
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   126
      mentors = self.getMentorforProject(user, project)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   127
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   128
      if len(mentors) < 1 or len(mentors) > 1:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   129
        logging.warning('Unable to determine mentor for \
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   130
        user %s. Results returned: %s ' % (
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   131
        user.key().name(), str(mentors)) )
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   132
        return False
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   133
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   134
      this_mentor = mentors[0]
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   135
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   136
    if survey.taking_access == 'student evaluation':
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   137
      students = self.getStudentforProject(user, project)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   138
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   139
      if len(students) < 1 or len(students) > 1:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   140
        logging.warning('Unable to determine student for \
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   141
        user %s. Results returned: %s ' % (
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   142
        user.key().name(), str(students)) )
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   143
        return False
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   144
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   145
  def getStudentforProject(self, user, project):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   146
    """Get student projects for a given User.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   147
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   148
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   149
      user = survey taking user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   150
      project = survey taker's student project
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   151
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   152
    from soc.logic.models.student import logic as student_logic
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   153
    import soc.models.student
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   154
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   155
    # TODO this should be done per Student or Program
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   156
    # TODO filter for accepted, midterm_passed, etc?
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   157
    user_students = student_logic.getForFields({'user': user}) 
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   158
    if not user_students: return []
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   159
    return set([project.student for project in sum(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   160
    (list(s.student_projects.run())
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   161
    for s in user_students), []) if project.key() == project.key()])
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   162
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   163
  def getMentorforProject(self, user, project):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   164
    """Get Student Projects that are being mentored by the given User.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   165
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   166
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   167
      user = survey taking user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   168
      project = survey taker's student project
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   169
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   170
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   171
    from soc.logic.models.mentor import logic as mentor_logic
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   172
    import soc.models.mentor
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   173
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   174
    # TODO filter for accepted, midterm_passed, etc?
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   175
    # TODO this should be done a program basis not user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   176
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   177
    user_mentors = mentor_logic.getForFields({'user': user}) 
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   178
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   179
    if not user_mentors:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   180
      return []
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   181
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   182
    return set([project.mentor for project in sum(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   183
            (list(mentor.student_projects.run())
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   184
             for mentor in user_mentors), [])
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   185
        if project.key() == project.key()])
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   186
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   187
  def activateGrades(self, survey):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   188
    """Activates the grades on a Grading Survey.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   189
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   190
    TODO(James) Fix this Docstring
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   191
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   192
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   193
      survey = survey entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   194
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   195
    if survey.taking_access != "mentor evaluation":
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   196
      logging.error("Cannot grade survey %s with taking access %s"
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   197
      % (survey.key().name(), survey.taking_access))
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   198
      return False
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   199
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   200
    program = survey.scope or Program.get_by_key_name(survey.scope_path)
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   201
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   202
    for project in program.student_projects.fetch(1000):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   203
      this_record_group = SurveyRecordGroup.all().filter(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   204
      "project = ", project).filter(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   205
      "initial_status = ", project.status).get()
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   206
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   207
      if not this_record_group:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   208
         logging.warning('neither mentor nor student has \
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   209
         taken the survey for project %s' % project.key().name() )
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   210
         continue
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   211
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   212
      if not this_record_group.mentor_record:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   213
        # student has taken survey, but not mentor
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   214
        logging.warning('not continuing without mentor record...')
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   215
        continue
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   216
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   217
      status_options = PROJECT_STATUSES.get(project.status)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   218
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   219
      if not status_options:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   220
        logging.warning('unable to find status options for project \
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   221
        status %s' % project.status)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   222
        continue
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   223
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   224
      new_project_grade = this_record_group.mentor_record.grade
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   225
      new_project_status = status_options.get(new_project_grade)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   226
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   227
      if getattr(this_record_group, 'final_status'):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   228
         logging.warning('project %s record group should not \
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   229
         yet have a final status %s' % (
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   230
         project.key().name(), this_record_group.final_status ) )
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   231
         continue
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   232
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   233
      # assign the new status to the project and surveyrecordgroup
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   234
      project.status = new_project_status
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   235
      this_record_group.final_status = new_project_status
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   236
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   237
  def getKeyNameFromPath(self, path):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   238
    """Gets survey key name from a request path.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   239
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   240
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   241
      path = path of the current request
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   242
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   243
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   244
    # TODO determine if kwargs in the request contains this information
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   245
    return '/'.join(path.split('/')[-4:]).split('?')[0]
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   246
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   247
  def getProjects(self, survey, user):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   248
    """Get projects linking user to a program.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   249
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   250
    Serves as access handler (since no projects == no access).
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   251
    And retrieves projects to choose from (if mentors have >1 projects).
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   252
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   253
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   254
      survey = survey entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   255
      user = survey taking user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   256
    """
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   257
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   258
    this_program = survey.scope or Program.get_by_key_name(survey.scope_path)
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   259
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   260
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   261
    if 'mentor' in survey.taking_access:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   262
      these_projects = self.getMentorProjects(user, this_program)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   263
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   264
    elif 'student' in survey.taking_access:
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   265
      these_projects = self.getStudentProjects(user, this_program)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   266
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   267
    logging.info(these_projects)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   268
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   269
    if len(these_projects) == 0:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   270
      return False
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   271
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   272
    return these_projects
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   273
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   274
  def getDebugUser(self, survey, this_program):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   275
    """Debugging method impersonates other roles.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   276
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   277
    Tests taking survey, saving response, and grading.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   278
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   279
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   280
      survey = survey entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   281
      this_program = program scope of survey
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   282
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   283
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   284
    if 'mentor' in survey.taking_access:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   285
      from soc.models.mentor import Mentor
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   286
      role = Mentor.get_by_key_name(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   287
      this_program.key().name() + "/org_1/test")
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   288
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   289
    if 'student' in survey.taking_access:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   290
      from soc.models.student import Student
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   291
      role = Student.get_by_key_name(
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   292
      this_program.key().name() + "/test")
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   293
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   294
    if role: return role.user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   295
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   296
  def getKeyValuesFromEntity(self, entity):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   297
    """See base.Logic.getKeyNameValues.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   298
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   299
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   300
    return [entity.prefix, entity.scope_path, entity.link_id]
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   301
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   302
  def getKeyValuesFromFields(self, fields):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   303
    """See base.Logic.getKeyValuesFromFields.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   304
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   305
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   306
    return [fields['prefix'], fields['scope_path'], fields['link_id']]
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   307
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   308
  def getKeyFieldNames(self):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   309
    """See base.Logic.getKeyFieldNames.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   310
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   311
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   312
    return ['prefix', 'scope_path', 'link_id']
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   313
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   314
  def getScope(self, entity):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   315
    """Gets Scope for entity.
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   316
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   317
    params:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   318
      entity = Survey entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   319
    """
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   320
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   321
    if getattr(entity, 'scope', None):
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   322
      return entity.scope
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   323
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   324
    import soc.models.program
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   325
    import soc.models.organization
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   326
    import soc.models.user
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   327
    import soc.models.site
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   328
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   329
    # use prefix to generate dict key
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   330
    scope_types = {"program": soc.models.program.Program,
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   331
    "org": soc.models.organization.Organization,
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   332
    "user": soc.models.user.User,
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   333
    "site": soc.models.site.Site}
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   334
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   335
    # determine the type of the scope
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   336
    scope_type = scope_types.get(entity.prefix)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   337
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   338
    if not scope_type:
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   339
      # no matching scope type found
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   340
      raise AttributeError('No Matching Scope type found for %s' % entity.prefix)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   341
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   342
    # set the scope and update the entity
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   343
    entity.scope = scope_type.get_by_key_name(entity.scope_path)
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   344
    entity.put()
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   345
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   346
    # return the scope
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   347
    return entity.scope
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   348
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   349
class ProjectLogic(Logic):
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   350
  """Logic class for ProjectSurvey.
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   351
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   352
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   353
  def __init__(self, model=ProjectSurvey,
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   354
               base_model=Survey, scope_logic=linkable_logic,
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   355
               record_logic=survey_record_logic.project_logic):
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   356
    """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
   357
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   358
2459
accd8ba94b44 Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   359
    super(ProjectLogic, self).__init__(model=model, base_model=base_model,
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   360
                                       scope_logic=scope_logic,
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   361
                                       record_logic=record_logic)
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   362
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   363
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   364
class GradingProjectLogic(ProjectLogic):
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   365
  """Logic class for GradingProjectSurvey
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   366
  """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   367
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   368
  def __init__(self, model=GradingProjectSurvey,
2462
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   369
               base_model=ProjectSurvey, scope_logic=linkable_logic,
2d5d2961f774 Added getRecordLogic method to SurveyLogic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2460
diff changeset
   370
               record_logic=survey_record_logic.grading_logic):
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   371
    """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
   372
    """
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   373
2459
accd8ba94b44 Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   374
    super(GradingProjectLogic, self).__init__(model=model,
accd8ba94b44 Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   375
                                              base_model=base_model,
accd8ba94b44 Calling the correct super in Project and Grading Survey Logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2455
diff changeset
   376
                                              scope_logic=scope_logic)
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   377
2442
dd1f94c3594c Start on adding ProjectSurvey and GradingProjectSurvey.
Daniel Diniz <ajaksu@gmail.com>
parents: 2439
diff changeset
   378
2431
800a020c9bcf Added logic for Survey and SurveyRecord.
James Levy <jamesalexanderlevy@gmail.com>
parents:
diff changeset
   379
logic = Logic()
2443
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   380
project_logic = ProjectLogic()
2e86dbd47907 Seperated Logic for ProjectSurvey and GradingProject surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2442
diff changeset
   381
grading_logic = GradingProjectLogic()