mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-09-13 14:53:18 +03:00
Refactor required_langs into required
This commit is contained in:
parent
7d629c186d
commit
c4bf151938
@ -1,6 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union, List
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.utils import IntegrityError
|
from django.db.utils import IntegrityError
|
||||||
@ -27,12 +27,12 @@ class LocalizedField(HStoreField):
|
|||||||
# The descriptor to use for accessing the attribute off of the class.
|
# The descriptor to use for accessing the attribute off of the class.
|
||||||
descriptor_class = LocalizedValueDescriptor
|
descriptor_class = LocalizedValueDescriptor
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, required: Union[bool, List[str]]=None, **kwargs):
|
||||||
"""Initializes a new instance of :see:LocalizedField."""
|
"""Initializes a new instance of :see:LocalizedField."""
|
||||||
|
|
||||||
super(LocalizedField, self).__init__(*args, **kwargs)
|
super(LocalizedField, self).__init__(*args, required=required, **kwargs)
|
||||||
|
|
||||||
if self.required is None and self.blank:
|
if (self.required is None and self.blank) or self.required is False:
|
||||||
self.required = []
|
self.required = []
|
||||||
elif self.required is None and not self.blank:
|
elif self.required is None and not self.blank:
|
||||||
self.required = [settings.LANGUAGE_CODE]
|
self.required = [settings.LANGUAGE_CODE]
|
||||||
@ -213,11 +213,9 @@ class LocalizedField(HStoreField):
|
|||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
"""Gets the form field associated with this field."""
|
"""Gets the form field associated with this field."""
|
||||||
|
|
||||||
defaults = dict(form_class=LocalizedFieldForm)
|
defaults = dict(
|
||||||
|
form_class=LocalizedFieldForm,
|
||||||
form_class = kwargs.get('form_class', LocalizedFieldForm)
|
required=False if self.blank else self.required
|
||||||
if issubclass(form_class, LocalizedFieldForm):
|
)
|
||||||
defaults.update(dict(required_langs=self.required))
|
|
||||||
|
|
||||||
defaults.update(kwargs)
|
defaults.update(kwargs)
|
||||||
return super().formfield(**defaults)
|
return super().formfield(**defaults)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from typing import List
|
from typing import List, Union
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -19,21 +19,22 @@ class LocalizedFieldForm(forms.MultiValueField):
|
|||||||
field_class = forms.fields.CharField
|
field_class = forms.fields.CharField
|
||||||
value_class = LocalizedValue
|
value_class = LocalizedValue
|
||||||
|
|
||||||
def __init__(self, *args, required_langs: List[str]=[], **kwargs):
|
def __init__(self, *args, required: Union[bool, List[str]]=False, **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 = dict(
|
field_options = dict(
|
||||||
required=lang_code in required_langs,
|
required=required if type(required) is bool else (lang_code in
|
||||||
|
required),
|
||||||
label=lang_code
|
label=lang_code
|
||||||
)
|
)
|
||||||
|
|
||||||
fields.append(self.field_class(**field_options))
|
fields.append(self.field_class(**field_options))
|
||||||
|
|
||||||
super(LocalizedFieldForm, self).__init__(
|
super(LocalizedFieldForm, self).__init__(
|
||||||
fields,
|
fields,
|
||||||
|
required=required if type(required) is bool else True,
|
||||||
require_all_fields=False,
|
require_all_fields=False,
|
||||||
*args, **kwargs
|
*args, **kwargs
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user