Skip to content

Commit 92cb5e8

Browse files
committed
Add LocalizedValue.is_empty()
1 parent 5c298ef commit 92cb5e8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

localized_fields/value.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def translate(self, language: Optional[str] = None) -> Optional[str]:
136136

137137
return None
138138

139+
def is_empty(self) -> bool:
140+
"""Gets whether all the languages contain the default value."""
141+
142+
for lang_code, _ in settings.LANGUAGES:
143+
if self.get(lang_code) != self.default_value:
144+
return False
145+
146+
return True
147+
139148
def __str__(self) -> str:
140149
"""Gets the value in the current language or falls back to the next
141150
language if there's no value in the current language."""

tests/test_value.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ def test_init_default_values():
3838
for lang_code, _ in settings.LANGUAGES:
3939
assert getattr(value, lang_code) is None
4040

41+
@staticmethod
42+
def test_is_empty():
43+
"""Tests whether a newly constructed :see:LocalizedValue without any
44+
content is considered "empty"."""
45+
46+
value = LocalizedValue()
47+
assert value.is_empty()
48+
49+
value.set(settings.LANGUAGE_CODE, "my value")
50+
assert not value.is_empty()
51+
4152
@staticmethod
4253
def test_init_array():
4354
"""Tests whether the __init__ function of :see:LocalizedValue properly

0 commit comments

Comments
 (0)