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:
parent
135c5d2da3
commit
5a6759adae
1 changed files with 27 additions and 28 deletions
55
vendor/regidesk/regidesk/views.py
vendored
55
vendor/regidesk/regidesk/views.py
vendored
|
@ -154,8 +154,9 @@ def boarding_prepare(request):
|
||||||
"boardingpass": sample_checkin.boardingpass,
|
"boardingpass": sample_checkin.boardingpass,
|
||||||
"code": sample_checkin.code,
|
"code": sample_checkin.code,
|
||||||
"qrcode": '<img src="data:image/png;base64,' + sample_checkin.qrcode + '"/>',
|
"qrcode": '<img src="data:image/png;base64,' + sample_checkin.qrcode + '"/>',
|
||||||
"qrcode_url": request.build_absolute_uri(
|
# "qrcode_url": request.build_absolute_uri(
|
||||||
reverse("regidesk:checkin_png", args=[sample_checkin.code])),
|
# 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 = Context(sample_ctx)
|
||||||
ctx["invoices"] = invoices(ctx)
|
ctx["invoices"] = invoices(ctx)
|
||||||
|
@ -184,7 +185,7 @@ def boarding_prepare(request):
|
||||||
request.session['boarding_attendees'] = attendee_pks
|
request.session['boarding_attendees'] = attendee_pks
|
||||||
return render(request, "regidesk/boardingpass_prepare.html", ctx)
|
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:
|
if attendee:
|
||||||
user = attendee.user
|
user = attendee.user
|
||||||
|
@ -197,8 +198,9 @@ def prepare_boarding_pass(request, template, attendee=None):
|
||||||
"checkin": user.checkin,
|
"checkin": user.checkin,
|
||||||
"code": user.checkin.code,
|
"code": user.checkin.code,
|
||||||
"qrcode": user.checkin.qrcode,
|
"qrcode": user.checkin.qrcode,
|
||||||
"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])),
|
||||||
|
"qrcode_url": "https://rego.linux.conf.au/checkin/" + user.checkin.code + ".png"
|
||||||
}
|
}
|
||||||
ctx = Context(ctx)
|
ctx = Context(ctx)
|
||||||
ctx["invoices"] = invoices(ctx)
|
ctx["invoices"] = invoices(ctx)
|
||||||
|
@ -227,6 +229,23 @@ def prepare_boarding_pass(request, template, attendee=None):
|
||||||
|
|
||||||
return bpass
|
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")
|
@permission_required("regidesk.send_boarding_pass")
|
||||||
def boarding_send(request):
|
def boarding_send(request):
|
||||||
|
|
||||||
|
@ -246,37 +265,17 @@ def boarding_send(request):
|
||||||
template_pk = request.session['template']
|
template_pk = request.session['template']
|
||||||
template = BoardingPassTemplate.objects.get(pk=template_pk)
|
template = BoardingPassTemplate.objects.get(pk=template_pk)
|
||||||
|
|
||||||
i = 0
|
|
||||||
for attendee in attendees:
|
for attendee in attendees:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user = attendee.user
|
user = attendee.user
|
||||||
bpass = prepare_boarding_pass(request, template, attendee)
|
bpass = prepare_boarding_pass(user, 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")
|
|
||||||
|
|
||||||
if user in boarding_users:
|
if user in boarding_users:
|
||||||
with transaction.atomic():
|
send_boarding_pass(user, bpass)
|
||||||
msg.send()
|
messages.success(request, "Sent boarding pass to %s" % attendee)
|
||||||
bpass.sent = datetime.now()
|
|
||||||
bpass.save()
|
|
||||||
messages.success(request, "Sent boarding pass to %s" % attendee)
|
|
||||||
except:
|
except:
|
||||||
messages.warning(request, "Could not send boarding pass to %s" % attendee)
|
messages.warning(request, "Could not send boarding pass to %s" % attendee)
|
||||||
|
|
||||||
request.session['boarding_attendees'].remove(attendee.pk)
|
request.session['boarding_attendees'].remove(attendee.pk)
|
||||||
|
|
||||||
i += 1
|
|
||||||
if i == 10:
|
|
||||||
return redirect("regidesk:boarding_send")
|
|
||||||
|
|
||||||
return redirect("regidesk:boarding_overview")
|
return redirect("regidesk:boarding_overview")
|
||||||
|
|
Loading…
Reference in a new issue