Coverage for src/backend/InvenTree/report/migrations/0023_auto_20240421_0455.py: 36%

39 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1# Generated by Django 4.2.11 on 2024-04-21 04:55 

2 

3import os 

4 

5from django.db import migrations 

6from django.core.files.base import ContentFile 

7 

8 

9def report_model_map(): 

10 """Return a map of model_type: report_type keys.""" 

11 

12 return { 

13 'stockitem': 'testreport', 

14 'stocklocation': 'stocklocationreport', 

15 'build': 'buildreport', 

16 'part': 'billofmaterialsreport', 

17 'purchaseorder': 'purchaseorderreport', 

18 'salesorder': 'salesorderreport', 

19 'returnorder': 'returnorderreport' 

20 } 

21 

22 

23def forward(apps, schema_editor): 

24 """Run forwards migration.  

25  

26 - Create a new ReportTemplate instance for each existing report 

27 """ 

28 

29 # New 'generic' report template model 

30 ReportTemplate = apps.get_model('report', 'reporttemplate') 

31 

32 count = 0 

33 

34 for model_type, report_model in report_model_map().items(): 

35 

36 model = apps.get_model('report', report_model) 

37 

38 for template in model.objects.all(): 38 ↛ 41line 38 didn't jump to line 41 because the loop on line 38 never started

39 # Construct a new ReportTemplate instance 

40 

41 filename = template.template.path 

42 

43 if not os.path.exists(filename): 

44 print(f"Migration error: Template file '{filename}' does not exist") 

45 continue 

46 

47 if '/report/inventree/' in filename: 

48 # Do not migrate internal report templates 

49 continue 

50 

51 filename = os.path.basename(filename) 

52 filedata = template.template.open('r').read() 

53 

54 name = template.name 

55 offset = 1 

56 

57 # Prevent duplicate names during migration 

58 while ReportTemplate.objects.filter(name=name, model_type=model_type).exists(): 

59 name = template.name + f"_{offset}" 

60 offset += 1 

61 

62 ReportTemplate.objects.create( 

63 name=name, 

64 template=ContentFile(filedata, filename), 

65 model_type=model_type, 

66 description=template.description, 

67 revision=template.revision, 

68 filters=template.filters, 

69 filename_pattern=template.filename_pattern, 

70 enabled=template.enabled, 

71 page_size=template.page_size, 

72 landscape=template.landscape, 

73 ) 

74 

75 count += 1 

76 

77 if count > 0: 77 ↛ 78line 77 didn't jump to line 78 because the condition on line 77 was never true

78 print(f"Migrated {count} report templates to new ReportTemplate model.") 

79 

80 

81def reverse(apps, schema_editor): 

82 """Run reverse migration. 

83 

84 - Delete any ReportTemplate instances in the database 

85 """ 

86 ReportTemplate = apps.get_model('report', 'reporttemplate') 

87 

88 n = ReportTemplate.objects.count() 

89 

90 if n > 0: 

91 for item in ReportTemplate.objects.all(): 

92 item.template.delete() 

93 item.delete() 

94 

95 print(f"Deleted {n} ReportTemplate objects and templates") 

96 

97 

98class Migration(migrations.Migration): 

99 

100 dependencies = [ 

101 ('report', '0022_reporttemplate'), 

102 ] 

103 

104 operations = [ 

105 migrations.RunPython(forward, reverse_code=reverse) 

106 ]