From fcbacc82e68cf467b4e49d8387a69edaa554e751 Mon Sep 17 00:00:00 2001 From: James Polley Date: Tue, 26 Dec 2017 22:59:35 +1100 Subject: [PATCH] Add views to allow for PNG rendering of badge --- requirements.txt | 5 +++-- vendor/registrasion/registrasion/urls.py | 2 ++ vendor/registrasion/registrasion/views.py | 22 +++++++++++++++------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index 24ebe457..921aea38 100644 --- a/requirements.txt +++ b/requirements.txt @@ -32,12 +32,13 @@ pytz==2015.7 django-ical==1.4 -# registratoin reqs +# registrasion reqs django-nested-admin==2.2.6 - +CairoSVG==2.1.2 # Registripe django-countries>=4.3 pinax-stripe==3.2.1 requests>=2.11.1 stripe==1.38.0 + diff --git a/vendor/registrasion/registrasion/urls.py b/vendor/registrasion/registrasion/urls.py index 18f69ba5..44a7854e 100644 --- a/vendor/registrasion/registrasion/urls.py +++ b/vendor/registrasion/registrasion/urls.py @@ -27,7 +27,9 @@ from .views import ( public = [ url(r"^amend/([0-9]+)$", amend_registration, name="amend_registration"), url(r"^mybadge$", user_badge, name="user_badge"), + url(r"^mybadge/(png|svg)", user_badge, name="user_badge"), url(r"^badge/([0-9]+)$", badge, name="badge"), + url(r"^badge/([0-9]+).(png|svg)$", badge, name="badge"), url(r"^badger/([A-Za-z0-9]+)$", badger, name="badger"), url(r"^badger/", badger, name="badger"), url(r"^category/([0-9]+)$", product_category, name="product_category"), diff --git a/vendor/registrasion/registrasion/views.py b/vendor/registrasion/registrasion/views.py index 5d7b8c58..bcdfd906 100644 --- a/vendor/registrasion/registrasion/views.py +++ b/vendor/registrasion/registrasion/views.py @@ -38,6 +38,7 @@ from django.template import Context, Template, loader from lxml import etree from copy import deepcopy +from cairosvg import svg2png from registrasion.forms import BadgeForm, ticket_selection from registrasion.contrib.badger import ( @@ -1113,26 +1114,33 @@ def _get_badge_template_name(): return os.path.join(settings.PROJECT_ROOT, 'pinaxcon', 'templates', settings.BADGER_DEFAULT_SVG) @login_required -def user_badge(request): +def user_badge(request, format="png"): '''Shows the logged-in user their badge''' - return render_badge(request.user) + return render_badge(request.user, format) @user_passes_test(_staff_only) -def badge(request, user_id): +def badge(request, user_id, format="svg"): ''' Renders a single user's badge (SVG). ''' user_id = int(user_id) user = User.objects.get(pk=user_id) - return render_badge(user) + return render_badge(user, format) -def render_badge(user): +def render_badge(user, format="svg"): rendered = render_badge_svg(user) + if format == "png": + rendered = svg2png(bytestring=svg, dpi=72, scale=3) + response = HttpResponse(rendered) - response["Content-Type"] = "image/svg+xml;charset=utf-8" - response["Content-Disposition"] = 'inline; filename="badge.svg"' + if format == "svg": + response["Content-Type"] = "image/svg+xml;charset=utf-8" + response["Content-Disposition"] = 'inline; filename="badge.svg"' + elif format == "png": + response["Content-Type"] = "image/png" + response["Content-Disposition"] = 'inline; filename="badge.png"' return response def render_badge_svg(user):