7 Commits
v3.2 ... v3.5

Author SHA1 Message Date
Swen Kooij
3951266747 Bump version to 3.5 2017-03-09 14:32:48 +02:00
Swen Kooij
b5f4c43d6b LocalizedValue is now cast-able to dict 2017-03-09 14:32:33 +02:00
Swen Kooij
3d08475468 __eq__ should only compare same types
unless it's a string
2017-03-09 11:59:21 +02:00
Swen Kooij
b3d7092b91 Bump version to 3.4 2017-02-28 15:00:13 +02:00
Swen Kooij
97a785e9b0 Upgrade django-postgres-extra to 1.4 2017-02-28 14:59:58 +02:00
Swen Kooij
97c14fd2ba Bump version to 3.3 2017-02-24 14:35:57 +02:00
Swen Kooij
6cb4cdf52e Now inheriting from PostgresModel for upserts 2017-02-24 14:35:29 +02:00
5 changed files with 40 additions and 18 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -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:

View File

@@ -1 +1 @@
django-postgres-extra==1.2
django-postgres-extra==1.4

View File

@@ -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',