From a6c95b013bdf87d88e7e9ba1c858c50204adfb82 Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Sun, 14 Nov 2021 10:43:55 +0100 Subject: [PATCH] ajout localize-logo flask command --- app/scodoc/sco_logos.py | 35 +++++++++++++++++++++++++++++++++++ scodoc.py | 14 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/app/scodoc/sco_logos.py b/app/scodoc/sco_logos.py index dba5dc61e..d2814fe6f 100644 --- a/app/scodoc/sco_logos.py +++ b/app/scodoc/sco_logos.py @@ -32,9 +32,11 @@ avec `ext` membre de LOGOS_IMAGES_ALLOWED_TYPES (= jpg, png) SCODOC_LOGOS_DIR /opt/scodoc-data/config/logos """ +import glob import imghdr import os import re +import shutil from pathlib import Path from flask import abort, current_app, url_for @@ -257,3 +259,36 @@ def guess_image_type(stream) -> str: if not fmt: return None return fmt if fmt != "jpeg" else "jpg" + + +def make_logo_local(logoname, dept_name): + breakpoint() + depts = Departement.query.filter_by(acronym=dept_name).all() + if len(depts) == 0: + print(f"no dept {dept_name} found. aborting") + return + if len(depts) > 1: + print(f"several depts {dept_name} found. aborting") + return + dept = depts[0] + print(f"Move logo {logoname}' from global to {dept.acronym}") + old_path_wild = f"/opt/scodoc-data/config/logos/logo_{logoname}.*" + new_dir = f"/opt/scodoc-data/config/logos/logos_{dept.id}" + logos = glob.glob(old_path_wild) + # checks that there is non local already present + for logo in logos: + filename = os.path.split(logo)[1] + new_name = os.path.sep.join([new_dir, filename]) + if os.path.exists(new_name): + print("local version of global logo already exists. aborting") + return + # create new__dir if necessary + if not os.path.exists(new_dir): + print(f"- create {new_dir} directory") + os.mkdir(new_dir) + # move global logo (all suffixes) to local dir note: pre existent file (logo_XXX.*) in local dir does not + # prevent operation if there is no conflict with moved files + # At this point everything is ok so we can do files manipulation + for logo in logos: + shutil.move(logo, new_dir) + # print(f"moved {n_moves}/{n} etuds") diff --git a/scodoc.py b/scodoc.py index 6b34d941c..3204c7668 100755 --- a/scodoc.py +++ b/scodoc.py @@ -349,6 +349,20 @@ def migrate_scodoc7_dept_logos(dept: str = ""): # migrate-scodoc7-dept-logos tools.migrate_scodoc7_dept_logos(dept) +@app.cli.command() +@click.argument("logo", default=None) +@click.argument("dept", default=None) +@with_appcontext +def localize_logo(logo: str = None, dept: str = None): # migrate-scodoc7-dept-logos + """Make local to a dept a global logo (both logo and dept names are mandatory)""" + if logo in ["header", "footer"]: + print( + f"Can't make logo '{logo}' local: add a local version throught configuration form instead" + ) + return + make_logo_local(logoname=logo, dept_name=dept) + + @app.cli.command() @click.argument("formsemestre_id", type=click.INT) @click.argument("xlsfile", type=click.File("rb"))