Refactor bpass build/send code

* Pull out the bits that prepare and build the bpass
* Make them not require a request object
* so that we can now call this code from another context
This commit is contained in:
James Polley 2018-01-19 07:01:41 +11:00
parent 135c5d2da3
commit 5a6759adae

View file

@ -154,8 +154,9 @@ def boarding_prepare(request):
"boardingpass": sample_checkin.boardingpass,
"code": sample_checkin.code,
"qrcode": '<img src="data:image/png;base64,' + sample_checkin.qrcode + '"/>',
"qrcode_url": request.build_absolute_uri(
reverse("regidesk:checkin_png", args=[sample_checkin.code])),
# "qrcode_url": request.build_absolute_uri(
# reverse("regidesk:checkin_png", args=[sample_checkin.code])),
"qrcode_url": "https://rego.linux.conf.au/checkin/" + sample_checkin.code + ".png"
}
ctx = Context(sample_ctx)
ctx["invoices"] = invoices(ctx)
@ -184,7 +185,7 @@ def boarding_prepare(request):
request.session['boarding_attendees'] = attendee_pks
return render(request, "regidesk/boardingpass_prepare.html", ctx)
def prepare_boarding_pass(request, template, attendee=None):
def prepare_boarding_pass(user, template, attendee=None):
if attendee:
user = attendee.user
@ -197,8 +198,9 @@ def prepare_boarding_pass(request, template, attendee=None):
"checkin": user.checkin,
"code": user.checkin.code,
"qrcode": user.checkin.qrcode,
"qrcode_url": request.build_absolute_uri(
reverse("regidesk:checkin_png", args=[user.checkin.code])),
# "qrcode_url": request.build_absolute_uri(
# reverse("regidesk:checkin_png", args=[user.checkin.code])),
"qrcode_url": "https://rego.linux.conf.au/checkin/" + user.checkin.code + ".png"
}
ctx = Context(ctx)
ctx["invoices"] = invoices(ctx)
@ -227,6 +229,23 @@ def prepare_boarding_pass(request, template, attendee=None):
return bpass
def send_boarding_pass(bpass, user):
msg = EmailMultiAlternatives(
bpass.subject,
bpass.body,
bpass.from_address,
[bpass.to_address,],
)
msg.content_subtype="plain"
msg.mixed_subtype="related"
if bpass.html_body:
msg.attach_alternative(bpass.html_body, "text/html")
msg.attach(filename="qrcode.png", content=user.checkin.qrcode, mimetype="image/png")
msg.send()
bpass.sent = datetime.now()
bpass.save()
@permission_required("regidesk.send_boarding_pass")
def boarding_send(request):
@ -246,37 +265,17 @@ def boarding_send(request):
template_pk = request.session['template']
template = BoardingPassTemplate.objects.get(pk=template_pk)
i = 0
for attendee in attendees:
try:
user = attendee.user
bpass = prepare_boarding_pass(request, template, attendee)
msg = EmailMultiAlternatives(
bpass.subject,
bpass.body,
bpass.from_address,
[bpass.to_address,],
)
msg.content_subtype="plain"
msg.mixed_subtype="related"
if bpass.html_body:
msg.attach_alternative(bpass.html_body, "text/html")
msg.attach(filename="qrcode.png", content=user.checkin.qrcode, mimetype="image/png")
bpass = prepare_boarding_pass(user, template, attendee)
if user in boarding_users:
with transaction.atomic():
msg.send()
bpass.sent = datetime.now()
bpass.save()
messages.success(request, "Sent boarding pass to %s" % attendee)
send_boarding_pass(user, bpass)
messages.success(request, "Sent boarding pass to %s" % attendee)
except:
messages.warning(request, "Could not send boarding pass to %s" % attendee)
request.session['boarding_attendees'].remove(attendee.pk)
i += 1
if i == 10:
return redirect("regidesk:boarding_send")
return redirect("regidesk:boarding_overview")