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