mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-24 19:32:53 +03:00
Add LocalizedFloatValue
This commit is contained in:
parent
21a42a383c
commit
0d9ec6385c
@ -257,4 +257,39 @@ class LocalizedIntegerValue(LocalizedValue):
|
||||
"""Returns string representation of value."""
|
||||
|
||||
value = self.translate()
|
||||
return str(value) if value is not None else ""
|
||||
return str(value) if value is not None else ''
|
||||
|
||||
|
||||
class LocalizedFloatValue(LocalizedValue):
|
||||
"""All values are floats"""
|
||||
|
||||
default_value = None
|
||||
|
||||
def translate(self):
|
||||
"""
|
||||
Gets the value in the current language, or in the configured
|
||||
fallback language.
|
||||
"""
|
||||
|
||||
value = super().translate()
|
||||
if value is None or (isinstance(value, str) and value.strip() == ''):
|
||||
return None
|
||||
|
||||
return float(value)
|
||||
|
||||
def __float__(self):
|
||||
"""
|
||||
Gets the value in the current language as a float
|
||||
"""
|
||||
value = self.translate()
|
||||
if value is None:
|
||||
return self.default_value
|
||||
|
||||
return float(value)
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""
|
||||
Returns the string repsentation of value
|
||||
"""
|
||||
value = self.translate
|
||||
return str(value) if value is not None else ''
|
||||
|
Loading…
x
Reference in New Issue
Block a user