From b336a1c1a26abc7857b67df992a05052e831b5fe Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Sun, 7 Nov 2021 09:19:27 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20URL=20de=20r=C3=A9cup=C3=A9raions=20des?= =?UTF-8?q?=20logos=20(deprecates=20/ScoDoc/logo=5Fheader=20et=20/ScoDoc/l?= =?UTF-8?q?ogo=5Ffooter)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/scodoc.py | 93 +++++++++++++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 24 deletions(-) diff --git a/app/views/scodoc.py b/app/views/scodoc.py index c8ea5aad..0def371c 100644 --- a/app/views/scodoc.py +++ b/app/views/scodoc.py @@ -30,6 +30,8 @@ Module main: page d'accueil, avec liste des départements Emmanuel Viennet, 2021 """ +import io + from app.auth.models import User import os @@ -38,7 +40,7 @@ from flask import abort, flash, url_for, redirect, render_template, send_file from flask import request from flask.app import Flask import flask_login -from flask_login.utils import login_required +from flask_login.utils import login_required, current_user from flask_wtf import FlaskForm from flask_wtf.file import FileField, FileAllowed from werkzeug.exceptions import BadRequest, NotFound @@ -65,6 +67,8 @@ from app.scodoc.sco_exceptions import AccessDenied from app.scodoc.sco_permissions import Permission from app.views import scodoc_bp as bp +from PIL import Image as PILImage + @bp.route("/") @bp.route("/ScoDoc") @@ -238,13 +242,9 @@ def configuration(): if form.validate_on_submit(): ScoDocSiteConfig.set_bonus_sport_func(form.bonus_sport_func_name.data) if form.logo_header.data: - sco_logos.store_image( - form.logo_header.data, os.path.join(scu.SCODOC_LOGOS_DIR, "logo_header") - ) + sco_logos.write_logo(stream=form.logo_header.data, name="header") if form.logo_footer.data: - sco_logos.store_image( - form.logo_footer.data, os.path.join(scu.SCODOC_LOGOS_DIR, "logo_footer") - ) + sco_logos.write_logo(stream=form.logo_footer.data, name="footer") app.clear_scodoc_cache() flash(f"Configuration enregistrée") return redirect(url_for("scodoc.index")) @@ -257,29 +257,74 @@ def configuration(): ) -def _return_logo(logo_type="header", scodoc_dept=""): +SMALL_SIZE = (300, 300) + + +def _return_logo(name="header", dept_id="", small=False, strict: bool = True): # stockée dans /opt/scodoc-data/config/logos donc servie manuellement ici - filename = sco_logos.get_logo_filename(logo_type, scodoc_dept) - if filename: - extension = os.path.splitext(filename)[1] - return send_file(filename, mimetype=f"image/{extension}") + logo = sco_logos.find_logo(name, dept_id, strict) + if logo is not None: + suffix = logo.suffix + if small: + with PILImage.open(logo.filepath) as im: + im.thumbnail(SMALL_SIZE) + stream = io.BytesIO() + # on garde le même format (on pourrait plus simplement générer systématiquement du JPEG) + fmt = { # adapt suffix to be compliant with PIL save format + "PNG": "PNG", + "JPG": "JPEG", + "JPEG": "JPEG", + }[suffix.upper()] + im.save(stream, fmt) + stream.seek(0) + return send_file(stream, mimetype=f"image/{fmt}") + else: + return send_file(logo.filepath, mimetype=f"image/{suffix}") else: - return "" + abort(404) -@bp.route("/ScoDoc/logo_header") -@bp.route("/ScoDoc//logo_header") -def logo_header(scodoc_dept=""): - "Image logo header" - # "/opt/scodoc-data/config/logos/logo_header") - return _return_logo(logo_type="header", scodoc_dept=scodoc_dept) +# small version (copy/paste from get_logo +@bp.route("/ScoDoc/logos//small", defaults={"dept_id": None}) +@bp.route("/ScoDoc//logos//small") +@admin_required +def get_logo_small(name: str, dept_id: int): + strict = request.args.get("strict", "False") + return _return_logo( + name, + dept_id=dept_id, + small=True, + strict=strict.upper() not in ["0", "FALSE"], + ) -@bp.route("/ScoDoc/logo_footer") -@bp.route("/ScoDoc//logo_footer") -def logo_footer(scodoc_dept=""): - "Image logo footer" - return _return_logo(logo_type="footer", scodoc_dept=scodoc_dept) +@bp.route( + "/ScoDoc/logos/", defaults={"dept_id": None} +) # if dept not specified, take global logo +@bp.route("/ScoDoc//logos/") +@admin_required +def get_logo(name: str, dept_id: int): + strict = request.args.get("strict", "False") + return _return_logo( + name, + dept_id=dept_id, + small=False, + strict=strict.upper() not in ["0", "FALSE"], + ) + + +# @bp.route("/ScoDoc/logo_header") +# @bp.route("/ScoDoc//logo_header") +# def logo_header(scodoc_dept=""): +# "Image logo header" +# return _return_logo(name="header", scodoc_dept=scodoc_dept) + + +# @bp.route("/ScoDoc/logo_footer") +# @bp.route("/ScoDoc//logo_footer") +# def logo_footer(scodoc_dept=""): +# "Image logo footer" +# return _return_logo(name="footer", scodoc_dept=scodoc_dept) # essais