Skip to content

Commit

Permalink
Attach calendar invite to cc add email
Browse files Browse the repository at this point in the history
  • Loading branch information
tnurse18 committed Jan 17, 2021
1 parent 74056d7 commit 4f983c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
5 changes: 4 additions & 1 deletion emails/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ def __init__(self,
self.email = EmailMultiAlternatives(subject, content_txt, from_email, to_emails, bcc=bcc, cc=cc,
reply_to=reply_to)
for a in attachments:
self.email.attach(a['name'], a['file_handle'], "application/pdf")
if a['file_handle']:
self.email.attach(a['name'], a['file_handle'], "application/pdf")
else:
self.email.attach(a)

if build_html:
template_html = "%s.html" % template_basename
Expand Down
6 changes: 3 additions & 3 deletions emails/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_poke_cc_message_generator(self):

# Test with 1 service passed in
expected = "Some example message<hr><strong>CC's needed:</strong> Lighting\n<strong>Services:</strong> " \
"Basic Lighting\n<strong>What:</strong> <a href='https://lnl.wpi.edu/db/events/view/1/'>" \
"Basic Lighting\n<strong>What:</strong> <a href='http://lnl.wpi.edu/db/events/view/1/'>" \
"All set</a>\n<strong>When:</strong> %s\n<strong>Setup by:</strong> %s\n<strong>Where:</strong> " \
"Test Location\n<strong>Description:</strong> An event that is pretty much all set\n\n" % \
(event1_start + event1_end, event1_setup)
Expand All @@ -118,11 +118,11 @@ def test_poke_cc_message_generator(self):

# Test with 2 services passed in
expected = "Some example message<hr><strong>CC's needed:</strong> Lighting\n<strong>Services:</strong> " \
"Basic Lighting\n<strong>What:</strong> <a href='https://lnl.wpi.edu/db/events/view/1/'>" \
"Basic Lighting\n<strong>What:</strong> <a href='http://lnl.wpi.edu/db/events/view/1/'>" \
"All set</a>\n<strong>When:</strong> %s\n<strong>Setup by:</strong> %s\n<strong>Where:</strong> " \
"Test Location\n<strong>Description:</strong> An event that is pretty much all set\n\n" \
"<strong>CC's needed:</strong> Sound\n<strong>Services:</strong> Basic Sound\n" \
"<strong>What:</strong> <a href='https://lnl.wpi.edu/db/events/view/2/'>Need Chiefs</a>\n" \
"<strong>What:</strong> <a href='http://lnl.wpi.edu/db/events/view/2/'>Need Chiefs</a>\n" \
"<strong>When:</strong> %s\n<strong>Setup by:</strong> %s\n<strong>Where:</strong> Test Location\n" \
"<strong>Description:</strong> A test event that we need CCs for\n\n" % \
(event1_start + event1_end, event1_setup, event2_start + event2_end, event2_setup)
Expand Down
31 changes: 31 additions & 0 deletions events/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import datetime
import icalendar

from django.conf import settings
from django.db.models.signals import post_save, pre_delete, pre_save
from email.mime.base import MIMEBase
from email.encoders import encode_base64
from django.dispatch import receiver
from django.utils import timezone
from django.utils.text import slugify
Expand All @@ -27,6 +30,34 @@ def email_cc_notification(sender, instance, created, raw=False, **kwargs):
filename = "%s.workorder.pdf" % slugify(event.event_name)
attachments = [{"file_handle": pdf_handle, "name": filename}]

# generate an Outlook invite
chief = i.crew_chief
cal = icalendar.Calendar()
cal['method'] = 'PUBLISH'
cal['prodid'] = '-//WPI Lens and Lights//LNLDB//EN'
cal['version'] = '2.0'
vevent = icalendar.Event()
vevent['summary'] = event.event_name
vevent['description'] = event.cal_desc()
vevent['uid'] = event.cal_guid()
vevent['dtstart'] = event.datetime_start.strftime('%Y%m%dT%H%M%SZ')
vevent['dtend'] = event.datetime_end.strftime('%Y%m%dT%H%M%SZ')
vevent['dtstamp'] = timezone.now().strftime('%Y%m%dT%H%M%SZ')
vevent['location'] = event.location.name
vevent['url'] = event.cal_link()
vevent.add('attendee', 'MAILTO:%s' % chief.email, parameters={'RSVP': 'FALSE', 'CN': chief.name})
cal.add_component(vevent)

invite_filename = '%s.invite.ics' % slugify(event.event_name)
invite = MIMEBase('text', "calendar", method="PUBLISH", name=invite_filename)
invite.set_payload(cal.to_ical())
encode_base64(invite)
invite.add_header('Content-Description', invite_filename)
invite.add_header('Content-class', "urn:content-classes:calendarmessage")
invite.add_header('Filename', invite_filename)
invite.add_header('Path', invite_filename)
attachments.append(invite)

if i.setup_start:
local = timezone.localtime(i.setup_start)
local_formatted = local.strftime("%A %B %d at %I:%M %p")
Expand Down

0 comments on commit 4f983c4

Please sign in to comment.