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