You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.2 KiB
62 lines
2.2 KiB
import fnmatch |
|
from os import listdir |
|
from io import BytesIO |
|
from pathlib import Path |
|
import qrcode |
|
from flask import send_file, flash, abort, current_app |
|
from flask_login import current_user |
|
from werkzeug.utils import secure_filename |
|
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 |
|
|
|
def save_player_caught_player_photo(file_storage, game, pcp): |
|
timestamp = pcp.timestamp.strftime('%Y%m%d%H%M%S') |
|
extension = Path(file_storage.filename).suffix |
|
filename = secure_filename(f'{timestamp}_{pcp.catching_player.user.name}_caught_{pcp.caught_player.user.name}{extension}') |
|
path = get_caught_bunny_photo_directory(game) |
|
path.mkdir(parents=True, exist_ok=True) |
|
file_storage.save(path / filename) |
|
|
|
def get_bunny_photo_filename(directory, timestamp, hunter_name, bunny_name): |
|
filename = secure_filename(f'{timestamp}_{hunter_name}_caught_{bunny_name}') + '.*' |
|
matches = fnmatch.filter(listdir(directory), filename) |
|
return matches[0] if matches else '' |
|
|
|
def get_caught_bunny_photo_directory(game): |
|
return Path(current_app.root_path).parent / \ |
|
current_app.config['UPLOAD_FOLDER'] / \ |
|
secure_filename(game.name) / \ |
|
current_app.config['PLAYER_CAUGHT_PLAYER_PHOTO_DIR_NAME']
|
|
|