From 936ea0ea8e8fd50b15715eb0d9ddf8c9b503b0d0 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 11 May 2022 11:20:15 +0200 Subject: [PATCH] Almet/qrcode (#1000) Add a QRCode utility to join the project. --- ihatemoney/templates/send_invites.html | 9 +++++++++ ihatemoney/web.py | 21 ++++++++++++++++++++- setup.cfg | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ihatemoney/templates/send_invites.html b/ihatemoney/templates/send_invites.html index d5112926..7983d61f 100644 --- a/ihatemoney/templates/send_invites.html +++ b/ihatemoney/templates/send_invites.html @@ -26,6 +26,15 @@ + + +

{{ _('Scan QR code') }}

+

{% trans download_mobile=url_for(".mobile") %}On a mobile device, with a compatible app installed.{% endtrans %}

+ + + {{ qrcode | safe }} + +

{{ _('Send via Emails') }}

diff --git a/ihatemoney/web.py b/ihatemoney/web.py index dd31c152..e682cbd9 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -9,8 +9,10 @@ some shortcuts to make your life better when coding (see `pull_project` and `add_project_id` for a quick overview) """ from functools import wraps +from io import StringIO import json import os +from urllib.parse import urlparse, urlunparse from flask import ( Blueprint, @@ -28,6 +30,8 @@ from flask import ( ) from flask_babel import gettext as _ from flask_mail import Message +import qrcode +import qrcode.image.svg from sqlalchemy_continuum import Operation from werkzeug.exceptions import NotFound from werkzeug.security import check_password_hash, generate_password_hash @@ -586,7 +590,22 @@ def invite(): "Sorry, there was an error while trying to send the invitation emails." ) # Fall-through: we stay on the same page and display the form again - return render_template("send_invites.html", form=form) + + # Generate the SVG QRCode. + invite_link = url_for( + ".join_project", + project_id=g.project.id, + token=g.project.generate_token(), + _external=True, + ) + invite_link = urlunparse(urlparse(invite_link)._replace(scheme="ihatemoney")) + qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgPathImage) + qr.add_data(invite_link) + qr.make(fit=True) + img = qr.make_image(attrib={"class": "qrcode"}) + qrcode_svg = img.to_string().decode() + + return render_template("send_invites.html", form=form, qrcode=qrcode_svg) @main.route("//") diff --git a/setup.cfg b/setup.cfg index 1404bd91..76802e36 100644 --- a/setup.cfg +++ b/setup.cfg @@ -40,6 +40,7 @@ install_requires = Werkzeug>=2,<2.1 # See https://github.com/spiral-project/ihatemoney/pull/1015 itsdangerous>=2,<3 Jinja2>=3,<4 + qrcode>=7,<8 requests>=2.22,<3 SQLAlchemy-Continuum>=1.3.12,<2 SQLAlchemy>=1.3.0,<1.4 # New 1.4 changes API, see #728