Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/migrations/0040_auto_20250613_1813.py: 37%

39 statements  

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

1# Copyright © Michal Čihař <michal@weblate.org> 

2# 

3# SPDX-License-Identifier: GPL-3.0-or-later 

4# Generated by Django 5.2.1 on 2025-06-13 18:13 

5 

6from django.conf import settings 

7from django.db import migrations 

8 

9from weblate.trans.actions import ACTIONS_CONTENT 

10 

11 

12def migrate_changes(apps, schema_editor) -> None: 

13 PendingUnitChange = apps.get_model("trans", "PendingUnitChange") 

14 Unit = apps.get_model("trans", "Unit") 

15 

16 User = apps.get_model(*settings.AUTH_USER_MODEL.split(".")) # noqa: N806 

17 anonymous_user = None 

18 

19 pending_units = Unit.objects.filter(pending=True) 

20 

21 pending_changes = [] 

22 for unit in pending_units: 22 ↛ 23line 22 didn't jump to line 23 because the loop on line 22 never started

23 try: 

24 # can be None in some cases, for instance unit updated in the 

25 # repository outside of weblate 

26 author = ( 

27 unit.change_set.filter(action__in=ACTIONS_CONTENT) 

28 .select_related("author") 

29 .order_by("-timestamp")[0] 

30 .author 

31 ) 

32 except IndexError: 

33 author = None 

34 

35 if author is None: 

36 # load anonymous user lazily as it doesn't exist when a new database 

37 # is created, such a database would not have any pending units either, 

38 # and this code block would never be executed. 

39 if anonymous_user is None: 

40 anonymous_user = User.objects.get(username=settings.ANONYMOUS_USER_NAME) 

41 author = anonymous_user 

42 

43 pending_changes.append( 

44 PendingUnitChange( 

45 unit=unit, 

46 author=author, 

47 target=unit.target, 

48 explanation=unit.explanation, 

49 state=unit.state, 

50 source_unit_explanation=unit.source_unit.explanation, 

51 add_unit=unit.details.get("add_unit", False), 

52 ) 

53 ) 

54 PendingUnitChange.objects.bulk_create(pending_changes) 

55 

56 pending_units.update(pending=False) 

57 

58 

59def reverse_migrate_changes(apps, schema_editor) -> None: 

60 PendingUnitChange = apps.get_model("trans", "PendingUnitChange") 

61 Unit = apps.get_model("trans", "Unit") 

62 pending_changes = PendingUnitChange.objects.select_related("unit").all() 

63 all_units_to_update = [] 

64 add_unit_updates = [] 

65 for change in PendingUnitChange.objects.select_related("unit").iterator(): 

66 pk = change.unit.pk 

67 all_units_to_update.append(pk) 

68 if change.add_unit: 

69 add_unit_updates.append(pk) 

70 Unit.objects.filter(pk__in=all_units_to_update).update(pending=True) 

71 Unit.objects.filter(pk__in=add_unit_updates).update(details={"add_unit": True}) 

72 pending_changes.delete() 

73 

74 

75class Migration(migrations.Migration): 

76 dependencies = [ 

77 ("weblate_auth", "0006_group_enforced_2fa"), 

78 ("trans", "0039_pendingunitchange"), 

79 ] 

80 

81 operations = [ 

82 migrations.RunPython(migrate_changes, reverse_migrate_changes), 

83 ]