mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-12-14 07:42:24 +03:00
Re-format all files
This commit is contained in:
@@ -5,30 +5,41 @@ from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms.widgets import FILE_INPUT_CONTRADICTION
|
||||
|
||||
from .value import LocalizedValue, LocalizedStringValue, \
|
||||
LocalizedFileValue, LocalizedIntegerValue
|
||||
from .widgets import LocalizedFieldWidget, LocalizedCharFieldWidget, \
|
||||
LocalizedFileWidget, AdminLocalizedIntegerFieldWidget
|
||||
from .value import (
|
||||
LocalizedFileValue,
|
||||
LocalizedIntegerValue,
|
||||
LocalizedStringValue,
|
||||
LocalizedValue,
|
||||
)
|
||||
from .widgets import (
|
||||
AdminLocalizedIntegerFieldWidget,
|
||||
LocalizedCharFieldWidget,
|
||||
LocalizedFieldWidget,
|
||||
LocalizedFileWidget,
|
||||
)
|
||||
|
||||
|
||||
class LocalizedFieldForm(forms.MultiValueField):
|
||||
"""Form for a localized field, allows editing
|
||||
the field in multiple languages."""
|
||||
"""Form for a localized field, allows editing the field in multiple
|
||||
languages."""
|
||||
|
||||
widget = LocalizedFieldWidget
|
||||
field_class = forms.fields.CharField
|
||||
value_class = LocalizedValue
|
||||
|
||||
def __init__(self, *args, required: Union[bool, List[str]]=False, **kwargs):
|
||||
def __init__(
|
||||
self, *args, required: Union[bool, List[str]] = False, **kwargs
|
||||
):
|
||||
"""Initializes a new instance of :see:LocalizedFieldForm."""
|
||||
|
||||
fields = []
|
||||
|
||||
for lang_code, _ in settings.LANGUAGES:
|
||||
field_options = dict(
|
||||
required=required if type(required) is bool else (lang_code in
|
||||
required),
|
||||
label=lang_code
|
||||
required=required
|
||||
if type(required) is bool
|
||||
else (lang_code in required),
|
||||
label=lang_code,
|
||||
)
|
||||
fields.append(self.field_class(**field_options))
|
||||
|
||||
@@ -36,7 +47,8 @@ class LocalizedFieldForm(forms.MultiValueField):
|
||||
fields,
|
||||
required=required if type(required) is bool else True,
|
||||
require_all_fields=False,
|
||||
*args, **kwargs
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# set 'required' attribute for each widget separately
|
||||
@@ -44,8 +56,8 @@ class LocalizedFieldForm(forms.MultiValueField):
|
||||
widget.is_required = field.required
|
||||
|
||||
def compress(self, value: List[str]) -> value_class:
|
||||
"""Compresses the values from individual fields
|
||||
into a single :see:LocalizedValue instance.
|
||||
"""Compresses the values from individual fields into a single
|
||||
:see:LocalizedValue instance.
|
||||
|
||||
Arguments:
|
||||
value:
|
||||
@@ -65,41 +77,41 @@ class LocalizedFieldForm(forms.MultiValueField):
|
||||
|
||||
|
||||
class LocalizedCharFieldForm(LocalizedFieldForm):
|
||||
"""Form for a localized char field, allows editing
|
||||
the field in multiple languages."""
|
||||
"""Form for a localized char field, allows editing the field in multiple
|
||||
languages."""
|
||||
|
||||
widget = LocalizedCharFieldWidget
|
||||
value_class = LocalizedStringValue
|
||||
|
||||
|
||||
class LocalizedTextFieldForm(LocalizedFieldForm):
|
||||
"""Form for a localized text field, allows editing
|
||||
the field in multiple languages."""
|
||||
"""Form for a localized text field, allows editing the field in multiple
|
||||
languages."""
|
||||
|
||||
value_class = LocalizedStringValue
|
||||
|
||||
|
||||
class LocalizedIntegerFieldForm(LocalizedFieldForm):
|
||||
"""Form for a localized integer field, allows editing
|
||||
the field in multiple languages."""
|
||||
"""Form for a localized integer field, allows editing the field in multiple
|
||||
languages."""
|
||||
|
||||
widget = AdminLocalizedIntegerFieldWidget
|
||||
value_class = LocalizedIntegerValue
|
||||
|
||||
|
||||
class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
|
||||
"""Form for a localized file field, allows editing
|
||||
the field in multiple languages."""
|
||||
"""Form for a localized file field, allows editing the field in multiple
|
||||
languages."""
|
||||
|
||||
widget = LocalizedFileWidget
|
||||
field_class = forms.fields.FileField
|
||||
value_class = LocalizedFileValue
|
||||
|
||||
def clean(self, value, initial=None):
|
||||
"""
|
||||
Most part of this method is a copy of
|
||||
django.forms.MultiValueField.clean, with the exception of initial
|
||||
value handling (this need for correct processing FileField's).
|
||||
"""Most part of this method is a copy of
|
||||
django.forms.MultiValueField.clean, with the exception of initial value
|
||||
handling (this need for correct processing FileField's).
|
||||
|
||||
All original comments saved.
|
||||
"""
|
||||
if initial is None:
|
||||
@@ -111,16 +123,21 @@ class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
|
||||
clean_data = []
|
||||
errors = []
|
||||
if not value or isinstance(value, (list, tuple)):
|
||||
if (not value or not [v for v in value if
|
||||
v not in self.empty_values]) \
|
||||
and (not initial or not [v for v in initial if
|
||||
v not in self.empty_values]):
|
||||
if (
|
||||
not value
|
||||
or not [v for v in value if v not in self.empty_values]
|
||||
) and (
|
||||
not initial
|
||||
or not [v for v in initial if v not in self.empty_values]
|
||||
):
|
||||
if self.required:
|
||||
raise ValidationError(self.error_messages['required'],
|
||||
code='required')
|
||||
raise ValidationError(
|
||||
self.error_messages["required"], code="required"
|
||||
)
|
||||
else:
|
||||
raise ValidationError(self.error_messages['invalid'],
|
||||
code='invalid')
|
||||
raise ValidationError(
|
||||
self.error_messages["invalid"], code="invalid"
|
||||
)
|
||||
for i, field in enumerate(self.fields):
|
||||
try:
|
||||
field_value = value[i]
|
||||
@@ -131,20 +148,23 @@ class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
|
||||
except IndexError:
|
||||
field_initial = None
|
||||
|
||||
if field_value in self.empty_values and \
|
||||
field_initial in self.empty_values:
|
||||
if (
|
||||
field_value in self.empty_values
|
||||
and field_initial in self.empty_values
|
||||
):
|
||||
if self.require_all_fields:
|
||||
# Raise a 'required' error if the MultiValueField is
|
||||
# required and any field is empty.
|
||||
if self.required:
|
||||
raise ValidationError(self.error_messages['required'],
|
||||
code='required')
|
||||
raise ValidationError(
|
||||
self.error_messages["required"], code="required"
|
||||
)
|
||||
elif field.required:
|
||||
# Otherwise, add an 'incomplete' error to the list of
|
||||
# collected errors and skip field cleaning, if a required
|
||||
# field is empty.
|
||||
if field.error_messages['incomplete'] not in errors:
|
||||
errors.append(field.error_messages['incomplete'])
|
||||
if field.error_messages["incomplete"] not in errors:
|
||||
errors.append(field.error_messages["incomplete"])
|
||||
continue
|
||||
try:
|
||||
clean_data.append(field.clean(field_value, field_initial))
|
||||
|
||||
Reference in New Issue
Block a user