app/soc/tasks/grading_survey_group.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Mon, 02 Nov 2009 23:38:43 +0100
changeset 3074 ebda36efbd61
parent 2647 69ac7307bb50
permissions -rw-r--r--
HtmlSanitizer becomes Python 2.6 compatible. The Cleaner class must not have any arguments when calling __init__ function for the object class, because in this case Python 2.6 raises TypeError (while previous versions just ignored them).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Tasks related to Grading Survey Groups.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
import datetime
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
import logging
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from google.appengine.api.labs import taskqueue
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
from django import http
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
from soc.tasks.helper import error_handler
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
# batch size to use when going through StudentProjects
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
DEF_BATCH_SIZE = 10
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
def getDjangoURLPatterns():
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
  """Returns the URL patterns for the tasks in this module.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
  """
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
  patterns = [(
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
      r'tasks/grading_survey_group/update_records$',
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
      'soc.tasks.grading_survey_group.updateOrCreateRecordsForSurveyGroup'),
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
              (
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
      r'tasks/grading_survey_group/update_projects$',
2647
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
    48
      'soc.tasks.grading_survey_group.updateProjectsForSurveyGroup'),
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
    49
              (
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
    50
      r'tasks/grading_survey_group/mail_result$',
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
    51
      'soc.tasks.grading_survey_group.sendMailAboutGradingRecordResult')]
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
  return patterns
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
def updateOrCreateRecordsForSurveyGroup(request, *args, **kwargs):
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
  """Updates or creates GradingRecords for the given GradingSurveyGroup.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
  Expects the following to be present in the POST dict:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
    group_key: Specifies the GradingSurveyGroup key name.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
    project_key: optional to specify which project was the last for which this
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
                 task was run
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
  Args:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
    request: Django Request object
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
  """
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
  from soc.logic.models.grading_record import logic as grading_record_logic
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
  from soc.logic.models.grading_survey_group import logic as survey_group_logic
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
  from soc.logic.models.student_project import logic as student_project_logic
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
  post_dict = request.POST
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
  group_key = post_dict.get('group_key')
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
  if not group_key:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
    # invalid task data, log and return OK
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
    return error_handler.logErrorAndReturnOK(
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
        'Invalid updateRecordForSurveyGroup data: %s' % post_dict)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
  # get the GradingSurveyGroup for the given keyname
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
  survey_group_entity = survey_group_logic.getFromKeyName(group_key)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
  if not survey_group_entity:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
    # invalid GradingSurveyGroup specified, log and return OK
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
    return error_handler.logErrorAndReturnOK(
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
        'Invalid GradingSurveyGroup specified: %s' % group_key)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    88
  # check and retrieve the project_key that has been done last
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    89
  if 'project_key' in post_dict:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
    project_start_key = post_dict['project_key']
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
  else:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
    project_start_key = None
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
  # get all valid StudentProjects from starting key
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
  fields = {'program': survey_group_entity.scope,
2639
9b05c44316f9 Withdrawn projects are removed from GradingRecord creation/update task.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2631
diff changeset
    96
            'status': ['accepted', 'failed', 'completed']}
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    97
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
  if project_start_key:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
    # retrieve the last project that was done
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
    project_start = student_project_logic.getFromKeyName(project_start_key)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
    if not project_start:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
      # invalid starting project key specified, log and return OK
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   104
      return error_handler.logErrorAndReturnOK(
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
          'Invalid Student Project Key specified: %s' %(project_start_key))
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   107
    fields['__key__ >'] = project_start.key()
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
  # get the first batch_size number of StudentProjects
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
  project_entities = student_project_logic.getForFields(fields,
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
                                                        limit=DEF_BATCH_SIZE)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   112
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   113
  # update/create and batch put the new GradingRecords
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   114
  grading_record_logic.updateOrCreateRecordsFor(survey_group_entity,
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   115
                                                project_entities)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   116
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   117
  if len(project_entities) == DEF_BATCH_SIZE:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   118
    # spawn new task starting from the last
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   119
    new_project_start = project_entities[DEF_BATCH_SIZE-1].key().id_or_name()
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   120
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   121
    # pass along these params as POST to the new task
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   122
    task_params = {'group_key': group_key,
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   123
                   'project_key': new_project_start}
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   124
    task_url = '/tasks/grading_survey_group/update_records'
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   125
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   126
    new_task = taskqueue.Task(params=task_params, url=task_url)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   127
    new_task.add()
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   128
  else:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   129
    # task completed, update timestamp for last update complete
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   130
    fields = {'last_update_complete': datetime.datetime.now()}
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   131
    survey_group_logic.updateEntityProperties(survey_group_entity, fields)
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   132
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   133
  # task completed, return OK
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   134
  return http.HttpResponse('OK')
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   135
2631
f0d3d24a609b Fixed style error in GradingSurveyGroup Task model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2618
diff changeset
   136
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   137
def updateProjectsForSurveyGroup(request, *args, **kwargs):
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   138
  """Updates each StudentProject for which a GradingRecord is found.
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   139
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   140
  Expects the following to be present in the POST dict:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   141
    group_key: Specifies the GradingSurveyGroup key name.
2644
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   142
    record_key: Optional, specifies the key of the last processed
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   143
                GradingRecord.
2647
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   144
    send_mail: Optional, if this string evaluates to True mail will be send
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   145
               for each GradingRecord that's processed.
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   146
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   147
  Args:
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   148
    request: Django Request object
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   149
  """
2644
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   150
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   151
  from soc.logic.models.grading_record import logic as grading_record_logic
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   152
  from soc.logic.models.grading_survey_group import logic as survey_group_logic
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   153
  from soc.logic.models.student_project import logic as student_project_logic
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   154
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   155
  post_dict = request.POST
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   156
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   157
  group_key = post_dict.get('group_key')
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   158
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   159
  if not group_key:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   160
    # invalid task data, log and return OK
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   161
    return error_handler.logErrorAndReturnOK(
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   162
        'Invalid updateRecordForSurveyGroup data: %s' % post_dict)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   163
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   164
  # get the GradingSurveyGroup for the given keyname
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   165
  survey_group_entity = survey_group_logic.getFromKeyName(group_key)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   166
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   167
  if not survey_group_entity:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   168
    # invalid GradingSurveyGroup specified, log and return OK
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   169
    return error_handler.logErrorAndReturnOK(
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   170
        'Invalid GradingSurveyGroup specified: %s' % group_key)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   171
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   172
  # check and retrieve the record_key that has been done last
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   173
  if 'record_key' in post_dict and post_dict['record_key'].isdigit():
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   174
    record_start_key = int(post_dict['record_key'])
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   175
  else:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   176
    record_start_key = None
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   177
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   178
  # get all valid StudentProjects from starting key
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   179
  fields = {'grading_survey_group': survey_group_entity}
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   180
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   181
  if record_start_key:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   182
    # retrieve the last record that was done
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   183
    record_start = grading_record_logic.getFromID(record_start_key)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   184
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   185
    if not record_start:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   186
      # invalid starting record key specified, log and return OK
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   187
      return error_handler.logErrorAndReturnOK(
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   188
          'Invalid GradingRecord Key specified: %s' %(record_start_key))
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   189
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   190
    fields['__key__ >'] = record_start.key()
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   191
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   192
  # get the first batch_size number of GradingRecords
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   193
  record_entities = grading_record_logic.getForFields(fields,
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   194
                                                      limit=DEF_BATCH_SIZE)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   195
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   196
  student_project_logic.updateProjectsForGradingRecords(record_entities)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   197
2647
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   198
  # check if we need to send an email for each GradingRecord
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   199
  send_mail = post_dict.get('send_mail', '')
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   200
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   201
  if send_mail:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   202
    # enqueue a task to send mail for each GradingRecord
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   203
    for record_entity in record_entities:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   204
      # pass along these params as POST to the new task
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   205
      task_params = {'record_key': record_entity.key().id_or_name()}
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   206
      task_url = '/tasks/grading_survey_group/mail_result'
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   207
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   208
      mail_task = taskqueue.Task(params=task_params, url=task_url)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   209
      mail_task.add('mail')
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   210
2644
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   211
  if len(record_entities) == DEF_BATCH_SIZE:
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   212
    # spawn new task starting from the last
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   213
    new_record_start = record_entities[DEF_BATCH_SIZE-1].key().id_or_name()
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   214
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   215
    # pass along these params as POST to the new task
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   216
    task_params = {'group_key': group_key,
2647
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   217
                   'record_key': new_record_start,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   218
                   'send_mail': send_mail}
2644
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   219
    task_url = '/tasks/grading_survey_group/update_projects'
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   220
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   221
    new_task = taskqueue.Task(params=task_params, url=task_url)
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   222
    new_task.add()
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   223
0eb27bb0122d Added Task to process GradingRecords and update their StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2639
diff changeset
   224
  # task completed, return OK
2618
6a3c464f871f Added Tasks to update/create GradingRecords for a given GradingSurveyGroup.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   225
  return http.HttpResponse('OK')
2647
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   226
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   227
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   228
def sendMailAboutGradingRecordResult(request, *args, **kwargs):
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   229
  """Sends out a mail about the result of one GradingRecord.
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   230
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   231
  Expects the following to be present in the POST dict:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   232
    record_key: Specifies the key for the record to process.
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   233
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   234
  Args:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   235
    request: Django Request object
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   236
  """
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   237
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   238
  from soc.logic import mail_dispatcher
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   239
  from soc.logic.models.grading_record import logic as grading_record_logic
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   240
  from soc.logic.models.org_admin import logic as org_admin_logic
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   241
  from soc.logic.models.site import logic as site_logic
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   242
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   243
  post_dict = request.POST
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   244
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   245
  # check and retrieve the record_key that has been done last
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   246
  if 'record_key' in post_dict and post_dict['record_key'].isdigit():
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   247
    record_key = int(post_dict['record_key'])
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   248
  else:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   249
    record_key = None
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   250
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   251
  if not record_key:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   252
    # no GradingRecord key specified, log and return OK
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   253
    error_handler.logErrorAndReturnOK(
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   254
        'No valid record_key specified in POST data: %s' % request.POST)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   255
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   256
  record_entity = grading_record_logic.getFromID(record_key)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   257
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   258
  if not record_entity:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   259
    # no valid GradingRecord key specified, log and return OK
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   260
    error_handler.logErrorAndReturnOK(
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   261
        'No valid GradingRecord key specified: %s' % record_key)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   262
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   263
  survey_group_entity = record_entity.grading_survey_group
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   264
  project_entity = record_entity.project
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   265
  student_entity = project_entity.student
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   266
  mentor_entity = project_entity.mentor
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   267
  org_entity = project_entity.scope
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   268
  site_entity = site_logic.getSingleton()
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   269
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   270
  mail_context = {
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   271
    'survey_group': survey_group_entity,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   272
    'grading_record': record_entity,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   273
    'project': project_entity,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   274
    'organization': org_entity,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   275
    'site_name': site_entity.site_name,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   276
    'to_name': student_entity.name()
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   277
  }
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   278
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   279
  # set the sender
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   280
  (sender, sender_address) = mail_dispatcher.getDefaultMailSender()
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   281
  mail_context['sender'] = sender_address
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   282
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   283
  # set the receiver and subject
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   284
  mail_context['to'] = student_entity.email
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   285
  mail_context['cc'] = [mentor_entity.email]
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   286
  mail_context['subject'] = '%s results processed for %s' %(
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   287
      survey_group_entity.name, project_entity.title)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   288
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   289
  # find all org admins for the project's organization
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   290
  fields = {'scope': org_entity,
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   291
            'status': 'active'}
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   292
  org_admin_entities = org_admin_logic.getForFields(fields)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   293
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   294
  # collect email addresses for all found org admins
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   295
  org_admin_addresses = []
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   296
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   297
  for org_admin_entity in org_admin_entities:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   298
    org_admin_addresses.append(org_admin_entity.email)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   299
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   300
  if org_admin_addresses:
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   301
    mail_context['cc'].extend(org_admin_addresses)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   302
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   303
  # send out the email using a template
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   304
  mail_template = 'soc/grading_record/mail/result.html'
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   305
  mail_dispatcher.sendMailFromTemplate(mail_template, mail_context)
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   306
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   307
  # return OK
69ac7307bb50 Added task to send out email for each processed GradingRecord.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2644
diff changeset
   308
  return http.HttpResponse()