Skip to content

Commit

Permalink
Unit: Consistent properties behavior
Browse files Browse the repository at this point in the history
Make all is_* cached properties. Having method among them is confusing
and easy to mistake in invocation.
  • Loading branch information
nijel committed Sep 21, 2020
1 parent d2a137a commit 4b023f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion weblate/trans/models/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def update_units(self, units, store, author_name, author_id):
store.add_unit(pounit.unit)

# Store translations
if unit.is_plural():
if unit.is_plural:
pounit.set_target(unit.get_target_plurals())
else:
pounit.set_target(unit.target)
Expand Down
7 changes: 4 additions & 3 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def update_from_unit(self, unit, pos, created):
self.update_priority(save=False)

# Sanitize number of plurals
if self.is_plural():
if self.is_plural:
self.target = join_plural(self.get_target_plurals())

if created:
Expand Down Expand Up @@ -609,11 +609,12 @@ def update_priority(self, save=True):
same_content=True, same_state=True, update_fields=["priority"]
)

@cached_property
def is_plural(self):
"""Check whether message is plural."""
return is_plural(self.source) or is_plural(self.target)

@property
@cached_property
def is_source(self):
return self.source_unit_id is None or self.source_unit_id == self.id

Expand All @@ -638,7 +639,7 @@ def source_string(self):
def get_target_plurals(self):
"""Return target plurals in array."""
# Is this plural?
if not self.is_plural():
if not self.is_plural:
return [self.target]

# Split plurals
Expand Down
6 changes: 3 additions & 3 deletions weblate/trans/tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,22 @@ def test_create_ts(self):
unit = Unit.objects.get(
source__startswith="Orangutan", translation__language_code="cs"
)
self.assertTrue(unit.is_plural())
self.assertTrue(unit.is_plural)
self.assertFalse(unit.translated)
self.assertFalse(unit.fuzzy)

unit = Unit.objects.get(
source__startswith="Hello", translation__language_code="cs"
)
self.assertFalse(unit.is_plural())
self.assertFalse(unit.is_plural)
self.assertTrue(unit.translated)
self.assertFalse(unit.fuzzy)
self.assertEqual(unit.target, "Hello, world!\n")

unit = Unit.objects.get(
source__startswith="Thank ", translation__language_code="cs"
)
self.assertFalse(unit.is_plural())
self.assertFalse(unit.is_plural)
self.assertFalse(unit.translated)
self.assertTrue(unit.fuzzy)
self.assertEqual(unit.target, "Thanks")
Expand Down

0 comments on commit 4b023f7

Please sign in to comment.