|
|
|
from io import BytesIO
|
|
|
|
import qrcode
|
|
|
|
from flask import send_file, flash, abort
|
|
|
|
from flask_login import current_user
|
|
|
|
from app.models import Game
|
|
|
|
|
|
|
|
def generate_qr_code(url):
|
|
|
|
qr = qrcode.QRCode(
|
|
|
|
version=None,
|
|
|
|
error_correction=qrcode.constants.ERROR_CORRECT_M,
|
|
|
|
box_size=30,
|
|
|
|
border=4,
|
|
|
|
)
|
|
|
|
qr.add_data(url)
|
|
|
|
qr.make(fit=True)
|
|
|
|
return qr.make_image(fill_color='black', back_color='white')
|
|
|
|
|
|
|
|
def serve_pil_image(pil_img):
|
|
|
|
# Source: https://stackoverflow.com/questions/7877282/how-to-send-image-generated-by-pil-to-browser
|
|
|
|
img_io = BytesIO()
|
|
|
|
pil_img.save(img_io, 'PNG', quality=70)
|
|
|
|
img_io.seek(0)
|
|
|
|
return send_file(img_io, mimetype='image/png')
|
|
|
|
|
|
|
|
def flash_errors(form):
|
|
|
|
"""Flashes form errors"""
|
|
|
|
print('a')
|
|
|
|
for field, errors in form.errors.items():
|
|
|
|
for error in errors:
|
|
|
|
flash(u"Error in the %s field - %s" % (
|
|
|
|
getattr(form, field).label.text,
|
|
|
|
error
|
|
|
|
), 'error')
|
|
|
|
|
|
|
|
def get_game_if_owner(game_name):
|
|
|
|
game = Game.query.filter_by(name=game_name).first_or_404()
|
|
|
|
if not game.owned_by(current_user):
|
|
|
|
abort(403)
|
|
|
|
return game
|