mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Format and lint code
This commit is contained in:
@@ -2,10 +2,10 @@ from .autoslug_field import LocalizedAutoSlugField
|
||||
from .char_field import LocalizedCharField
|
||||
from .field import LocalizedField
|
||||
from .file_field import LocalizedFileField
|
||||
from .float_field import LocalizedFloatField
|
||||
from .integer_field import LocalizedIntegerField
|
||||
from .text_field import LocalizedTextField
|
||||
from .uniqueslug_field import LocalizedUniqueSlugField
|
||||
from .float_field import LocalizedFloatField
|
||||
|
||||
__all__ = [
|
||||
"LocalizedField",
|
||||
@@ -15,7 +15,7 @@ __all__ = [
|
||||
"LocalizedTextField",
|
||||
"LocalizedFileField",
|
||||
"LocalizedIntegerField",
|
||||
"LocalizedFloatField"
|
||||
"LocalizedFloatField",
|
||||
]
|
||||
|
||||
try:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from typing import Optional, Union, Dict
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
from django.conf import settings
|
||||
from django.db.utils import IntegrityError
|
||||
|
||||
from .field import LocalizedField
|
||||
from ..value import LocalizedValue, LocalizedFloatValue
|
||||
from ..forms import LocalizedIntegerFieldForm
|
||||
from ..value import LocalizedFloatValue, LocalizedValue
|
||||
from .field import LocalizedField
|
||||
|
||||
|
||||
class LocalizedFloatField(LocalizedField):
|
||||
@@ -27,7 +27,9 @@ class LocalizedFloatField(LocalizedField):
|
||||
|
||||
return cls._convert_localized_value(db_value)
|
||||
|
||||
def to_python(self, value: Union[Dict[str, int], int, None]) -> LocalizedFloatValue:
|
||||
def to_python(
|
||||
self, value: Union[Dict[str, int], int, None]
|
||||
) -> LocalizedFloatValue:
|
||||
"""Converts the value from a database value into a Python value."""
|
||||
|
||||
db_value = super().to_python(value)
|
||||
@@ -55,20 +57,22 @@ class LocalizedFloatField(LocalizedField):
|
||||
if local_value is not None:
|
||||
float(local_value)
|
||||
except (TypeError, ValueError):
|
||||
raise IntegrityError('non-float value in column "%s.%s" violates '
|
||||
'float constraint' % (self.name, lang_code))
|
||||
raise IntegrityError(
|
||||
'non-float value in column "%s.%s" violates '
|
||||
"float constraint" % (self.name, lang_code)
|
||||
)
|
||||
|
||||
# convert to a string before saving because the underlying
|
||||
# type is hstore, which only accept strings
|
||||
prepped_value[lang_code] = str(local_value) if local_value is not None else None
|
||||
prepped_value[lang_code] = (
|
||||
str(local_value) if local_value is not None else None
|
||||
)
|
||||
|
||||
return prepped_value
|
||||
|
||||
def formfield(self, **kwargs):
|
||||
"""Gets the form field associated with this field."""
|
||||
defaults = {
|
||||
'form_class': LocalizedIntegerFieldForm
|
||||
}
|
||||
defaults = {"form_class": LocalizedIntegerFieldForm}
|
||||
|
||||
defaults.update(kwargs)
|
||||
return super().formfield(**defaults)
|
||||
@@ -80,7 +84,7 @@ class LocalizedFloatField(LocalizedField):
|
||||
float_values = {}
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
local_value = value.get(lang_code, None)
|
||||
if local_value is None or local_value.strip() == '':
|
||||
if local_value is None or local_value.strip() == "":
|
||||
local_value = None
|
||||
|
||||
try:
|
||||
|
||||
@@ -242,10 +242,10 @@ class LocalizedNumericValue(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 ""
|
||||
|
||||
def __float__(self):
|
||||
"""Gets the value in the current language as a float"""
|
||||
"""Gets the value in the current language as a float."""
|
||||
value = self.translate()
|
||||
if value is None:
|
||||
return self.default_value
|
||||
@@ -259,28 +259,26 @@ class LocalizedIntegerValue(LocalizedNumericValue):
|
||||
default_value = None
|
||||
|
||||
def translate(self):
|
||||
"""Gets the value in the current language, or
|
||||
in the configured fallbck language."""
|
||||
"""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() == ''):
|
||||
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
|
||||
|
||||
def translate(self):
|
||||
"""
|
||||
Gets the value in the current language, or in the configured
|
||||
fallback language.
|
||||
"""
|
||||
"""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() == ''):
|
||||
if value is None or (isinstance(value, str) and value.strip() == ""):
|
||||
return None
|
||||
|
||||
return float(value)
|
||||
|
||||
Reference in New Issue
Block a user