From df0e4a9b6c7a188af49d7caf96f097ebabf4b16e Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Wed, 9 Apr 2025 17:33:34 +0200 Subject: [PATCH] custom django command for promo logos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit added path vailidity verification and IOError handling added option to overwrite existing logo and force flag improved uppon suggestions mistake correction fixed string conversion bugs and logical error corrected path conversion f better error handling and corrections ajout d'une section de documentation pour la feature copié coller fixed documentation bullet points added resampling clean up error handling removed useless IOError --- core/management/commands/add_promo_logo.py | 41 ++++++++++++++++++++++ docs/howto/logo.md | 9 +++++ 2 files changed, 50 insertions(+) create mode 100644 core/management/commands/add_promo_logo.py diff --git a/core/management/commands/add_promo_logo.py b/core/management/commands/add_promo_logo.py new file mode 100644 index 00000000..7627af90 --- /dev/null +++ b/core/management/commands/add_promo_logo.py @@ -0,0 +1,41 @@ +import pathlib + +from django.apps import apps +from django.core.management.base import BaseCommand +from PIL import Image, UnidentifiedImageError + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("number", type=int) + parser.add_argument("path", type=pathlib.Path) + parser.add_argument("-f", "--force", action="store_true") + + def handle(self, number: int, path: pathlib.Path, force: int, *args, **options): + if not path.exists() or path.is_dir(): + self.stderr.write(f"{path} is not a file or does not exist") + return + + dest_path = ( + pathlib.Path(apps.get_app_config("core").path) + / "static" + / "core" + / "img" + / f"promo_{number}.png" + ) + + if dest_path.exists() and not force: + over = input("File already exists, do you want to overwrite it? (y/N):") + if over.lower() != "y": + self.stdout.write("exiting") + return + try: + im = Image.open(path) + im.resize((120, 120), resample=Image.Resampling.LANCZOS).save( + dest_path, format="PNG" + ) + self.stdout.write( + f"Promo logo moved and resized successfully at {dest_path}" + ) + except UnidentifiedImageError: + self.stderr.write("image cannot be opened and identified.") diff --git a/docs/howto/logo.md b/docs/howto/logo.md index 6db5b289..d292db0e 100644 --- a/docs/howto/logo.md +++ b/docs/howto/logo.md @@ -12,6 +12,15 @@ nouveau logo d'une promo. C'est un processus manuel. de faire cette opération manuellement, ça prend quelques minutes et on est certain de la qualité à la fin. +### avec une commande django +```bash +./manage.py add_promo_logo numero_de_promo chemin_dacces_du_logo +``` +options: + +* `--force/-f` pour automatiquement écraser les logos de promo avec le même nom. + +### manuellement Les logos de promo sont à manuellement ajouter dans le projet. Ils se situent dans le dossier `core/static/core/img/`.