Commit 35fdbf3a by Chris Rossi Committed by Diana Huang

Send mail.

parent 5d7befde
...@@ -6,15 +6,16 @@ LinkedIn profiles. ...@@ -6,15 +6,16 @@ LinkedIn profiles.
import json import json
import urllib import urllib
from courseware.courses import get_course_by_id from django.core.mail import send_mail
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.template import Context from django.template import Context
from django.template.loader import get_template from django.template.loader import get_template
from optparse import make_option from optparse import make_option
from certificates.models import GeneratedCertificate from certificates.models import GeneratedCertificate
from ...models import LinkedIn from courseware.courses import get_course_by_id
from ...models import LinkedIn
from . import LinkedinAPI from . import LinkedinAPI
...@@ -40,6 +41,10 @@ class Command(BaseCommand): ...@@ -40,6 +41,10 @@ class Command(BaseCommand):
"certificates. Afterwards the default, one email per " "certificates. Afterwards the default, one email per "
"certificate mail form will be used."),) "certificate mail form will be used."),)
def __init__(self):
super(BaseCommand, self).__init__()
self.api = LinkedinAPI()
def handle(self, *args, **options): def handle(self, *args, **options):
grandfather = options.get('grandfather', False) grandfather = options.get('grandfather', False)
accounts = LinkedIn.objects.filter(has_linkedin_account=True) accounts = LinkedIn.objects.filter(has_linkedin_account=True)
...@@ -53,15 +58,15 @@ class Command(BaseCommand): ...@@ -53,15 +58,15 @@ class Command(BaseCommand):
if not certificates: if not certificates:
continue continue
if grandfather: if grandfather:
send_grandfather_email(user, certificates) self.send_grandfather_email(user, certificates)
emailed.extend([cert.course_id for cert in certificates]) emailed.extend([cert.course_id for cert in certificates])
else: else:
for certificate in certificates: for certificate in certificates:
send_email(user, certificate) self.send_email(user, certificate)
emailed.append(certificate.course_id) emailed.append(certificate.course_id)
account.emailed_courses = json.dumps(emailed) account.emailed_courses = json.dumps(emailed)
def certificate_url(api, course, certificate, grandfather=False): def certificate_url(self, course, certificate, grandfather=False):
""" """
Generates a certificate URL based on LinkedIn's documentation. The Generates a certificate URL based on LinkedIn's documentation. The
documentation is from a Word document: DAT_DOCUMENTATION_v3.12.docx documentation is from a Word document: DAT_DOCUMENTATION_v3.12.docx
...@@ -74,8 +79,8 @@ def certificate_url(api, course, certificate, grandfather=False): ...@@ -74,8 +79,8 @@ def certificate_url(api, course, certificate, grandfather=False):
'gf' if grandfather else 'T']) 'gf' if grandfather else 'T'])
query = { query = {
'pfCertificationName': certificate.name, 'pfCertificationName': certificate.name,
'pfAuthorityName': api.config['COMPANY_NAME'], 'pfAuthorityName': self.api.config['COMPANY_NAME'],
'pfAuthorityId': api.config['COMPANY_ID'], 'pfAuthorityId': self.api.config['COMPANY_ID'],
'pfCertificationUrl': certificate.download_url, 'pfCertificationUrl': certificate.download_url,
'pfLicenseNo': certificate.course_id, 'pfLicenseNo': certificate.course_id,
'pfCertStartDate': course.start.strftime('%Y%mI'), 'pfCertStartDate': course.start.strftime('%Y%mI'),
...@@ -87,27 +92,28 @@ def certificate_url(api, course, certificate, grandfather=False): ...@@ -87,27 +92,28 @@ def certificate_url(api, course, certificate, grandfather=False):
} }
return 'http://www.linkedin.com/profile/guided?' + urllib.urlencode(query) return 'http://www.linkedin.com/profile/guided?' + urllib.urlencode(query)
def send_grandfather_email(self, user, certificates):
def send_grandfather_email(user, certificates):
""" """
Send the 'grandfathered' email informing historical students that they may Send the 'grandfathered' email informing historical students that they
now post their certificates on their LinkedIn profiles. may now post their certificates on their LinkedIn profiles.
""" """
print "GRANDFATHER: ", user, certificates print "GRANDFATHER: ", user, certificates
def send_email(self, user, certificate):
def send_email(user, certificate):
""" """
Email a user that recently earned a certificate, inviting them to post their Email a user that recently earned a certificate, inviting them to post
certificate on their LinkedIn profile. their certificate on their LinkedIn profile.
""" """
api = LinkedinAPI()
template = get_template("linkedin_email.html") template = get_template("linkedin_email.html")
course = get_course_by_id(certificate.course_id) course = get_course_by_id(certificate.course_id)
url = certificate_url(api, course, certificate) url = self.certificate_url(course, certificate)
context = Context({ context = Context({
'student_name': user.profile.name, 'student_name': user.profile.name,
'course_name': certificate.name, 'course_name': certificate.name,
'url': url}) 'url': url})
print template.render(context)
print url subject = 'Congratulations! Put your certificate on LinkedIn'
body = template.render(context)
fromaddr = self.api.config['EMAIL_FROM']
toaddr = '%s <%s>' % (user.profile.name, user.email)
send_mail(subject, body, fromaddr, (toaddr,))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment