Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/checklists.py: 25%

57 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 

5from __future__ import annotations 

6 

7from collections import UserList 

8from typing import TYPE_CHECKING, Protocol 

9 

10from django.utils.functional import cached_property 

11 

12from weblate.checks.models import CHECKS 

13from weblate.trans.filter import FILTERS 

14 

15if TYPE_CHECKING: 15 ↛ 16line 15 didn't jump to line 16 because the condition on line 15 was never true

16 from weblate.trans.models.project import Project 

17 from weblate.utils.stats import BaseStats 

18 

19 

20class TranslationProtocol(Protocol): 

21 project: Project 

22 stats: BaseStats 

23 enable_review: bool 

24 is_readonly: bool 

25 is_source: bool 

26 

27 

28class TranslationChecklistMixin: 

29 @cached_property 

30 def list_translation_checks(self: TranslationProtocol) -> TranslationChecklist: 

31 """Return list of failing checks on current translation.""" 

32 result = TranslationChecklist() 

33 

34 # All strings 

35 result.add(self.stats, "all", "") 

36 

37 result.add_if( 

38 self.stats, "readonly", "primary" if self.enable_review else "success" 

39 ) 

40 

41 if not self.is_readonly: 

42 if self.enable_review: 

43 result.add_if(self.stats, "approved", "primary") 

44 

45 # Count of translated strings 

46 result.add_if(self.stats, "translated", "success") 

47 

48 # To approve 

49 if self.enable_review: 

50 result.add_if(self.stats, "unapproved", "success") 

51 

52 # Approved with suggestions 

53 result.add_if(self.stats, "approved_suggestions", "primary") 

54 

55 # Unfinished strings 

56 result.add_if(self.stats, "todo", "") 

57 

58 # Untranslated strings 

59 result.add_if(self.stats, "nottranslated", "") 

60 

61 # Fuzzy strings 

62 result.add_if(self.stats, "fuzzy", "") 

63 

64 # Translations with suggestions 

65 if result.add_if(self.stats, "suggestions", ""): 

66 result.add_if(self.stats, "nosuggestions", "") 

67 

68 # All checks 

69 result.add_if(self.stats, "allchecks", "") 

70 

71 # Translated strings with checks 

72 if not self.is_source: 

73 result.add_if(self.stats, "translated_checks", "") 

74 

75 # Dismissed checks 

76 result.add_if(self.stats, "dismissed_checks", "") 

77 

78 # Process specific checks 

79 for check in CHECKS: 

80 check_obj = CHECKS[check] 

81 result.add_if(self.stats, check_obj.url_id, "") 

82 

83 # Grab comments 

84 result.add_if(self.stats, "comments", "") 

85 

86 # Include labels 

87 labels = self.project.label_set.order_by("name") 

88 if labels: 

89 has_label = False 

90 for label in labels: 

91 has_label |= result.add_if( 

92 self.stats, 

93 f"label:{label.name}", 

94 f"label label-{label.color}", 

95 ) 

96 if has_label: 

97 result.add_if(self.stats, "unlabeled", "") 

98 

99 return result 

100 

101 

102class TranslationChecklist(UserList): 

103 """Simple list wrapper for translation checklist.""" 

104 

105 def add_if(self, stats, name, level) -> bool: 

106 """Add to list if there are matches.""" 

107 if getattr(stats, name) > 0: 

108 self.add(stats, name, level) 

109 return True 

110 return False 

111 

112 def add(self, stats, name, level) -> None: 

113 """Add item to the list.""" 

114 self.append( 

115 ( 

116 FILTERS.get_filter_query(name), 

117 FILTERS.get_filter_name(name), 

118 getattr(stats, name), 

119 level, 

120 getattr(stats, f"{name}_words"), 

121 getattr(stats, f"{name}_chars"), 

122 ) 

123 )