Coverage for src/backend/InvenTree/common/migrations/0043_auto_20260518_1206.py: 55%
25 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1# Generated by Django 5.2.14 on 2026-05-18 12:06
3import io
4import os
6from PIL import Image
7from tqdm import tqdm
9from time import sleep
11from django.db import migrations
12from django.core.files.base import ContentFile
13from django.core.files.storage import default_storage
16def update_image_attachments(apps, schema_editor):
17 """Update existing attachments to ensure that image attachments have thumbnails.
19 For each existing attachment, check if it is an image.
20 If it is, generate a thumbnail for it.
22 Note: This function mirrors the logic used in the Attachment model's
23 check_is_image method, at the time of writing this migration (2026-05-18).
24 """
26 import common.tasks
28 Attachment = apps.get_model('common', 'Attachment')
30 # Find all Attachment instances which (potentially) have a file attached
31 attachments = Attachment.objects.exclude(attachment__isnull=True).exclude(attachment='')
33 if attachments.count() == 0: 33 ↛ 36line 33 didn't jump to line 36 because the condition on line 33 was always true
34 return
36 progress = tqdm(total=attachments.count(), desc='Migration common.0043: Updating attachments')
38 for attachment in attachments:
39 progress.update(1)
41 if not attachment.attachment:
42 continue
44 if not attachment.attachment.name or not default_storage.exists(attachment.attachment.name):
45 continue
47 common.tasks.rebuild_attachment(attachment.pk)
50class Migration(migrations.Migration):
52 dependencies = [
53 ("common", "0042_attachment_is_image_attachment_thumbnail"),
54 ]
56 operations = [
57 migrations.RunPython(update_image_attachments, reverse_code=migrations.RunPython.noop),
58 ]