app/soc/cron/student_proposal_mailer.py
changeset 2238 71f4d7642afb
parent 2226 6159450c4767
child 2240 df37a3ff8ed5
equal deleted inserted replaced
2237:3cfaa7dfd9e0 2238:71f4d7642afb
    20 __authors__ = [
    20 __authors__ = [
    21     '"Lennard de Rijk" <ljvderijk@gmail.com>',
    21     '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22   ]
    22   ]
    23 
    23 
    24 
    24 
    25 import logging
    25 from soc.logic import mail_dispatcher
    26 
       
    27 from soc.logic.models.job import logic as job_logic
    26 from soc.logic.models.job import logic as job_logic
    28 from soc.logic.models.priority_group import logic as priority_logic
    27 from soc.logic.models.priority_group import logic as priority_logic
    29 from soc.logic.models.program import logic as program_logic
    28 from soc.logic.models.program import logic as program_logic
    30 from soc.logic.models.student import logic as student_logic
    29 from soc.logic.models.student import logic as student_logic
    31 from soc.logic.models.student_proposal import logic as proposal_logic
    30 from soc.logic.models.student_proposal import logic as proposal_logic
    32 
    31 
    33 
    32 
    34 # amount of students to create jobs for before updating
    33 # amount of students to create jobs for before updating
    35 DEF_STUDENT_STEP_SIZE = 10
    34 DEF_STUDENT_STEP_SIZE = 10
       
    35 
       
    36 # template for the accepted proposal mail
       
    37 DEF_ACCEPTED_MAIL_TEMPLATE = \
       
    38     'gsoc/student_proposal/mail/accepted_gsoc2009.html'
       
    39 
       
    40 # template for the rejected proposal mail
       
    41 DEF_REJECTED_MAIL_TEMPLATE = \
       
    42     'gsoc/student_proposal/mail/rejected_gsoc2009.html'
    36 
    43 
    37 
    44 
    38 def setupStudentProposalMailing(job_entity):
    45 def setupStudentProposalMailing(job_entity):
    39   """Job that setup jobs that will mail students if they have been accepted in
    46   """Job that setup jobs that will mail students if they have been accepted in
    40   a program with a GSoC-like workflow.
    47   a program with a GSoC-like workflow.
   112 
   119 
   113   Args:
   120   Args:
   114     job_entity: a Job entity with key_data set to [student_key]
   121     job_entity: a Job entity with key_data set to [student_key]
   115   """
   122   """
   116 
   123 
       
   124   from soc.cron.job import Error
   117   from soc.cron.job import FatalJobError
   125   from soc.cron.job import FatalJobError
   118 
   126 
   119 
   127 
   120   student_keyname = job_entity.key_data[0].name()
   128   student_keyname = job_entity.key_data[0].name()
   121   student_entity = student_logic.getFromKeyName(student_keyname)
   129   student_entity = student_logic.getFromKeyName(student_keyname)
   127   # only students who have sent in a proposal will be mailed
   135   # only students who have sent in a proposal will be mailed
   128   fields = {'scope': student_entity}
   136   fields = {'scope': student_entity}
   129   proposal = proposal_logic.getForFields(fields, unique=True)
   137   proposal = proposal_logic.getForFields(fields, unique=True)
   130 
   138 
   131   if proposal:
   139   if proposal:
       
   140     # a proposal has been found we must sent out an email
       
   141     default_sender = mail_dispatcher.getDefaultMailSender()
       
   142 
       
   143     if not default_sender:
       
   144       # no default sender abort
       
   145       raise Error('No valid sender address could be found, try setting '
       
   146                   'a no-reply address on the site settings page')
       
   147     else:
       
   148       (sender_name, sender) = default_sender
       
   149 
       
   150     # construct the contents of the email
       
   151     student_entity = proposal.scope
       
   152     program_entity = proposal.program
       
   153 
       
   154     context = {
       
   155         'to': student_entity.email,
       
   156         'to_name': student_entity.given_name,
       
   157         'sender': sender,
       
   158         'sender_name': sender_name,
       
   159         'program_name': program_entity.name,
       
   160     }
       
   161 
   132     # check if the student has an accepted proposal
   162     # check if the student has an accepted proposal
   133     fields['status'] = 'accepted'
   163     fields['status'] = 'accepted'
   134     accepted_proposal = proposal_logic.getForFields(fields, unique=True)
   164     accepted_proposal = proposal_logic.getForFields(fields, unique=True)
   135 
   165 
   136     # TODO(ljvderijk) replace with real mail sending
       
   137     if accepted_proposal:
   166     if accepted_proposal:
   138       logging.info('Sending acceptance mail to %s' % (student_entity.name()))
   167       org_entity = accepted_proposal.org
       
   168       # use the accepted template and subject
       
   169       template = DEF_ACCEPTED_MAIL_TEMPLATE
       
   170       context['subject'] = 'Congratulations!'
       
   171       context['proposal_title'] = accepted_proposal.title
       
   172       context['org_name'] = accepted_proposal.org.name
   139     else:
   173     else:
   140       logging.info('Sending rejectance mail to %s' % (student_entity.name()))
   174       # use the rejected template and subject
       
   175       template = DEF_REJECTED_MAIL_TEMPLATE
       
   176       context['subject'] = 'Thank you for applying to %s' % (
       
   177           program_entity.name)
       
   178 
       
   179     # send out the constructed email
       
   180     mail_dispatcher.sendMailFromTemplate(template, context)
   141 
   181 
   142   # we are done here
   182   # we are done here
   143   return
   183   return
   144 
   184