mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-30 02:28:57 +03:00
Move LocalizedValue and add get_language_codes
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
from .localized_field import LocalizedField
|
||||
from .localized_value import LocalizedValue
|
||||
from .localized_autoslug_field import LocalizedAutoSlugField
|
||||
from .localized_bleach_field import LocalizedBleachField
|
||||
|
||||
|
||||
__all__ = [
|
||||
'LocalizedField',
|
||||
'LocalizedValue',
|
||||
'LocalizedAutoSlugField',
|
||||
'LocalizedBleachField'
|
||||
]
|
||||
|
||||
@@ -5,7 +5,7 @@ from django.conf import settings
|
||||
from django.utils.text import slugify
|
||||
|
||||
from .localized_field import LocalizedField
|
||||
from .localized_value import LocalizedValue
|
||||
from ..localized_value import LocalizedValue
|
||||
|
||||
|
||||
class LocalizedAutoSlugField(LocalizedField):
|
||||
@@ -17,7 +17,11 @@ class LocalizedAutoSlugField(LocalizedField):
|
||||
"""Initializes a new instance of :see:LocalizedAutoSlugField."""
|
||||
|
||||
self.populate_from = kwargs.pop('populate_from', None)
|
||||
super(LocalizedAutoSlugField, self).__init__(*args, **kwargs)
|
||||
|
||||
super(LocalizedAutoSlugField, self).__init__(
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def deconstruct(self):
|
||||
"""Deconstructs the field into something the database
|
||||
|
||||
@@ -2,8 +2,8 @@ from django.conf import settings
|
||||
from django.contrib.postgres.fields import HStoreField
|
||||
from django.db.utils import IntegrityError
|
||||
|
||||
from ..forms import LocalizedFieldForm
|
||||
from .localized_value import LocalizedValue
|
||||
from localized_fields import LocalizedFieldForm
|
||||
from ..localized_value import LocalizedValue
|
||||
|
||||
|
||||
class LocalizedField(HStoreField):
|
||||
@@ -164,9 +164,10 @@ class LocalizedField(HStoreField):
|
||||
"""Gets the values to pass to :see:__init__ when
|
||||
re-creating this object."""
|
||||
|
||||
values = super(LocalizedField, self).deconstruct()
|
||||
values[3].update({
|
||||
'uniqueness': self.uniqueness
|
||||
})
|
||||
name, path, args, kwargs = super(
|
||||
LocalizedField, self).deconstruct()
|
||||
|
||||
return values
|
||||
if self.uniqueness:
|
||||
kwargs['uniqueness'] = self.uniqueness
|
||||
|
||||
return name, path, args, kwargs
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
from django.conf import settings
|
||||
from django.utils import translation
|
||||
|
||||
|
||||
class LocalizedValue:
|
||||
"""Represents the value of a :see:LocalizedField."""
|
||||
|
||||
def __init__(self, keys: dict=None):
|
||||
"""Initializes a new instance of :see:LocalizedValue.
|
||||
|
||||
Arguments:
|
||||
keys:
|
||||
The keys to initialize this value with. Every
|
||||
key contains the value of this field in a
|
||||
different language.
|
||||
"""
|
||||
|
||||
if isinstance(keys, str):
|
||||
setattr(self, settings.LANGUAGE_CODE, keys)
|
||||
else:
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
value = keys.get(lang_code) if keys else None
|
||||
setattr(self, lang_code, value)
|
||||
|
||||
def get(self, language: str=None) -> str:
|
||||
"""Gets the underlying value in the specified or
|
||||
primary language.
|
||||
|
||||
Arguments:
|
||||
language:
|
||||
The language to get the value in.
|
||||
|
||||
Returns:
|
||||
The value in the current language, or
|
||||
the primary language in case no language
|
||||
was specified.
|
||||
"""
|
||||
|
||||
language = language or settings.LANGUAGE_CODE
|
||||
return getattr(self, language, None)
|
||||
|
||||
def set(self, language: str, value: str):
|
||||
"""Sets the value in the specified language.
|
||||
|
||||
Arguments:
|
||||
language:
|
||||
The language to set the value in.
|
||||
|
||||
value:
|
||||
The value to set.
|
||||
"""
|
||||
|
||||
setattr(self, language, value)
|
||||
return self
|
||||
|
||||
def deconstruct(self) -> dict:
|
||||
"""Deconstructs this value into a primitive type.
|
||||
|
||||
Returns:
|
||||
A dictionary with all the localized values
|
||||
contained in this instance.
|
||||
"""
|
||||
|
||||
path = 'localized_fields.fields.LocalizedValue'
|
||||
return path, [self.__dict__], {}
|
||||
|
||||
def __str__(self) -> str:
|
||||
"""Gets the value in the current language, or falls
|
||||
back to the primary language if there's no value
|
||||
in the current language."""
|
||||
|
||||
value = self.get(translation.get_language())
|
||||
|
||||
if not value:
|
||||
value = self.get(settings.LANGUAGE_CODE)
|
||||
|
||||
return value or ''
|
||||
|
||||
def __repr__(self): # pragma: no cover
|
||||
"""Gets a textual representation of this object."""
|
||||
|
||||
return 'LocalizedValue<%s> 0x%s' % (self.__dict__, id(self))
|
||||
Reference in New Issue
Block a user