added path vailidity verification and IOError handling

This commit is contained in:
Kenneth SOARES 2025-04-09 19:31:46 +02:00
parent c0419d0805
commit 61844e4cc5

View File

@ -1,14 +1,25 @@
import pathlib
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from PIL import Image from PIL import Image
from sith.settings import BASE_DIR
class Command(BaseCommand): class Command(BaseCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument("--numero", type=int) parser.add_argument("numero", type=int)
parser.add_argument("--path", type=str) parser.add_argument("path", type=pathlib.Path)
def handle(self, *args, **options): def handle(self, *args, **options):
if options["path"] and options["numero"]: if options["path"].exists():
im = Image.open(options["path"]).resize((120, 120)).convert("RGBA") path = pathlib.Path(
im.save(f"core/static/core/img/promo_{options['numero']}.png", format="PNG") f"{BASE_DIR}/core/static/core/img/promo_{options['numero']}.png"
im.close() )
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.")