mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-09-13 14:53:18 +03:00
improve functionality of required parameter
This commit is contained in:
parent
d4c24dea97
commit
aaf49614f2
@ -32,6 +32,13 @@ class LocalizedField(HStoreField):
|
|||||||
|
|
||||||
super(LocalizedField, self).__init__(*args, **kwargs)
|
super(LocalizedField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if self.required is None and self.blank:
|
||||||
|
self.required = []
|
||||||
|
elif self.required is None and not self.blank:
|
||||||
|
self.required = [settings.LANGUAGE_CODE]
|
||||||
|
elif type(self.required) == bool and self.required:
|
||||||
|
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.
|
||||||
|
|
||||||
@ -185,8 +192,8 @@ class LocalizedField(HStoreField):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def validate(self, value: LocalizedValue, *_):
|
def validate(self, value: LocalizedValue, *_):
|
||||||
"""Validates that the value for the primary language
|
"""Validates that the values has been filled in for all required
|
||||||
has been filled in.
|
languages
|
||||||
|
|
||||||
Exceptions are raises in order to notify the user
|
Exceptions are raises in order to notify the user
|
||||||
of invalid values.
|
of invalid values.
|
||||||
@ -199,17 +206,14 @@ class LocalizedField(HStoreField):
|
|||||||
if self.null:
|
if self.null:
|
||||||
return
|
return
|
||||||
|
|
||||||
primary_lang_val = getattr(value, settings.LANGUAGE_CODE)
|
for lang in self.required:
|
||||||
|
lang_val = getattr(value, settings.LANGUAGE_CODE)
|
||||||
|
|
||||||
# NOTE(seroy): use check for None, instead of `not primary_lang_val`
|
# NOTE(seroy): use check for None, instead of `not lang_val`
|
||||||
# condition, cause in this way we can not save '' value
|
# condition, cause in this way we can not save '' value
|
||||||
if primary_lang_val is None:
|
if lang_val is None:
|
||||||
raise IntegrityError(
|
raise IntegrityError('null value in column "%s.%s" violates' \
|
||||||
'null value in column "%s.%s" violates not-null constraint' % (
|
'not-null constraint' % (self.name, lang))
|
||||||
self.name,
|
|
||||||
settings.LANGUAGE_CODE
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
"""Gets the form field associated with this field."""
|
"""Gets the form field associated with this field."""
|
||||||
@ -217,18 +221,10 @@ class LocalizedField(HStoreField):
|
|||||||
defaults = {
|
defaults = {
|
||||||
'form_class': LocalizedFieldForm
|
'form_class': LocalizedFieldForm
|
||||||
}
|
}
|
||||||
|
if issubclass(kwargs.get('form_class', LocalizedFieldForm),
|
||||||
|
LocalizedFieldForm):
|
||||||
|
defaults.update({
|
||||||
|
'required_langs': self.required
|
||||||
|
})
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
return super().formfield(**defaults)
|
return super().formfield(**defaults)
|
||||||
|
|
||||||
def deconstruct(self):
|
|
||||||
"""Gets the values to pass to :see:__init__ when
|
|
||||||
re-creating this object."""
|
|
||||||
|
|
||||||
name, path, args, kwargs = super(
|
|
||||||
LocalizedField, self).deconstruct()
|
|
||||||
|
|
||||||
if self.uniqueness:
|
|
||||||
kwargs['uniqueness'] = self.uniqueness
|
|
||||||
|
|
||||||
return name, path, args, kwargs
|
|
||||||
|
@ -146,8 +146,6 @@ class LocalizedFileField(LocalizedField):
|
|||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': LocalizedFileFieldForm}
|
defaults = {'form_class': LocalizedFileFieldForm}
|
||||||
if 'initial' in kwargs:
|
|
||||||
defaults['required'] = False
|
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
return super().formfield(**defaults)
|
return super().formfield(**defaults)
|
||||||
|
|
||||||
|
@ -19,16 +19,13 @@ class LocalizedFieldForm(forms.MultiValueField):
|
|||||||
field_class = forms.fields.CharField
|
field_class = forms.fields.CharField
|
||||||
value_class = LocalizedValue
|
value_class = LocalizedValue
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, required_langs: List[str]=[], **kwargs):
|
||||||
"""Initializes a new instance of :see:LocalizedFieldForm."""
|
"""Initializes a new instance of :see:LocalizedFieldForm."""
|
||||||
|
|
||||||
fields = []
|
fields = []
|
||||||
|
|
||||||
for lang_code, _ in settings.LANGUAGES:
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
field_options = {'required': False}
|
field_options = {'required': lang_code in required_langs}
|
||||||
|
|
||||||
if lang_code == settings.LANGUAGE_CODE:
|
|
||||||
field_options['required'] = kwargs.get('required', True)
|
|
||||||
|
|
||||||
field_options['label'] = lang_code
|
field_options['label'] = lang_code
|
||||||
fields.append(self.field_class(**field_options))
|
fields.append(self.field_class(**field_options))
|
||||||
|
@ -12,7 +12,7 @@ class LocalizedFieldFormTestCase(TestCase):
|
|||||||
"""Tests whether the constructor correctly
|
"""Tests whether the constructor correctly
|
||||||
creates a field for every language."""
|
creates a field for every language."""
|
||||||
|
|
||||||
form = LocalizedFieldForm()
|
form = LocalizedFieldForm(required_langs=[settings.LANGUAGE_CODE])
|
||||||
|
|
||||||
for (lang_code, _), field in zip(settings.LANGUAGES, form.fields):
|
for (lang_code, _), field in zip(settings.LANGUAGES, form.fields):
|
||||||
assert field.label == lang_code
|
assert field.label == lang_code
|
||||||
|
Loading…
x
Reference in New Issue
Block a user