app/soc/cron/student_proposal_mailer.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 19 Apr 2009 17:42:44 +0000
changeset 2246 c29272f640b0
parent 2240 df37a3ff8ed5
child 2255 783e69b54d47
permissions -rw-r--r--
Tweak the 'load balancing' algorithm In order to reduce contention we randomly skipped jobs, but this caused many jobs to end up stopping early. Now instead we keep on going until we time out (also increased the chance of doing work). Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2009 the Melange authors.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    17
"""Cron job handler for Student Proposal mailing.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    24
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    25
from soc.logic import mail_dispatcher
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    26
from soc.logic.models.job import logic as job_logic
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from soc.logic.models.priority_group import logic as priority_logic
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
from soc.logic.models.program import logic as program_logic
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
from soc.logic.models.student import logic as student_logic
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
from soc.logic.models.student_proposal import logic as proposal_logic
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    31
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    32
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
# amount of students to create jobs for before updating
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
DEF_STUDENT_STEP_SIZE = 10
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    36
# template for the accepted proposal mail
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    37
DEF_ACCEPTED_MAIL_TEMPLATE = \
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    38
    'gsoc/student_proposal/mail/accepted_gsoc2009.html'
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    39
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    40
# template for the rejected proposal mail
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    41
DEF_REJECTED_MAIL_TEMPLATE = \
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    42
    'gsoc/student_proposal/mail/rejected_gsoc2009.html'
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
    43
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
def setupStudentProposalMailing(job_entity):
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
  """Job that setup jobs that will mail students if they have been accepted in
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
  a program with a GSoC-like workflow.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    48
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    49
  Args:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    50
    job_entity: a Job entity with key_data set to 
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
                [program, last_completed_student]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
  """
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
  from soc.cron.job import FatalJobError
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
  # retrieve the data we need to continue our work
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
  key_data = job_entity.key_data
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
  program_key = key_data[0]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
  program_keyname = program_key.name()
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
  program_entity = program_logic.getFromKeyName(program_keyname)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
  if not program_entity:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
    raise FatalJobError('The program with key %s could not be found' % (
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
        program_keyname))
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
  student_fields = {'scope': program_entity}
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
  if len(key_data) >= 2:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
    # start where we left off
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
    student_fields['__key__ >'] = key_data[1]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
  students = student_logic.getForFields(student_fields,
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
                                        limit=DEF_STUDENT_STEP_SIZE)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
  # set the default fields for the jobs we are going to create
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
  priority_group = priority_logic.getGroup(priority_logic.EMAIL)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
  job_fields = {
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
      'priority_group': priority_group,
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
      'task_name': 'sendStudentProposalMail'}
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
  job_query_fields = job_fields.copy()
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    85
  while students:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    86
    # for each student create a mailing job
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    87
    for student in students:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    88
2226
6159450c4767 Instead of text_data use key_data property to check for uniqueness.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2223
diff changeset
    89
      job_query_fields['key_data'] = student.key()
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    90
      mail_job = job_logic.getForFields(job_query_fields, unique=True)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    91
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    92
      if not mail_job:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    93
        # this student did not receive mail yet
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    94
        job_fields['key_data'] = [student.key()]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    95
        job_logic.updateOrCreateFromFields(job_fields)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    96
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    97
    # update our own job
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    98
    last_student_key = students[-1].key()
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    99
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   100
    if len(key_data) >= 2:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   101
      key_data[1] = last_student_key
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   102
    else:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   103
      key_data.append(last_student_key)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   104
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   105
    updated_job_fields = {'key_data': key_data}
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   106
    job_logic.updateEntityProperties(job_entity, updated_job_fields)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   107
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   108
    # rinse and repeat
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   109
    student_fields['__key__ >'] = last_student_key
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   110
    students = student_logic.getForFields(student_fields,
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   111
                                          limit=DEF_STUDENT_STEP_SIZE)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   112
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   113
  # we are finished
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   114
  return
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   115
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   116
def sendStudentProposalMail(job_entity):
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   117
  """Job that will send out an email to a student that sent in a proposal
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   118
  that either got accepted or rejected.
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   119
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   120
  Args:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   121
    job_entity: a Job entity with key_data set to [student_key]
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   122
  """
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   123
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   124
  from soc.cron.job import FatalJobError
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   125
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   126
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   127
  student_keyname = job_entity.key_data[0].name()
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   128
  student_entity = student_logic.getFromKeyName(student_keyname)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   129
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   130
  if not student_entity:
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   131
    raise FatalJobError('The student with keyname %s does not exist!' % (
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   132
        student_keyname))
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   133
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   134
  # only students who have sent in a proposal will be mailed
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   135
  fields = {'scope': student_entity}
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   136
  proposal = proposal_logic.getForFields(fields, unique=True)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   137
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   138
  if proposal:
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   139
    # a proposal has been found we must sent out an email
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   140
    default_sender = mail_dispatcher.getDefaultMailSender()
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   141
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   142
    if not default_sender:
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   143
      # no default sender abort
2240
df37a3ff8ed5 Changed the type of Error occurring when no valid sender could be found to a FatalJobError in student_proposal_mailer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2238
diff changeset
   144
      raise FatalJobError('No valid sender address could be found, try '
df37a3ff8ed5 Changed the type of Error occurring when no valid sender could be found to a FatalJobError in student_proposal_mailer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2238
diff changeset
   145
                          'setting a no-reply address on the site settings '
df37a3ff8ed5 Changed the type of Error occurring when no valid sender could be found to a FatalJobError in student_proposal_mailer.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2238
diff changeset
   146
                          'page')
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   147
    else:
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   148
      (sender_name, sender) = default_sender
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   149
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   150
    # construct the contents of the email
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   151
    student_entity = proposal.scope
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   152
    program_entity = proposal.program
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   153
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   154
    context = {
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   155
        'to': student_entity.email,
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   156
        'to_name': student_entity.given_name,
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   157
        'sender': sender,
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   158
        'sender_name': sender_name,
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   159
        'program_name': program_entity.name,
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   160
    }
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   161
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   162
    # check if the student has an accepted proposal
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   163
    fields['status'] = 'accepted'
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   164
    accepted_proposal = proposal_logic.getForFields(fields, unique=True)
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   165
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   166
    if accepted_proposal:
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   167
      org_entity = accepted_proposal.org
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   168
      # use the accepted template and subject
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   169
      template = DEF_ACCEPTED_MAIL_TEMPLATE
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   170
      context['subject'] = 'Congratulations!'
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   171
      context['proposal_title'] = accepted_proposal.title
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   172
      context['org_name'] = accepted_proposal.org.name
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   173
    else:
2238
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   174
      # use the rejected template and subject
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   175
      template = DEF_REJECTED_MAIL_TEMPLATE
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   176
      context['subject'] = 'Thank you for applying to %s' % (
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   177
          program_entity.name)
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   178
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   179
    # send out the constructed email
71f4d7642afb Student Proposal Mailer is now sending out real emails.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2226
diff changeset
   180
    mail_dispatcher.sendMailFromTemplate(template, context)
2223
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   181
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   182
  # we are done here
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   183
  return
7b8b812aa146 Added Student Proposal Mailer module for mailing students who are accepted/rejected into the program.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
   184