Student Proposal Mailer is now sending out real emails.
authorLennard de Rijk <ljvderijk@gmail.com>
Sun, 19 Apr 2009 10:29:57 +0000
changeset 2238 71f4d7642afb
parent 2237 3cfaa7dfd9e0
child 2239 9a47a64dcc08
Student Proposal Mailer is now sending out real emails. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
app/soc/cron/student_proposal_mailer.py
--- a/app/soc/cron/student_proposal_mailer.py	Sun Apr 19 10:28:35 2009 +0000
+++ b/app/soc/cron/student_proposal_mailer.py	Sun Apr 19 10:29:57 2009 +0000
@@ -22,8 +22,7 @@
   ]
 
 
-import logging
-
+from soc.logic import mail_dispatcher
 from soc.logic.models.job import logic as job_logic
 from soc.logic.models.priority_group import logic as priority_logic
 from soc.logic.models.program import logic as program_logic
@@ -34,6 +33,14 @@
 # amount of students to create jobs for before updating
 DEF_STUDENT_STEP_SIZE = 10
 
+# template for the accepted proposal mail
+DEF_ACCEPTED_MAIL_TEMPLATE = \
+    'gsoc/student_proposal/mail/accepted_gsoc2009.html'
+
+# template for the rejected proposal mail
+DEF_REJECTED_MAIL_TEMPLATE = \
+    'gsoc/student_proposal/mail/rejected_gsoc2009.html'
+
 
 def setupStudentProposalMailing(job_entity):
   """Job that setup jobs that will mail students if they have been accepted in
@@ -114,6 +121,7 @@
     job_entity: a Job entity with key_data set to [student_key]
   """
 
+  from soc.cron.job import Error
   from soc.cron.job import FatalJobError
 
 
@@ -129,15 +137,47 @@
   proposal = proposal_logic.getForFields(fields, unique=True)
 
   if proposal:
+    # a proposal has been found we must sent out an email
+    default_sender = mail_dispatcher.getDefaultMailSender()
+
+    if not default_sender:
+      # no default sender abort
+      raise Error('No valid sender address could be found, try setting '
+                  'a no-reply address on the site settings page')
+    else:
+      (sender_name, sender) = default_sender
+
+    # construct the contents of the email
+    student_entity = proposal.scope
+    program_entity = proposal.program
+
+    context = {
+        'to': student_entity.email,
+        'to_name': student_entity.given_name,
+        'sender': sender,
+        'sender_name': sender_name,
+        'program_name': program_entity.name,
+    }
+
     # check if the student has an accepted proposal
     fields['status'] = 'accepted'
     accepted_proposal = proposal_logic.getForFields(fields, unique=True)
 
-    # TODO(ljvderijk) replace with real mail sending
     if accepted_proposal:
-      logging.info('Sending acceptance mail to %s' % (student_entity.name()))
+      org_entity = accepted_proposal.org
+      # use the accepted template and subject
+      template = DEF_ACCEPTED_MAIL_TEMPLATE
+      context['subject'] = 'Congratulations!'
+      context['proposal_title'] = accepted_proposal.title
+      context['org_name'] = accepted_proposal.org.name
     else:
-      logging.info('Sending rejectance mail to %s' % (student_entity.name()))
+      # use the rejected template and subject
+      template = DEF_REJECTED_MAIL_TEMPLATE
+      context['subject'] = 'Thank you for applying to %s' % (
+          program_entity.name)
+
+    # send out the constructed email
+    mail_dispatcher.sendMailFromTemplate(template, context)
 
   # we are done here
   return