From 21cebaa31dd111afb2274e8464d49263c500bd46 Mon Sep 17 00:00:00 2001 From: Julian Rother <julianr@fsmpi.rwth-aachen.de> Date: Thu, 1 Oct 2020 19:40:43 +0200 Subject: [PATCH] implemented qrcode_svg template filter --- README.md | 1 + uffd/template_helper.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 593db3cc..2ca0bf7a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A web service to manage LDAP users, groups and permissions. - python3-ldap3 - python3-flask - python3-flask-sqlalchemy +- python3-qrcode - git (cli utility, musst be in path) ## development diff --git a/uffd/template_helper.py b/uffd/template_helper.py index 66e1e334..0dff9a43 100644 --- a/uffd/template_helper.py +++ b/uffd/template_helper.py @@ -1,12 +1,27 @@ +from flask import Markup + +import qrcode, qrcode.image.svg + import random import subprocess from datetime import timedelta, datetime +import io def register_template_helper(app): # debian ships jinja2 without this test... def equalto(a, b): return a == b + @app.template_filter() + def qrcode_svg(content, **attrs): + img = qrcode.make(content, image_factory=qrcode.image.svg.SvgPathImage) + svg = img.get_image() + for key, value, in attrs.items(): + svg.set(key, value) + buf = io.BytesIO() + img.save(buf) + return Markup(buf.getvalue().decode()) + @app.url_defaults def static_version_inject(endpoint, values): #pylint: disable=unused-variable if endpoint == 'static': -- GitLab