# Path: localized_fields/forms.py
# class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
#     """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).
# 
#         All original comments saved.
#         """
#         if initial is None:
#             initial = [None for x in range(0, len(value))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.widget.decompress(initial)
# 
#         clean_data = []
#         errors = []
#         if not value or isinstance(value, (list, tuple)):
#             is_empty = [v for v in value if v not in self.empty_values]
#             if (not value or not is_empty) and (not initial or not is_empty):
#                 if self.required:
#                     raise ValidationError(
#                         self.error_messages["required"], code="required"
#                     )
#         else:
#             raise ValidationError(
#                 self.error_messages["invalid"], code="invalid"
#             )
#         for i, field in enumerate(self.fields):
#             try:
#                 field_value = value[i]
#             except IndexError:
#                 field_value = None
# 
#             try:
#                 field_initial = initial[i]
#             except IndexError:
#                 field_initial = None
# 
#             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"
#                         )
#                 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"])
#                     continue
#             try:
#                 clean_data.append(field.clean(field_value, field_initial))
#             except ValidationError as e:
#                 # Collect all validation errors in a single list, which we'll
#                 # raise at the end of clean(), rather than raising a single
#                 # exception for the first error we encounter. Skip duplicates.
#                 errors.extend(m for m in e.error_list if m not in errors)
#         if errors:
#             raise ValidationError(errors)
# 
#         out = self.compress(clean_data)
#         self.validate(out)
#         self.run_validators(out)
#         return out
# 
#     def bound_data(self, data, initial):
#         bound_data = []
#         if initial is None:
#             initial = [None for x in range(0, len(data))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.widget.decompress(initial)
#         for d, i in zip(data, initial):
#             if d in (None, FILE_INPUT_CONTRADICTION):
#                 bound_data.append(i)
#             else:
#                 bound_data.append(d)
#         return bound_data


from django.conf import settings
from django.core.exceptions import ValidationError
from django.forms.widgets import FILE_INPUT_CONTRADICTION
from django.test import TestCase
from localized_fields.forms import LocalizedFileFieldForm



class LocalizedFileFieldFormTestCase(TestCase):
    """Tests the workings of the :see:LocalizedFileFieldForm class."""

    def test_clean(self):
        """Tests whether the :see:clean function is working properly."""

        formfield = LocalizedFileFieldForm(required=True)
        with self.assertRaises(ValidationError):
            formfield.clean([])
        with self.assertRaises(ValidationError):
            formfield.clean([], {"en": None})
        with self.assertRaises(ValidationError):
            formfield.clean("badvalue")
        with self.assertRaises(ValidationError):
            value = [FILE_INPUT_CONTRADICTION] * len(settings.LANGUAGES)
            formfield.clean(value)

        formfield = LocalizedFileFieldForm(required=False)
        formfield.clean([""] * len(settings.LANGUAGES))
        formfield.clean(["", ""], ["", ""])

    def test_bound_data(self):
        """Tests whether the :see:bound_data function is returns correctly
        value."""


# Path: localized_fields/forms.py
# class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
#     """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).
# 
#         All original comments saved.
#         """
#         if initial is None:
#             initial = [None for x in range(0, len(value))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.widget.decompress(initial)
# 
#         clean_data = []
#         errors = []
#         if not value or isinstance(value, (list, tuple)):
#             is_empty = [v for v in value if v not in self.empty_values]
#             if (not value or not is_empty) and (not initial or not is_empty):
#                 if self.required:
#                     raise ValidationError(
#                         self.error_messages["required"], code="required"
#                     )
#         else:
#             raise ValidationError(
#                 self.error_messages["invalid"], code="invalid"
#             )
#         for i, field in enumerate(self.fields):
#             try:
#                 field_value = value[i]
#             except IndexError:
#                 field_value = None
# 
#             try:
#                 field_initial = initial[i]
#             except IndexError:
#                 field_initial = None
# 
#             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"
#                         )
#                 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"])
#                     continue
#             try:
#                 clean_data.append(field.clean(field_value, field_initial))
#             except ValidationError as e:
#                 # Collect all validation errors in a single list, which we'll
#                 # raise at the end of clean(), rather than raising a single
#                 # exception for the first error we encounter. Skip duplicates.
#                 errors.extend(m for m in e.error_list if m not in errors)
#         if errors:
#             raise ValidationError(errors)
# 
#         out = self.compress(clean_data)
#         self.validate(out)
#         self.run_validators(out)
#         return out
# 
#     def bound_data(self, data, initial):
#         bound_data = []
#         if initial is None:
#             initial = [None for x in range(0, len(data))]
#         else:
#             if not isinstance(initial, list):
#                 initial = self.widget.decompress(initial)
#         for d, i in zip(data, initial):
#             if d in (None, FILE_INPUT_CONTRADICTION):
#                 bound_data.append(i)
#             else:
#                 bound_data.append(d)
#         return bound_data


# Path: tests/test_file_field_form.py
from django.conf import settings
from django.core.exceptions import ValidationError
from django.forms.widgets import FILE_INPUT_CONTRADICTION
from django.test import TestCase
from localized_fields.forms import LocalizedFileFieldForm




class LocalizedFileFieldFormTestCase(TestCase):
    """Tests the workings of the :see:LocalizedFileFieldForm class."""

    def test_clean(self):
        """Tests whether the :see:clean function is working properly."""

        formfield = LocalizedFileFieldForm(required=True)
        with self.assertRaises(ValidationError):
            formfield.clean([])
        with self.assertRaises(ValidationError):
            formfield.clean([], {"en": None})
        with self.assertRaises(ValidationError):
            formfield.clean("badvalue")
        with self.assertRaises(ValidationError):
            value = [FILE_INPUT_CONTRADICTION] * len(settings.LANGUAGES)
            formfield.clean(value)

        formfield = LocalizedFileFieldForm(required=False)
        formfield.clean([""] * len(settings.LANGUAGES))
        formfield.clean(["", ""], ["", ""])

    def test_bound_data(self):
        """Tests whether the :see:bound_data function is returns correctly
        value."""


        formfield = LocalizedFileFieldForm()