You haven't pasted all the code but it sounds like you're doing something like:
ical = create_ics(thedata)
email_invite(ical, theuser)
This raises the exception you see because ics_form
is an HttpResponse
object... the attach
method is expecting instead the contents of the file to attach, as a string.
Maybe you should have the create_ics
function just return the ical content, and do the returning of an HttpResponse in a separate view function. Something like:
def email_invite(filename, ics, user_assigned):
subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
from_address = 'foo@no-reply.com'
body = "This is a invite"
recip = [user_assigned]
email = EmailMessage(subject, body, from_address, recip)
email.attach(filename, ics, 'text/calendar')
email.send()
def create_ics(data):
start1 = data['date_due']
utc = vobject.icalendar.utc
start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
start3 = data['action']
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH'
vevent = cal.add('vevent')
vevent.add('dtstart').value = start2
vevent.add('dtend').value = start2
vevent.add('dtstamp').value = datetime.datetime.now()
vevent.add('summary').value = data['action'].name
return cal.serialize()
def download_ics(request):
# do whatever you do to prepare thedata
ics = create_ics(thedata)
response = HttpResponse(ics, content_type='text/calendar')
response['Filename'] = 'filename.ics'
response['Content-Disposition'] = 'attachment; filename=filename.ics'
return response
ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)