mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-24 08:18:58 +03:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3951266747 | ||
|
|
b5f4c43d6b | ||
|
|
3d08475468 | ||
|
|
b3d7092b91 | ||
|
|
97a785e9b0 |
@@ -2,7 +2,7 @@ from django.conf import settings
|
|||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
|
|
||||||
|
|
||||||
class LocalizedValue:
|
class LocalizedValue(dict):
|
||||||
"""Represents the value of a :see:LocalizedField."""
|
"""Represents the value of a :see:LocalizedField."""
|
||||||
|
|
||||||
def __init__(self, keys: dict=None):
|
def __init__(self, keys: dict=None):
|
||||||
@@ -20,7 +20,7 @@ class LocalizedValue:
|
|||||||
else:
|
else:
|
||||||
for lang_code, _ in settings.LANGUAGES:
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
value = keys.get(lang_code) if keys else None
|
value = keys.get(lang_code) if keys else None
|
||||||
setattr(self, lang_code, value)
|
self.set(lang_code, value)
|
||||||
|
|
||||||
def get(self, language: str=None) -> str:
|
def get(self, language: str=None) -> str:
|
||||||
"""Gets the underlying value in the specified or
|
"""Gets the underlying value in the specified or
|
||||||
@@ -37,7 +37,7 @@ class LocalizedValue:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
language = language or settings.LANGUAGE_CODE
|
language = language or settings.LANGUAGE_CODE
|
||||||
return getattr(self, language, None)
|
return super().get(language, None)
|
||||||
|
|
||||||
def set(self, language: str, value: str):
|
def set(self, language: str, value: str):
|
||||||
"""Sets the value in the specified language.
|
"""Sets the value in the specified language.
|
||||||
@@ -50,7 +50,8 @@ class LocalizedValue:
|
|||||||
The value to set.
|
The value to set.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
setattr(self, language, value)
|
self[language] = value
|
||||||
|
self.__dict__.update(self)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def deconstruct(self) -> dict:
|
def deconstruct(self) -> dict:
|
||||||
@@ -85,13 +86,42 @@ class LocalizedValue:
|
|||||||
And False when they are not.
|
And False when they are not.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if not isinstance(other, type(self)):
|
||||||
|
if isinstance(other, str):
|
||||||
|
return self.__str__() == other
|
||||||
|
return False
|
||||||
|
|
||||||
for lang_code, _ in settings.LANGUAGES:
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
if self.get(lang_code) != other.get(lang_code):
|
if self.get(lang_code) != other.get(lang_code):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def __ne__(self, other):
|
||||||
|
"""Compares :paramref:self to :paramerf:other for
|
||||||
|
in-equality.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
True when :paramref:self is not equal to :paramref:other.
|
||||||
|
And False when they are.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return not self.__eq__(other)
|
||||||
|
|
||||||
|
def __setattr__(self, language: str, value: str):
|
||||||
|
"""Sets the value for a language with the specified name.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
language:
|
||||||
|
The language to set the value in.
|
||||||
|
|
||||||
|
value:
|
||||||
|
The value to set.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.set(language, value)
|
||||||
|
|
||||||
def __repr__(self): # pragma: no cover
|
def __repr__(self): # pragma: no cover
|
||||||
"""Gets a textual representation of this object."""
|
"""Gets a textual representation of this object."""
|
||||||
|
|
||||||
return 'LocalizedValue<%s> 0x%s' % (self.__dict__, id(self))
|
return 'LocalizedValue<%s> 0x%s' % (dict(self), id(self))
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
django-postgres-extra==1.2
|
django-postgres-extra==1.4
|
||||||
|
|||||||
4
setup.py
4
setup.py
@@ -7,7 +7,7 @@ with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='django-localized-fields',
|
name='django-localized-fields',
|
||||||
version='3.3',
|
version='3.5',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
license='MIT License',
|
license='MIT License',
|
||||||
@@ -18,7 +18,7 @@ setup(
|
|||||||
author_email='open-source@sectorlabs.ro',
|
author_email='open-source@sectorlabs.ro',
|
||||||
keywords=['django', 'localized', 'language', 'models', 'fields'],
|
keywords=['django', 'localized', 'language', 'models', 'fields'],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'django-postgres-extra>=1.3'
|
'django-postgres-extra>=1.4'
|
||||||
],
|
],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Environment :: Web Environment',
|
'Environment :: Web Environment',
|
||||||
|
|||||||
Reference in New Issue
Block a user