Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): move key_filter validation to the model #12664

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading