Add views to allow for PNG rendering of badge

This commit is contained in:
James Polley 2017-12-26 22:59:35 +11:00
parent 469e6f1ad9
commit fcbacc82e6
3 changed files with 20 additions and 9 deletions

View file

@ -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

View file

@ -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"),

View file

@ -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):