dnscms: version image URLs by file hash so media can be cached as immutable
dnscms / ci (push) Successful in 1m10s
web / ci (push) Successful in 2m38s

This commit is contained in:
ponas committed 2026-10-15 01:43:10 +02:00
1 parent fb1453b419
commit cff6376129
4 files changed
+122 -5

No files matched your search

@@ -0,0 +1,32 @@
from django.db import migrations
from wagtail.utils.file import hash_filelike
def backfill_file_hash_and_size(apps, schema_editor):
"""
Images from the 2024 bulk import were created without Wagtail's upload
form, which is what sets file_hash and file_size. The hash versions image
URLs (see CustomImage.versioned_url).
"""
CustomImage = apps.get_model("images", "CustomImage")
images = CustomImage.objects.filter(file_hash="") | CustomImage.objects.filter(
file_size__isnull=True
)
for image in images.iterator():
try:
with image.file.open("rb") as f:
image.file_hash = hash_filelike(f)
image.file_size = image.file.size
except FileNotFoundError:
continue
image.save(update_fields=["file_hash", "file_size"])
class Migration(migrations.Migration):
dependencies = [
("images", "0005_customimage_description"),
]
operations = [
migrations.RunPython(backfill_file_hash_and_size, migrations.RunPython.noop),
]