Add type hints

This commit is contained in:
Joakim Hellsén 2026-03-16 19:35:51 +01:00
commit 25f2d29fb6
Signed by: Joakim Hellsén
SSH key fingerprint: SHA256:/9h/CsExpFp+PRhsfA0xznFx2CGfTT5R/kpuFfUgEQk

View file

@ -21,6 +21,7 @@ if TYPE_CHECKING:
from django.core.management.base import CommandParser from django.core.management.base import CommandParser
from django.db.models import QuerySet from django.db.models import QuerySet
from django.db.models.fields.files import FieldFile from django.db.models.fields.files import FieldFile
from PIL.ImageFile import ImageFile
class Command(BaseCommand): class Command(BaseCommand):
@ -69,7 +70,7 @@ class Command(BaseCommand):
self.stdout.write( self.stdout.write(
self.style.MIGRATE_HEADING("\nProcessing Drop Campaigns..."), self.style.MIGRATE_HEADING("\nProcessing Drop Campaigns..."),
) )
stats = self._download_campaign_images( stats: dict[str, int] = self._download_campaign_images(
client=client, client=client,
limit=limit, limit=limit,
force=force, force=force,
@ -161,7 +162,7 @@ class Command(BaseCommand):
stats["skipped"] += 1 stats["skipped"] += 1
continue continue
result = self._download_image( result: str = self._download_image(
client, client,
campaign.image_url, campaign.image_url,
campaign.twitch_id, campaign.twitch_id,
@ -210,7 +211,7 @@ class Command(BaseCommand):
stats["skipped"] += 1 stats["skipped"] += 1
continue continue
result = self._download_image( result: str = self._download_image(
client, client,
benefit.image_asset_url, benefit.image_asset_url,
benefit.twitch_id, benefit.twitch_id,
@ -259,7 +260,7 @@ class Command(BaseCommand):
stats["skipped"] += 1 stats["skipped"] += 1
continue continue
result = self._download_image( result: str = self._download_image(
client, client,
reward_campaign.image_url, reward_campaign.image_url,
reward_campaign.twitch_id, reward_campaign.twitch_id,
@ -335,17 +336,19 @@ class Command(BaseCommand):
}: }:
return return
base_path = source_path.with_suffix("") base_path: Path = source_path.with_suffix("")
webp_path = base_path.with_suffix(".webp") webp_path: Path = base_path.with_suffix(".webp")
avif_path = base_path.with_suffix(".avif") avif_path: Path = base_path.with_suffix(".avif")
with Image.open(source_path) as img: with Image.open(source_path) as img:
# Convert to RGB if needed # Convert to RGB if needed
if img.mode in {"RGBA", "LA"} or ( if img.mode in {"RGBA", "LA"} or (
img.mode == "P" and "transparency" in img.info img.mode == "P" and "transparency" in img.info
): ):
background = Image.new("RGB", img.size, (255, 255, 255)) background: Image = Image.new("RGB", img.size, (255, 255, 255))
rgba_img = img.convert("RGBA") if img.mode == "P" else img rgba_img: Image | ImageFile = (
img.convert("RGBA") if img.mode == "P" else img
)
background.paste( background.paste(
rgba_img, rgba_img,
mask=rgba_img.split()[-1] mask=rgba_img.split()[-1]
@ -354,9 +357,9 @@ class Command(BaseCommand):
) )
rgb_img = background rgb_img = background
elif img.mode != "RGB": elif img.mode != "RGB":
rgb_img = img.convert("RGB") rgb_img: Image = img.convert("RGB")
else: else:
rgb_img = img rgb_img: ImageFile = img
# Save WebP # Save WebP
rgb_img.save(webp_path, "WEBP", quality=85, method=6) rgb_img.save(webp_path, "WEBP", quality=85, method=6)
@ -387,11 +390,11 @@ class Command(BaseCommand):
), ),
) )
if stats["downloaded"] > 0: if stats["downloaded"] > 0:
media_path = Path(settings.MEDIA_ROOT) media_path: Path = Path(settings.MEDIA_ROOT)
if "Campaigns" in model_name and "Reward" not in model_name: if "Campaigns" in model_name and "Reward" not in model_name:
image_dir = media_path / "campaigns" / "images" image_dir: Path = media_path / "campaigns" / "images"
elif "Benefits" in model_name: elif "Benefits" in model_name:
image_dir = media_path / "benefits" / "images" image_dir: Path = media_path / "benefits" / "images"
else: else:
image_dir = media_path / "reward_campaigns" / "images" image_dir: Path = media_path / "reward_campaigns" / "images"
self.stdout.write(self.style.SUCCESS(f"Saved images to: {image_dir}")) self.stdout.write(self.style.SUCCESS(f"Saved images to: {image_dir}"))