Attach qrcode as attachment

This commit is contained in:
James Polley 2018-01-07 23:15:32 +11:00
parent 0831a4aa76
commit d30852e5f1

View file

@ -1,6 +1,7 @@
import base64 import base64
import logging import logging
from datetime import datetime from datetime import datetime
from email.mime.image import MIMEImage
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
@ -169,7 +170,7 @@ def boarding_send(request):
"user": user, "user": user,
"checkin": user.checkin, "checkin": user.checkin,
"code": user.checkin.code, "code": user.checkin.code,
"qrcode": user.checkin.qrcode, "qrcode": '<img src="cid:qrcode.png"/>',
"qrcode_url": request.build_absolute_uri( "qrcode_url": request.build_absolute_uri(
reverse("regidesk:checkin_png", args=[user.checkin.code])), reverse("regidesk:checkin_png", args=[user.checkin.code])),
} }
@ -188,29 +189,34 @@ def boarding_send(request):
html_body=html_body html_body=html_body
) )
bpass.save() bpass.save()
if user.checkin.boardingpass: if user.checkin.boardingpass:
user.checkin.boardingpass.delete() user.checkin.boardingpass.delete()
user.checkin.boardingpass = bpass user.checkin.boardingpass = bpass
user.checkin.save() user.checkin.save()
with transaction.atomic(): msg = EmailMultiAlternatives(
bpass.subject,
bpass.body,
bpass.from_address,
[bpass.to_address,],
)
msg.content_subtype="html"
msg.mixed_subtype="related"
if bpass.html_body:
msg.attach_alternative(bpass.html_body, "text/html")
msg = EmailMultiAlternatives( qrcode_image = MIMEImage(base64.b64decode(user.checkin.qrcode))
bpass.subject, qrcode_image.add_header('Content-ID', '<qrcode.png>')
bpass.body, msg.attach(qrcode_image)
bpass.from_address,
[bpass.to_address,],
)
if bpass.html_body:
msg.attach_alternative(bpass.html_body, "text/html")
if user in boarding_users: if user in boarding_users:
with transaction.atomic():
msg.send() msg.send()
bpass.sent = datetime.now() bpass.sent = datetime.now()
bpass.save() bpass.save()
messages.success(request, "Sent boarding pass to %s" % attendee) messages.success(request, "Sent boarding pass to %s" % attendee)
request.session['boarding_attendees'].remove(attendee.pk) request.session['boarding_attendees'].remove(attendee.pk)
return redirect("regidesk:boarding_overview") return redirect("regidesk:boarding_overview")