From 61844e4cc5f025cd604e54e48a5abf2590177bcd Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Wed, 9 Apr 2025 19:31:46 +0200 Subject: [PATCH] added path vailidity verification and IOError handling --- core/management/commands/add_promo_logo.py | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/core/management/commands/add_promo_logo.py b/core/management/commands/add_promo_logo.py index b640a621..1aad8448 100644 --- a/core/management/commands/add_promo_logo.py +++ b/core/management/commands/add_promo_logo.py @@ -1,14 +1,25 @@ +import pathlib + from django.core.management.base import BaseCommand from PIL import Image +from sith.settings import BASE_DIR + class Command(BaseCommand): def add_arguments(self, parser): - parser.add_argument("--numero", type=int) - parser.add_argument("--path", type=str) + parser.add_argument("numero", type=int) + parser.add_argument("path", type=pathlib.Path) def handle(self, *args, **options): - if options["path"] and options["numero"]: - im = Image.open(options["path"]).resize((120, 120)).convert("RGBA") - im.save(f"core/static/core/img/promo_{options['numero']}.png", format="PNG") - im.close() + if options["path"].exists(): + path = pathlib.Path( + f"{BASE_DIR}/core/static/core/img/promo_{options['numero']}.png" + ) + try: + with Image.open(options["path"]) as im: + im.resize((120, 120)).save(path, format="PNG") + except IOError as ioe: + self.stderr.write(ioe) + else: + self.stdout.write("file does not exist.")