def get_emergency_codes_image(request, **kwargs): r_k = "emergency-nonce-for-%s" % request.browser.bid_public if dcache.get(r_k) != kwargs["single_use_code"]: custom_log(request, "emergency codes: invalid one-time code for output. Referrer: %s" % request.META.get("HTTP_REFERRER"), level="warn") response = render_to_response("login_frontend/download_invalid_nonce.html", {}, context_instance=RequestContext(request)) response.status_code = "403" response.reason_phrase = "Forbidden" return response dcache.delete(r_k) codes = request.browser.user.get_emergency_codes() if not codes or codes.codes_left() == 0: custom_log(request, "Tried to download emergency codes; none available.", level="error") raise Http404 font = PIL.ImageFont.truetype(settings.EMERGENCY_FONT, 17) img = PIL.Image.new("RGB", (350, 35 + 10 + 35 * codes.codes_left()), (255,255,255)) draw = PIL.ImageDraw.Draw(img) draw.text((10, 20), str(codes.generated_at), "black", font=font) i = 1 for code in EmergencyCode.objects.all().filter(codegroup=codes).order_by("code_id"): draw.text((10, 20 + i * 35), "#%s: %s" % (code.code_id, code.code_val), "black", font=font) i += 1 stringio = StringIO() img.save(stringio, "png") stringio.seek(0) codes.downloaded_at = timezone.now() codes.downloaded_with = request.browser codes.save() response = HttpResponse(stringio.read(), content_type="image/png") response['Content-Disposition'] = 'attachment; filename=emergency_codes.png' add_user_log(request, "Downloaded emergency codes", "info") custom_log(request, "Downloaded emergency codes", level="info") new_emergency_generated_notify(request, codes) return response
def get_emergency_codes_pdf(request, **kwargs): r_k = "emergency-nonce-for-%s" % request.browser.bid_public if dcache.get(r_k) != kwargs["single_use_code"]: custom_log(request, "emergency codes: invalid one-time code for output. Referrer: %s" % request.META.get("HTTP_REFERRER"), level="warn") response = render_to_response("login_frontend/download_invalid_nonce.html", {}, context_instance=RequestContext(request)) response.status_code = "403" response.reason_phrase = "Forbidden" return response dcache.delete(r_k) codes = request.browser.user.get_emergency_codes() if not codes or codes.codes_left() == 0: custom_log(request, "Tried to download emergency codes; none available.", level="error") raise Http404 buffer = BytesIO() p = canvas.Canvas(buffer, pagesize=A4, bottomup=0) pdfmetrics.registerFont(TTFont('defaultfont', settings.EMERGENCY_FONT)) p.setFont('defaultfont', 20) # Draw sheet timestamp (UTC) on top. p.drawString(100,100, str(codes.generated_at)) i = 0 for code in EmergencyCode.objects.all().filter(codegroup=codes).order_by("code_id"): # Add space every 5 characters to improve readability. formatted_code = " ".join(textwrap.wrap(code.code_val, 5)) p.drawString(100, 150 + i * 35, "#%s: %s" % (code.code_id, formatted_code)) i += 1 codes.downloaded_at = timezone.now() codes.downloaded_with = request.browser codes.save() p.showPage() p.save() pdf = buffer.getvalue() buffer.close() response = HttpResponse(content_type="application/pdf") response['Content-Disposition'] = 'attachment; filename=emergency_codes.pdf' response.write(pdf) add_user_log(request, "Downloaded emergency codes", "info") custom_log(request, "Downloaded emergency codes", level="info") new_emergency_generated_notify(request, codes) return response