diff --git a/weblate/trans/forms.py b/weblate/trans/forms.py index 68b882a01a73..d3e00b2bbe40 100644 --- a/weblate/trans/forms.py +++ b/weblate/trans/forms.py @@ -1653,15 +1653,6 @@ def clean(self) -> None: if self.hide_restricted: data["restricted"] = self.instance.restricted - if ( - self.instance - and self.instance.key_filter - and not self.instance.file_format_cls.monolingual - ): - raise ValidationError( - gettext("To use the key filter, the file format must be monolingual.") - ) - class ComponentCreateForm(SettingsBaseForm, ComponentDocsMixin, ComponentAntispamMixin): """Component creation form.""" diff --git a/weblate/trans/models/component.py b/weblate/trans/models/component.py index 5dd1b7e50fbe..cf009444f018 100644 --- a/weblate/trans/models/component.py +++ b/weblate/trans/models/component.py @@ -830,7 +830,7 @@ def save(self, *args, **kwargs) -> None: create = True # Sets the key_filter to blank if the file format is bilingual - if self.key_filter and not self.file_format_cls.monolingual: + if self.key_filter and not self.has_template(): self.key_filter = "" if self.id: @@ -3106,6 +3106,11 @@ def clean(self) -> None: {"suggestion_autoaccept": msg, "suggestion_voting": msg} ) + if self.key_filter and not self.has_template(): + raise ValidationError( + gettext("To use the key filter, the file format must be monolingual.") + ) + def get_template_filename(self): """Create absolute filename for template.""" return os.path.join(self.full_path, self.template) diff --git a/weblate/trans/tests/test_component.py b/weblate/trans/tests/test_component.py index 042c88ce3cc1..076cbdd5d861 100644 --- a/weblate/trans/tests/test_component.py +++ b/weblate/trans/tests/test_component.py @@ -1064,7 +1064,7 @@ class ComponentKeyFilterTest(ViewTestCase): """Test the key filtering implementation in Component.""" def create_component(self): - return self.create_android(key_filter=r"^tr") + return self.create_android(key_filter="^tr") def test_get_key_filter_re(self) -> None: self.assertEqual(self.component.key_filter_re.pattern, "^tr") @@ -1076,7 +1076,7 @@ def test_get_filtered_result(self) -> None: self.assertEqual(units.all()[0].context, "try") def test_change_key_filter(self) -> None: - self.component.key_filter = r"^th" + self.component.key_filter = "^th" self.component.save() self.assertEqual(self.component.key_filter_re.pattern, "^th") translations = self.component.translation_set.all() @@ -1096,11 +1096,16 @@ def test_change_key_filter(self) -> None: def test_bilingual_component(self): project = self.component.project component = self.create_po( - name="Bilingual Test", project=project, key_filter=r"^tr" - ) - self.assertRaisesMessage( - ValidationError, - "To use the key filter, the file format must be monolingual.", + name="Bilingual Test", project=project, key_filter="^tr" ) + # Save should remove it self.assertEqual(component.key_filter, "") self.assertEqual(component.key_filter_re.pattern, "") + + # Verify validation will reject it + component.key_filter = "^tr" + with self.assertRaisesMessage( + ValidationError, + "To use the key filter, the file format must be monolingual.", + ): + component.clean()