mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 19:52:54 +03:00
Compare commits
No commits in common. "master" and "v6.8b4" have entirely different histories.
@ -28,26 +28,18 @@ class LocalizedField(HStoreField):
|
|||||||
descriptor_class = LocalizedValueDescriptor
|
descriptor_class = LocalizedValueDescriptor
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self, *args, required: Union[bool, List[str]] = None, **kwargs
|
||||||
*args,
|
|
||||||
required: Optional[Union[bool, List[str]]] = None,
|
|
||||||
blank: bool = False,
|
|
||||||
**kwargs
|
|
||||||
):
|
):
|
||||||
"""Initializes a new instance of :see:LocalizedField."""
|
"""Initializes a new instance of :see:LocalizedField."""
|
||||||
|
|
||||||
if (required is None and blank) or required is False:
|
super(LocalizedField, self).__init__(*args, required=required, **kwargs)
|
||||||
self.required = []
|
|
||||||
elif required is None and not blank:
|
|
||||||
self.required = [settings.LANGUAGE_CODE]
|
|
||||||
elif required is True:
|
|
||||||
self.required = [lang_code for lang_code, _ in settings.LANGUAGES]
|
|
||||||
else:
|
|
||||||
self.required = required
|
|
||||||
|
|
||||||
super(LocalizedField, self).__init__(
|
if (self.required is None and self.blank) or self.required is False:
|
||||||
*args, required=self.required, blank=blank, **kwargs
|
self.required = []
|
||||||
)
|
elif self.required is None and not self.blank:
|
||||||
|
self.required = [settings.LANGUAGE_CODE]
|
||||||
|
elif self.required is True:
|
||||||
|
self.required = [lang_code for lang_code, _ in settings.LANGUAGES]
|
||||||
|
|
||||||
def contribute_to_class(self, model, name, **kwargs):
|
def contribute_to_class(self, model, name, **kwargs):
|
||||||
"""Adds this field to the specifed model.
|
"""Adds this field to the specifed model.
|
||||||
|
@ -23,7 +23,6 @@ from django.db.models.lookups import (
|
|||||||
StartsWith,
|
StartsWith,
|
||||||
)
|
)
|
||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
from psqlextra.expressions import HStoreColumn
|
|
||||||
|
|
||||||
from .fields import LocalizedField
|
from .fields import LocalizedField
|
||||||
|
|
||||||
@ -38,33 +37,14 @@ except ImportError:
|
|||||||
|
|
||||||
class LocalizedLookupMixin:
|
class LocalizedLookupMixin:
|
||||||
def process_lhs(self, qn, connection):
|
def process_lhs(self, qn, connection):
|
||||||
# If the LHS is already a reference to a specific hstore key, there
|
if isinstance(self.lhs, Col):
|
||||||
# is nothing to be done since it already references as specific language.
|
language = translation.get_language() or settings.LANGUAGE_CODE
|
||||||
if isinstance(self.lhs, HStoreColumn) or isinstance(
|
self.lhs = KeyTransform(language, self.lhs)
|
||||||
self.lhs, KeyTransform
|
|
||||||
):
|
|
||||||
return super().process_lhs(qn, connection)
|
|
||||||
|
|
||||||
# If this is something custom expression, we don't really know how to
|
|
||||||
# handle that, so we better do nothing.
|
|
||||||
if not isinstance(self.lhs, Col):
|
|
||||||
return super().process_lhs(qn, connection)
|
|
||||||
|
|
||||||
# Select the key for the current language. We do this so that
|
|
||||||
#
|
|
||||||
# myfield__<lookup>=
|
|
||||||
#
|
|
||||||
# Is converted into:
|
|
||||||
#
|
|
||||||
# myfield__<lookup>__<current language>=
|
|
||||||
language = translation.get_language() or settings.LANGUAGE_CODE
|
|
||||||
self.lhs = KeyTransform(language, self.lhs)
|
|
||||||
|
|
||||||
return super().process_lhs(qn, connection)
|
return super().process_lhs(qn, connection)
|
||||||
|
|
||||||
def get_prep_lookup(self):
|
def get_prep_lookup(self):
|
||||||
# Django 4.0 removed the ability for isnull fields to be something
|
# Django 4.0 removed the ability for isnull fields to be something other than a bool
|
||||||
# other than a bool We should NOT convert them to strings
|
# We should NOT convert them to strings
|
||||||
if isinstance(self.rhs, bool):
|
if isinstance(self.rhs, bool):
|
||||||
return self.rhs
|
return self.rhs
|
||||||
return str(self.rhs)
|
return str(self.rhs)
|
||||||
|
@ -34,11 +34,10 @@
|
|||||||
|
|
||||||
.localized-fields-widget.tabs .localized-fields-widget.tab label {
|
.localized-fields-widget.tabs .localized-fields-widget.tab label {
|
||||||
padding: 5px 10px;
|
padding: 5px 10px;
|
||||||
display: inline;
|
display: inline-block;
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
color: #fff;
|
color: #fff;
|
||||||
width: initial;
|
width: initial;
|
||||||
cursor: pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.localized-fields-widget.tabs .localized-fields-widget.tab.active,
|
.localized-fields-widget.tabs .localized-fields-widget.tab.active,
|
||||||
|
@ -3,5 +3,3 @@ DJANGO_SETTINGS_MODULE=settings
|
|||||||
testpaths=tests
|
testpaths=tests
|
||||||
addopts=-m "not benchmark"
|
addopts=-m "not benchmark"
|
||||||
junit_family=legacy
|
junit_family=legacy
|
||||||
filterwarnings=
|
|
||||||
ignore::DeprecationWarning:localized_fields.fields.autoslug_field
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import django
|
|
||||||
import dj_database_url
|
import dj_database_url
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
@ -52,12 +51,6 @@ MIDDLEWARE = [
|
|||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
]
|
]
|
||||||
|
|
||||||
# See: https://github.com/psycopg/psycopg2/issues/1293
|
|
||||||
if django.VERSION >= (3, 1):
|
|
||||||
USE_TZ = True
|
|
||||||
USE_I18N = True
|
|
||||||
TIME_ZONE = 'UTC'
|
|
||||||
|
|
||||||
# set to a lower number than the default, since
|
# set to a lower number than the default, since
|
||||||
# we want the tests to be fast, default is 100
|
# we want the tests to be fast, default is 100
|
||||||
LOCALIZED_FIELDS_MAX_RETRIES = 3
|
LOCALIZED_FIELDS_MAX_RETRIES = 3
|
||||||
|
3
setup.py
3
setup.py
@ -36,7 +36,7 @@ with open(
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="django-localized-fields",
|
name="django-localized-fields",
|
||||||
version="6.8b5",
|
version="6.8b4",
|
||||||
packages=find_packages(exclude=["tests"]),
|
packages=find_packages(exclude=["tests"]),
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
license="MIT License",
|
license="MIT License",
|
||||||
@ -98,7 +98,6 @@ setup(
|
|||||||
"autopep8==1.4.4",
|
"autopep8==1.4.4",
|
||||||
"isort==4.3.20",
|
"isort==4.3.20",
|
||||||
"sl-docformatter==1.4",
|
"sl-docformatter==1.4",
|
||||||
"click==8.0.2",
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
cmdclass={
|
cmdclass={
|
||||||
|
@ -3,7 +3,6 @@ from django.conf import settings
|
|||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from django.utils import translation
|
from django.utils import translation
|
||||||
|
|
||||||
from localized_fields.expressions import LocalizedRef
|
|
||||||
from localized_fields.fields import LocalizedField
|
from localized_fields.fields import LocalizedField
|
||||||
from localized_fields.value import LocalizedValue
|
from localized_fields.value import LocalizedValue
|
||||||
|
|
||||||
@ -50,18 +49,6 @@ class LocalizedLookupsTestCase(TestCase):
|
|||||||
# ensure that hstore lookups still work
|
# ensure that hstore lookups still work
|
||||||
assert self.TestModel.objects.filter(text__ro="text_ro").exists()
|
assert self.TestModel.objects.filter(text__ro="text_ro").exists()
|
||||||
|
|
||||||
def test_localized_lookup_specific_isnull(self):
|
|
||||||
self.TestModel.objects.create(
|
|
||||||
text=LocalizedValue(dict(en="text_en", ro="text_ro", nl=None))
|
|
||||||
)
|
|
||||||
|
|
||||||
translation.activate("nl")
|
|
||||||
assert (
|
|
||||||
self.TestModel.objects.annotate(text_localized=LocalizedRef("text"))
|
|
||||||
.filter(text_localized__isnull=True)
|
|
||||||
.exists()
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class LocalizedRefLookupsTestCase(TestCase):
|
class LocalizedRefLookupsTestCase(TestCase):
|
||||||
"""Tests whether ref lookups properly work with."""
|
"""Tests whether ref lookups properly work with."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user