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 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.")