app/soc/tasks/surveys.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Sun, 27 Sep 2009 01:23:58 +0530
changeset 2978 2b3c39483f6f
parent 2880 8aa1f20aaac6
permissions -rw-r--r--
Fixes the problem of GHOP Task editing after it is created.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Tasks related to Surveys.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    25
import logging
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from google.appengine.api.labs import taskqueue
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
from django import http
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
2880
8aa1f20aaac6 Fix import sort order
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2879
diff changeset
    31
from soc.logic import system
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
    32
from soc.tasks.helper import error_handler
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
    33
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
def getDjangoURLPatterns():
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
  """Returns the URL patterns for the tasks in this module.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
  """
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
  patterns = [(r'tasks/surveys/projects/send_reminder/spawn$',
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    40
               'soc.tasks.surveys.spawnRemindersForProjectSurvey'),
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    41
              (r'tasks/surveys/projects/send_reminder/send$',
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
               'soc.tasks.surveys.sendSurveyReminderForProject')]
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
  return patterns
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
def spawnRemindersForProjectSurvey(request, *args, **kwargs):
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
  """Spawns tasks for each StudentProject in the given Program.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
  Expects the following to be present in the POST dict:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
    program_key: Specifies the program key name for which to loop over all the
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
                 StudentProjects for
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
    survey_key: specifies the key name for the ProjectSurvey to send reminders
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
                for
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
    survey_type: either project or grading depending on the type of Survey
2615
08788ae10436 Fixed errors in comments and docstring for the Surveys Task module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2614
diff changeset
    56
    project_key: optional to specify which project was the last for which a
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
                 task was spawn
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
  Args:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
    request: Django Request object
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
  """
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
  
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
  from google.appengine.ext import db
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
  from soc.logic.models.program import logic as program_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
  from soc.logic.models.student_project import logic as student_project_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
  # set default batch size
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
  batch_size = 10
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
  post_dict = request.POST
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
  # retrieve the program_key and survey_key from POST data
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
  program_key = post_dict.get('program_key')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
  survey_key = post_dict.get('survey_key')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
  survey_type = post_dict.get('survey_type')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
  if not (program_key and survey_key and survey_type):
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
    # invalid task data, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
    80
    return error_handler.logErrorAndReturnOK(
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
        'Invalid sendRemindersForProjectSurvey data: %s' % post_dict)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
  # get the program for the given keyname
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
  program_entity = program_logic.getFromKeyName(program_key)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
  if not program_entity:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
    # invalid program specified, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
    88
    return error_handler.logErrorAndReturnOK(
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
    89
        'Invalid program specified: %s' % program_key)
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
  # check and retrieve the project_key that has been done last
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
  if 'project_key' in post_dict:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
    project_start_key = post_dict['project_key']
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
  else:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
    project_start_key = None
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    96
2615
08788ae10436 Fixed errors in comments and docstring for the Surveys Task module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2614
diff changeset
    97
  # get all valid StudentProjects from starting key
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
  fields = {'program': program_entity,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
            'status': 'accepted'}
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
  if project_start_key:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
    # retrieve the last project that was done
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
    project_start = student_project_logic.getFromKeyName(project_start_key)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   104
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
    if not project_start:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
      # invalid starting project key specified, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   107
      return error_handler.logErrorAndReturnOK(
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   108
          'Invalid Student Project Key specified: %s' %(project_start_key))
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
    fields['__key__ >'] = project_start.key()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   112
  project_entities = student_project_logic.getForFields(fields,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   113
                                                        limit=batch_size)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   114
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   115
  for project_entity in project_entities:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   116
    # pass along these params as POST to the new task
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   117
    task_params = {'survey_key': survey_key,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   118
                   'survey_type': survey_type,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   119
                   'project_key': project_entity.key().id_or_name()}
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   120
    task_url = '/tasks/surveys/projects/send_reminder/send'
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   121
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   122
    new_task = taskqueue.Task(params=task_params, url=task_url)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   123
    new_task.add('mail')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   124
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   125
  if len(project_entities) == batch_size:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   126
    # spawn new task starting from the last
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   127
    new_project_start = project_entities[batch_size-1].key().id_or_name()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   128
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   129
    # pass along these params as POST to the new task
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   130
    task_params = {'program_key': program_key,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   131
                   'survey_key': survey_key,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   132
                   'survey_type': survey_type,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   133
                   'project_key': new_project_start}
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   134
    task_url = '/tasks/surveys/projects/send_reminder/spawn'
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   135
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   136
    new_task = taskqueue.Task(params=task_params, url=task_url)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   137
    new_task.add()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   138
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   139
  # return OK
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   140
  return http.HttpResponse()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   141
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   142
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   143
def sendSurveyReminderForProject(request, *args, **kwargs):
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   144
  """Sends a reminder mail for a given StudentProject and Survey.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   145
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   146
  A reminder is only send if no record is on file for the given Survey and 
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   147
  StudentProject.
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   148
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   149
  Expects the following to be present in the POST dict:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   150
    survey_key: specifies the key name for the ProjectSurvey to send reminders
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   151
                for
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   152
    survey_type: either project or grading depending on the type of Survey
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   153
    project_key: key which specifies the project to send a reminder for
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   154
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   155
  Args:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   156
    request: Django Request object
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   157
  """
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   158
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   159
  from soc.logic import mail_dispatcher
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   160
  from soc.logic.models.org_admin import logic as org_admin_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   161
  from soc.logic.models.site import logic as site_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   162
  from soc.logic.models.student_project import logic as student_project_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   163
  from soc.logic.models.survey import grading_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   164
  from soc.logic.models.survey import project_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   165
  from soc.views.helper import redirects
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   166
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   167
  post_dict = request.POST
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   168
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   169
  project_key = post_dict.get('project_key')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   170
  survey_key = post_dict.get('survey_key')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   171
  survey_type = post_dict.get('survey_type')
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   172
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   173
  if not (project_key and survey_key and survey_type):
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   174
    # invalid task data, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   175
    return error_handler.logErrorAndReturnOK(
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   176
        'Invalid sendSurveyReminderForProject data: %s' % post_dict)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   177
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   178
  # set logic depending on survey type specified in POST
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   179
  if survey_type == 'project':
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   180
    survey_logic = project_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   181
  elif survey_type == 'grading':
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   182
    survey_logic = grading_logic
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   183
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   184
  # retrieve the project and survey
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   185
  student_project = student_project_logic.getFromKeyName(project_key)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   186
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   187
  if not student_project:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   188
    # no existing project found, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   189
    return error_handler.logErrorAndReturnOK(
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   190
        'Invalid project specified %s:' % project_key)
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   191
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   192
  survey = survey_logic.getFromKeyName(survey_key)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   193
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   194
  if not survey:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   195
    # no existing survey found, log and return OK
2614
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   196
    return error_handler.logErrorAndReturnOK(
dbc8a50dbce0 Refactored logErrorAndReturnOK into separate module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2601
diff changeset
   197
        'Invalid survey specified %s:' % survey_key)
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   198
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   199
  # try to retrieve an existing record
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   200
  record_logic = survey_logic.getRecordLogic()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   201
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   202
  fields = {'project': student_project,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   203
            'survey': survey}
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   204
  record_entity = record_logic.getForFields(fields, unique=True)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   205
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   206
  if not record_entity:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   207
    # send reminder email because we found no record
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   208
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   209
    student_entity = student_project.student
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   210
    site_entity = site_logic.getSingleton()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   211
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   212
    if survey_type == 'project':
2601
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   213
      survey_redirect = redirects.getTakeSurveyRedirect(
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   214
          survey,{'url_name': 'project_survey'})
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   215
      to_role = student_entity
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   216
      mail_template = 'soc/project_survey/mail/reminder_gsoc.html'
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   217
    elif survey_type == 'grading':
2601
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   218
      survey_redirect = redirects.getTakeSurveyRedirect(
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   219
          survey,{'url_name': 'grading_project_survey'})
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   220
      to_role = student_project.mentor
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   221
      mail_template = 'soc/grading_project_survey/mail/reminder_gsoc.html'
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   222
2601
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   223
    survey_url = "http://%(host)s%(redirect)s" % {
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   224
      'redirect': survey_redirect,
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
   225
      'host': system.getHostname(),
2601
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   226
      }
1f575a1e82d5 Fixed the redirect for Survey reminder emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2595
diff changeset
   227
2595
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   228
    # set the context for the mail template
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   229
    mail_context = {
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   230
        'student_name': student_entity.name(),
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   231
        'project_title': student_project.title,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   232
        'survey_url': survey_url,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   233
        'survey_end': survey.survey_end,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   234
        'to_name': to_role.name(),
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   235
        'site_name': site_entity.site_name,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   236
    }
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   237
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   238
    # set the sender
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   239
    (sender, sender_address) = mail_dispatcher.getDefaultMailSender()
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   240
    mail_context['sender'] = sender_address
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   241
    # set the receiver and subject
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   242
    mail_context['to'] = to_role.email
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   243
    mail_context['subject'] = 'Evaluation Survey "%s" Reminder' %(survey.title)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   244
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   245
    # find all org admins for the project's organization
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   246
    org_entity = student_project.scope
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   247
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   248
    fields = {'scope': org_entity,
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   249
              'status': 'active'}
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   250
    org_admin_entities = org_admin_logic.getForFields(fields)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   251
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   252
    # collect email addresses for all found org admins
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   253
    org_admin_addresses = []
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   254
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   255
    for org_admin_entity in org_admin_entities:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   256
      org_admin_addresses.append(org_admin_entity.email)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   257
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   258
    if org_admin_addresses:
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   259
      mail_context['cc'] = org_admin_addresses
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   260
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   261
    # send out the email
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   262
    mail_dispatcher.sendMailFromTemplate(mail_template, mail_context)
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   263
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   264
  # return OK
4c00831ef55c Added Surveys Tasks module to send reminders.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   265
  return http.HttpResponse()