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

1# Generated by Django 5.2.14 on 2026-05-18 12:06 

2 

3import io 

4import os 

5 

6from PIL import Image 

7from tqdm import tqdm 

8 

9from time import sleep 

10 

11from django.db import migrations 

12from django.core.files.base import ContentFile 

13from django.core.files.storage import default_storage 

14 

15 

16def update_image_attachments(apps, schema_editor): 

17 """Update existing attachments to ensure that image attachments have thumbnails. 

18  

19 For each existing attachment, check if it is an image. 

20 If it is, generate a thumbnail for it. 

21 

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

25 

26 import common.tasks 

27 

28 Attachment = apps.get_model('common', 'Attachment') 

29 

30 # Find all Attachment instances which (potentially) have a file attached 

31 attachments = Attachment.objects.exclude(attachment__isnull=True).exclude(attachment='') 

32 

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 

35 

36 progress = tqdm(total=attachments.count(), desc='Migration common.0043: Updating attachments') 

37 

38 for attachment in attachments: 

39 progress.update(1) 

40 

41 if not attachment.attachment: 

42 continue 

43 

44 if not attachment.attachment.name or not default_storage.exists(attachment.attachment.name): 

45 continue 

46 

47 common.tasks.rebuild_attachment(attachment.pk) 

48 

49 

50class Migration(migrations.Migration): 

51 

52 dependencies = [ 

53 ("common", "0042_attachment_is_image_attachment_thumbnail"), 

54 ] 

55 

56 operations = [ 

57 migrations.RunPython(update_image_attachments, reverse_code=migrations.RunPython.noop), 

58 ]