1
0
forked from ScoDoc/ScoDoc

ajout URL de récupéraions des logos (deprecates /ScoDoc/logo_header et /ScoDoc/logo_footer)

This commit is contained in:
Jean-Marie Place 2021-11-07 09:19:27 +01:00
parent 29c9982afc
commit b336a1c1a2

View File

@ -30,6 +30,8 @@ Module main: page d'accueil, avec liste des départements
Emmanuel Viennet, 2021 Emmanuel Viennet, 2021
""" """
import io
from app.auth.models import User from app.auth.models import User
import os import os
@ -38,7 +40,7 @@ from flask import abort, flash, url_for, redirect, render_template, send_file
from flask import request from flask import request
from flask.app import Flask from flask.app import Flask
import flask_login 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 import FlaskForm
from flask_wtf.file import FileField, FileAllowed from flask_wtf.file import FileField, FileAllowed
from werkzeug.exceptions import BadRequest, NotFound 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.scodoc.sco_permissions import Permission
from app.views import scodoc_bp as bp from app.views import scodoc_bp as bp
from PIL import Image as PILImage
@bp.route("/") @bp.route("/")
@bp.route("/ScoDoc") @bp.route("/ScoDoc")
@ -238,13 +242,9 @@ def configuration():
if form.validate_on_submit(): if form.validate_on_submit():
ScoDocSiteConfig.set_bonus_sport_func(form.bonus_sport_func_name.data) ScoDocSiteConfig.set_bonus_sport_func(form.bonus_sport_func_name.data)
if form.logo_header.data: if form.logo_header.data:
sco_logos.store_image( sco_logos.write_logo(stream=form.logo_header.data, name="header")
form.logo_header.data, os.path.join(scu.SCODOC_LOGOS_DIR, "logo_header")
)
if form.logo_footer.data: if form.logo_footer.data:
sco_logos.store_image( sco_logos.write_logo(stream=form.logo_footer.data, name="footer")
form.logo_footer.data, os.path.join(scu.SCODOC_LOGOS_DIR, "logo_footer")
)
app.clear_scodoc_cache() app.clear_scodoc_cache()
flash(f"Configuration enregistrée") flash(f"Configuration enregistrée")
return redirect(url_for("scodoc.index")) 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 # stockée dans /opt/scodoc-data/config/logos donc servie manuellement ici
filename = sco_logos.get_logo_filename(logo_type, scodoc_dept) logo = sco_logos.find_logo(name, dept_id, strict)
if filename: if logo is not None:
extension = os.path.splitext(filename)[1] suffix = logo.suffix
return send_file(filename, mimetype=f"image/{extension}") 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: else:
return "" abort(404)
@bp.route("/ScoDoc/logo_header") # small version (copy/paste from get_logo
@bp.route("/ScoDoc/<scodoc_dept>/logo_header") @bp.route("/ScoDoc/logos/<name>/small", defaults={"dept_id": None})
def logo_header(scodoc_dept=""): @bp.route("/ScoDoc/<int:dept_id>/logos/<name>/small")
"Image logo header" @admin_required
# "/opt/scodoc-data/config/logos/logo_header") def get_logo_small(name: str, dept_id: int):
return _return_logo(logo_type="header", scodoc_dept=scodoc_dept) 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(
@bp.route("/ScoDoc/<scodoc_dept>/logo_footer") "/ScoDoc/logos/<name>", defaults={"dept_id": None}
def logo_footer(scodoc_dept=""): ) # if dept not specified, take global logo
"Image logo footer" @bp.route("/ScoDoc/<int:dept_id>/logos/<name>")
return _return_logo(logo_type="footer", scodoc_dept=scodoc_dept) @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/<scodoc_dept>/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/<scodoc_dept>/logo_footer")
# def logo_footer(scodoc_dept=""):
# "Image logo footer"
# return _return_logo(name="footer", scodoc_dept=scodoc_dept)
# essais # essais