Skip to content

Commit

Permalink
fix(component): move key_filter validation to the model
Browse files Browse the repository at this point in the history
- This needs to be on the model to validate edits via API.
- Fix test to actually test for validation error.
- Remove not needed r prefix from some strings.
  • Loading branch information
nijel committed Oct 3, 2024
1 parent 16a2c68 commit 8784599
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
9 changes: 0 additions & 9 deletions weblate/trans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
7 changes: 6 additions & 1 deletion weblate/trans/models/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions weblate/trans/tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
Expand 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()

0 comments on commit 8784599

Please sign in to comment.