app/soc/views/models/grading_project_survey.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 11 Jul 2009 23:29:22 +0200
changeset 2598 59ebf718c6a4
parent 2596 56924ec61cd2
child 2659 8df08a3b17db
permissions -rw-r--r--
Changed URL pattern for starting sending out survey reminders.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Views for GradingProjectSurveys.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
2528
9de8348890d8 Added Daniel Diniz to authors for Grading Project Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2527
diff changeset
    21
  '"Daniel Diniz" <ajaksu@gmail.com>',
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
    22
  '"James Levy" <jamesalexanderlevy@gmail.com>',
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
  ]
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
2528
9de8348890d8 Added Daniel Diniz to authors for Grading Project Survey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2527
diff changeset
    26
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    27
from django import forms
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
from soc.logic import dicts
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    30
from soc.logic.models.survey import GRADES
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
from soc.logic.models.survey import grading_logic as grading_survey_logic
2532
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
    32
from soc.logic.models.user import logic as user_logic
2452
4abc27894b61 Added two missing imports to the Project and Grading Surveys views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2448
diff changeset
    33
from soc.views.helper import access
4abc27894b61 Added two missing imports to the Project and Grading Surveys views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2448
diff changeset
    34
from soc.views.helper import decorators
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    35
from soc.views.helper import surveys
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
from soc.views.models import project_survey
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
class View(project_survey.View):
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
  """View methods for the GradingProjectSurvey model.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
  """
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
  def __init__(self, params=None):
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
    """Defines the fields and methods required for the base View class
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
    to provide the user with list, public, create, edit and delete views.
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
    Params:
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
      params: a dict with params for this View
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
    """
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
    rights = access.Checker(params)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
    rights['any_access'] = ['allow']
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
    rights['show'] = [('checkIsSurveyReadable', grading_survey_logic)]
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
    rights['create'] = ['checkIsUser']
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
    rights['edit'] = [('checkIsSurveyWritable', grading_survey_logic)]
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2532
diff changeset
    56
    rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
    rights['list'] = ['checkDocumentList']
2576
7a1138f8a0e2 Removed results list from Survey edit.
Daniel Diniz <ajaksu@gmail.com>
parents: 2564
diff changeset
    58
    rights['results'] = [('checkIsSurveyWritable', grading_survey_logic)]
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2535
diff changeset
    59
    rights['take'] = [('checkIsSurveyTakeable', grading_survey_logic),
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2535
diff changeset
    60
                      ('checkIsAllowedToTakeProjectSurveyAs',
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2535
diff changeset
    61
                       [grading_survey_logic, 'mentor', 'project'])]
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
    new_params = {}
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
    new_params['logic'] = grading_survey_logic
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
    new_params['rights'] = rights
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
    new_params['name'] = "Grading Project Survey"
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
2596
56924ec61cd2 Added a view to start sending out a reminder.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2576
diff changeset
    69
    # used for sending reminders
56924ec61cd2 Added a view to start sending out a reminder.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2576
diff changeset
    70
    new_params['survey_type'] = 'grading'
56924ec61cd2 Added a view to start sending out a reminder.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2576
diff changeset
    71
2512
2e8ccec037a9 Sub_merge params for GradingProjectSurvey View.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2511
diff changeset
    72
    params = dicts.merge(params, new_params, sub_merge=True)
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
    super(View, self).__init__(params=params)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    76
  # TODO: work on grade activation
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    77
  def activate(self, request, **kwargs):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    78
    """This is a hack to support the 'Enable grades' button.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    79
    """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    80
    self.activateGrades(request)
2513
c6de2d3eae90 Style fixes to the GradingProjectSurvey View module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2512
diff changeset
    81
    redirect_path = request.path.replace('/activate/', '/edit/') + (
c6de2d3eae90 Style fixes to the GradingProjectSurvey View module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2512
diff changeset
    82
        '?activate=1')
2478
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    83
    return http.HttpResponseRedirect(redirect_path)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    84
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    85
  def activateGrades(self, request, **kwargs):
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    86
    """Updates SurveyRecord's grades for a given Survey.
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    87
    """
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    88
    survey_key_name = survey_logic.getKeyNameFromPath(request.path)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    89
    survey = Survey.get_by_key_name(survey_key_name)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    90
    survey_logic.activateGrades(survey)
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    91
    return
985fd974e095 Clean up and other fixes for Survey modules.
Daniel Diniz <ajaksu@gmail.com>
parents: 2452
diff changeset
    92
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
    93
  def _getSurveyTakeForm(self, survey, record, params, post_dict=None):
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    94
    """Returns the specific SurveyTakeForm needed for the take view.
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
    95
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
    96
    Args:
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    97
        survey: a Survey entity
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    98
        record: a SurveyRecord instance if any exist
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
    99
        params: the params dict for the requesting View
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   100
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   101
    Returns:
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   102
        An instance of GradseSurveyTakeForm.
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   103
    """
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   104
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   105
    grade_choices = (('pass', 'Pass'), ('fail', 'Fail'))
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   106
    survey_form = GradeSurveyTakeForm(survey_content=survey.survey_content,
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   107
                                      survey_record=record,
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   108
                                      survey_logic=params['logic'],
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   109
                                      grade_choices=grade_choices,
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   110
                                      data=post_dict)
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   111
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   112
    return survey_form
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   113
2532
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   114
  def _constructFilterForProjectSelection(self, survey, params):
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   115
    """Returns the filter needed for the Project selection view.
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   116
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   117
    Constructs a filter that returns all valid projects for which the current
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   118
    user is the mentor. Only for the projects in the program given by the
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   119
    survey's scope of course.
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   120
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   121
    For args see project_survey.View._constructFilterForProjectSelection().
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   122
    """
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   123
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   124
    from soc.logic.models.mentor import logic as mentor_logic
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   125
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   126
    survey_logic = params['logic']
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   127
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   128
    user_entity = user_logic.getForCurrentAccount()
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   129
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   130
    # get the mentor entities for the current user and program
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   131
    fields = {'user': user_entity,
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   132
              'program': survey_logic.getScope(survey),
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   133
              'status': 'active'}
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   134
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   135
    mentor_entities = mentor_logic.getForFields(fields)
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   136
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   137
    # TODO: Ensure that this doesn't break when someone is a mentor for
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   138
    # a lot of organizations.
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   139
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   140
    # TODO(ljvderijk) transform StudentProject to handle multiple surveys
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   141
    fields = {'mentor': mentor_entities,
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   142
              'status': 'accepted'}
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   143
0b4dbe4b3fb7 Fixed showing the correct list of projects for taking a Grading Project Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2528
diff changeset
   144
    return fields
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   145
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   146
class GradeSurveyTakeForm(surveys.SurveyTakeForm):
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   147
  """Extends SurveyTakeForm by adding a grade field.
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   148
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   149
  The grade field logic is dependent on the kwarg 'grade_choices' (behavior
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   150
  should be the same as the base class's if this argument is missing).
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   151
  """
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   152
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   153
  def __init__(self, *args, **kwargs):
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   154
    """Store grade choices and initialize the form.
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   155
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   156
    params:
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   157
      grade_choices: pair of tuples representing the grading choices
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   158
    """
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   159
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   160
    self.grade_choices = kwargs.pop('grade_choices', None)
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   161
2564
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   162
    super(GradeSurveyTakeForm, self).__init__(*args, **kwargs)
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   163
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   164
  def setCleaners(self, post_dict=None):
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   165
    """Ensures that the grade field is added to the clean data.
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   166
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   167
    For args see surveys.SurveyTakeForm.setCleaners().
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   168
    """
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   169
2564
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   170
    clean_data = super(GradeSurveyTakeForm, self).setCleaners(
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   171
        post_dict=post_dict)
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   172
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   173
    if post_dict:
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   174
      clean_data['grade'] = post_dict.get('grade', None)
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   175
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   176
    return clean_data
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   177
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   178
  def clean_grade(self):
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   179
    """Validate the grade field.
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   180
    """
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   181
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   182
    grade = self.cleaned_data['grade']
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   183
    # map to bool
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   184
    grade_vals = {'pass': True, 'fail': False, '': ''}
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   185
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   186
    return grade_vals.get(grade, None)
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   187
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   188
  def getFields(self, post_dict=None):
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   189
    """Add the extra grade field's value from POST or survey_record.
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   190
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   191
    Args:
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   192
        post_dict: dict with POST data if exists
2513
c6de2d3eae90 Style fixes to the GradingProjectSurvey View module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2512
diff changeset
   193
    """
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   194
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   195
    # fetch value from post_dict if it's there
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   196
    post_dict = post_dict or {}
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   197
    grade = post_dict.get('grade', None)
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   198
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   199
    # otherwise, try to fetch from survey_record
2564
81b36f56d61a Ensure that the Grade field data stays in clean_data.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2542
diff changeset
   200
    if grade == None and hasattr(self.survey_record, 'grade'):
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   201
      grade = self.survey_record.grade
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   202
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   203
    # remap bool to string values as the ChoiceField validates on 'choices'.
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   204
    vals_grade = {True: 'pass', False: 'fail'}
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   205
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   206
    if self.grade_choices:
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   207
      self.data['grade'] = vals_grade.get(grade, None) or grade
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   208
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   209
    return super(GradeSurveyTakeForm, self).getFields(post_dict)
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   210
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   211
  def insertFields(self):
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   212
    """Add ordered fields to self.fields, add grade field with grade choices.
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   213
    """
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   214
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   215
    # add common survey fields
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   216
    fields = super(GradeSurveyTakeForm, self).insertFields()
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   217
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   218
    if self.grade_choices:
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   219
      # add empty option to choices
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   220
      grade_choices = (('', "Choose a Grade"),) + tuple(self.grade_choices)
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   221
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   222
      gradeField = forms.fields.ChoiceField(choices=grade_choices,
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   223
                                            required=True,
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   224
                                            widget=forms.Select(),
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   225
                                            initial=self.data.get('grade'))
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   226
      # add the grade field at the form's bottom
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   227
      fields.insert(len(fields) + 1, 'grade', gradeField)
2527
ce657149b90a Add a grade field to the Grading Project Survey form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2513
diff changeset
   228
2542
a9dec4763c6b POST data always overwrites survey_record data when used to populate form.
Daniel Diniz <ajaksu@gmail.com>
parents: 2536
diff changeset
   229
    return fields
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   230
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   231
2448
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   232
view = View()
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   233
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   234
create = decorators.view(view.create)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   235
edit = decorators.view(view.edit)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   236
delete = decorators.view(view.delete)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   237
list = decorators.view(view.list)
325e08ee9d25 Added skeleton view for ProjectSurvey and GradingProjectSurvey.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   238
public = decorators.view(view.public)
2576
7a1138f8a0e2 Removed results list from Survey edit.
Daniel Diniz <ajaksu@gmail.com>
parents: 2564
diff changeset
   239
results = decorators.view(view.viewResults)
2596
56924ec61cd2 Added a view to start sending out a reminder.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2576
diff changeset
   240
send_reminder = decorators.view(view.sendReminder)
2511
0e53ad183df9 Added rudimentary grade field to GradingProjectSurvey take View.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2478
diff changeset
   241
take = decorators.view(view.take)