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,61 +58,62 @@ class Command(BaseCommand): ...@@ -53,61 +58,62 @@ 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
""" """
tracking_code = '-'.join([ tracking_code = '-'.join([
'eml', 'eml',
'prof', # the 'product'--no idea what that's supposed to mean 'prof', # the 'product'--no idea what that's supposed to mean
course.org, # Partner's name course.org, # Partner's name
course.number, # Certificate's name course.number, # Certificate's name
'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'),
'pfCertFuture': certificate.created_date.strftime('%Y%m'), 'pfCertFuture': certificate.created_date.strftime('%Y%m'),
'_mSplash': '1', '_mSplash': '1',
'trk': tracking_code, 'trk': tracking_code,
'startTask': 'CERTIFICATION_name', 'startTask': 'CERTIFICATION_name',
'force': 'true', 'force': 'true',
} }
return 'http://www.linkedin.com/profile/guided?' + urllib.urlencode(query) return 'http://www.linkedin.com/profile/guided?' + urllib.urlencode(query)
def send_grandfather_email(user, certificates): def send_grandfather_email(self, 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):
"""
Email a user that recently earned a certificate, inviting them to post
their certificate on their LinkedIn profile.
"""
template = get_template("linkedin_email.html")
course = get_course_by_id(certificate.course_id)
url = self.certificate_url(course, certificate)
context = Context({
'student_name': user.profile.name,
'course_name': certificate.name,
'url': url})
def send_email(user, certificate): subject = 'Congratulations! Put your certificate on LinkedIn'
""" body = template.render(context)
Email a user that recently earned a certificate, inviting them to post their fromaddr = self.api.config['EMAIL_FROM']
certificate on their LinkedIn profile. toaddr = '%s <%s>' % (user.profile.name, user.email)
""" send_mail(subject, body, fromaddr, (toaddr,))
api = LinkedinAPI()
template = get_template("linkedin_email.html")
course = get_course_by_id(certificate.course_id)
url = certificate_url(api, course, certificate)
context = Context({
'student_name': user.profile.name,
'course_name': certificate.name,
'url': url})
print template.render(context)
print url
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