ajout localize-logo flask command

This commit is contained in:
Jean-Marie Place 2021-11-14 10:43:55 +01:00
parent 51506c6d6f
commit a6c95b013b
2 changed files with 49 additions and 0 deletions

View File

@ -32,9 +32,11 @@ avec `ext` membre de LOGOS_IMAGES_ALLOWED_TYPES (= jpg, png)
SCODOC_LOGOS_DIR /opt/scodoc-data/config/logos SCODOC_LOGOS_DIR /opt/scodoc-data/config/logos
""" """
import glob
import imghdr import imghdr
import os import os
import re import re
import shutil
from pathlib import Path from pathlib import Path
from flask import abort, current_app, url_for from flask import abort, current_app, url_for
@ -257,3 +259,36 @@ def guess_image_type(stream) -> str:
if not fmt: if not fmt:
return None return None
return fmt if fmt != "jpeg" else "jpg" 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")

View File

@ -349,6 +349,20 @@ def migrate_scodoc7_dept_logos(dept: str = ""): # migrate-scodoc7-dept-logos
tools.migrate_scodoc7_dept_logos(dept) 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() @app.cli.command()
@click.argument("formsemestre_id", type=click.INT) @click.argument("formsemestre_id", type=click.INT)
@click.argument("xlsfile", type=click.File("rb")) @click.argument("xlsfile", type=click.File("rb"))