mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-24 19:32:53 +03:00
Create LocalizedNumericValue with __int__ and __float__ methods
This commit is contained in:
parent
0f1d6636f6
commit
5e1d46669c
@ -229,24 +229,9 @@ class LocalizedFileValue(LocalizedValue):
|
||||
return self.get(translation.get_language())
|
||||
|
||||
|
||||
class LocalizedIntegerValue(LocalizedValue):
|
||||
"""All values are integers."""
|
||||
|
||||
default_value = None
|
||||
|
||||
def translate(self):
|
||||
"""Gets the value in the current language, or in the configured fallbck
|
||||
language."""
|
||||
|
||||
value = super().translate()
|
||||
if value is None or (isinstance(value, str) and value.strip() == ""):
|
||||
return None
|
||||
|
||||
return int(value)
|
||||
|
||||
class LocalizedNumericValue(LocalizedValue):
|
||||
def __int__(self):
|
||||
"""Gets the value in the current language as an integer."""
|
||||
|
||||
value = self.translate()
|
||||
if value is None:
|
||||
return self.default_value
|
||||
@ -259,8 +244,32 @@ class LocalizedIntegerValue(LocalizedValue):
|
||||
value = self.translate()
|
||||
return str(value) if value is not None else ''
|
||||
|
||||
def __float__(self):
|
||||
"""Gets the value in the current language as a float"""
|
||||
value = self.translate()
|
||||
if value is None:
|
||||
return self.default_value
|
||||
|
||||
class LocalizedFloatValue(LocalizedValue):
|
||||
return float(value)
|
||||
|
||||
|
||||
class LocalizedIntegerValue(LocalizedNumericValue):
|
||||
"""All values are integers."""
|
||||
|
||||
default_value = None
|
||||
|
||||
def translate(self):
|
||||
"""Gets the value in the current language, or
|
||||
in the configured fallbck language."""
|
||||
|
||||
value = super().translate()
|
||||
if value is None or (isinstance(value, str) and value.strip() == ''):
|
||||
return None
|
||||
|
||||
return int(value)
|
||||
|
||||
|
||||
class LocalizedFloatValue(LocalizedNumericValue):
|
||||
"""All values are floats"""
|
||||
|
||||
default_value = None
|
||||
@ -270,26 +279,8 @@ class LocalizedFloatValue(LocalizedValue):
|
||||
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