mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-19 06:18:57 +03:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3951266747 | ||
|
b5f4c43d6b | ||
|
3d08475468 | ||
|
b3d7092b91 | ||
|
97a785e9b0 | ||
|
97c14fd2ba | ||
|
6cb4cdf52e |
@@ -18,12 +18,6 @@ class LocalizedField(HStoreField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initializes a new instance of :see:LocalizedField."""
|
||||
|
||||
required = kwargs.get('required')
|
||||
if required is None:
|
||||
required = [settings.LANGUAGE_CODE]
|
||||
|
||||
kwargs['required'] = required
|
||||
|
||||
super(LocalizedField, self).__init__(*args, **kwargs)
|
||||
|
||||
@staticmethod
|
||||
|
@@ -2,7 +2,7 @@ from django.conf import settings
|
||||
from django.utils import translation
|
||||
|
||||
|
||||
class LocalizedValue:
|
||||
class LocalizedValue(dict):
|
||||
"""Represents the value of a :see:LocalizedField."""
|
||||
|
||||
def __init__(self, keys: dict=None):
|
||||
@@ -20,7 +20,7 @@ class LocalizedValue:
|
||||
else:
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
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:
|
||||
"""Gets the underlying value in the specified or
|
||||
@@ -37,7 +37,7 @@ class LocalizedValue:
|
||||
"""
|
||||
|
||||
language = language or settings.LANGUAGE_CODE
|
||||
return getattr(self, language, None)
|
||||
return super().get(language, None)
|
||||
|
||||
def set(self, language: str, value: str):
|
||||
"""Sets the value in the specified language.
|
||||
@@ -50,7 +50,8 @@ class LocalizedValue:
|
||||
The value to set.
|
||||
"""
|
||||
|
||||
setattr(self, language, value)
|
||||
self[language] = value
|
||||
self.__dict__.update(self)
|
||||
return self
|
||||
|
||||
def deconstruct(self) -> dict:
|
||||
@@ -85,13 +86,42 @@ class LocalizedValue:
|
||||
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:
|
||||
if self.get(lang_code) != other.get(lang_code):
|
||||
return False
|
||||
|
||||
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
|
||||
"""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,12 +1,10 @@
|
||||
from django.db import models, transaction
|
||||
from django.db.utils import IntegrityError
|
||||
from django.conf import settings
|
||||
from psqlextra.models import PostgresModel
|
||||
|
||||
from .fields import LocalizedField
|
||||
from .localized_value import LocalizedValue
|
||||
|
||||
|
||||
class LocalizedModel(models.Model):
|
||||
class LocalizedModel(PostgresModel):
|
||||
"""A model that contains localized fields."""
|
||||
|
||||
class Meta:
|
||||
|
@@ -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(
|
||||
name='django-localized-fields',
|
||||
version='3.2',
|
||||
version='3.5',
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
license='MIT License',
|
||||
@@ -18,7 +18,7 @@ setup(
|
||||
author_email='open-source@sectorlabs.ro',
|
||||
keywords=['django', 'localized', 'language', 'models', 'fields'],
|
||||
install_requires=[
|
||||
'django-postgres-extra>=1.2'
|
||||
'django-postgres-extra>=1.4'
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
|
Reference in New Issue
Block a user