Passed
Push — master ( f7c14f...06873a )
by Swen
02:26
created

LocalizedTextFieldForm

Complexity

Total Complexity 0

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 2
cts 2
cp 1
wmc 0
1 1
from typing import List
0 ignored issues
show
Configuration introduced by
The import typing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
2
3 1
from django import forms
0 ignored issues
show
Configuration introduced by
The import django could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4 1
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5 1
from django.core.exceptions import ValidationError
0 ignored issues
show
Configuration introduced by
The import django.core.exceptions could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6 1
from django.forms.widgets import FILE_INPUT_CONTRADICTION
0 ignored issues
show
Configuration introduced by
The import django.forms.widgets could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7
8 1
from .value import LocalizedValue, LocalizedStringValue, \
9
    LocalizedFileValue
10 1
from .widgets import LocalizedFieldWidget, LocalizedCharFieldWidget, \
11
    LocalizedFileWidget
12
13
14 1
class LocalizedFieldForm(forms.MultiValueField):
15
    """Form for a localized field, allows editing
16
    the field in multiple languages."""
17
18 1
    widget = LocalizedFieldWidget
19 1
    field_class = forms.fields.CharField
20 1
    value_class = LocalizedValue
21
22 1
    def __init__(self, *args, **kwargs):
23
        """Initializes a new instance of :see:LocalizedFieldForm."""
24
25 1
        fields = []
26
27 1
        for lang_code, _ in settings.LANGUAGES:
28 1
            field_options = {'required': False}
29
30 1
            if lang_code == settings.LANGUAGE_CODE:
31 1
                field_options['required'] = kwargs.get('required', True)
32
33 1
            field_options['label'] = lang_code
34 1
            fields.append(self.field_class(**field_options))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
35
36 1
        super(LocalizedFieldForm, self).__init__(
37
            fields,
38
            require_all_fields=False,
39
            *args, **kwargs
40
        )
41
        # set 'required' attribute for each widget separately
42 1
        for f, w in zip(self.fields, self.widget.widgets):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldForm does not seem to have a member named fields.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Bug introduced by
The Class LocalizedFieldWidget does not seem to have a member named widgets.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
43 1
            w.is_required = f.required
44
45 1
    def compress(self, value: List[str]) -> value_class:
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'value_class'
Loading history...
46
        """Compresses the values from individual fields
47
        into a single :see:LocalizedValue instance.
48
49
        Arguments:
50
            value:
51
                The values from all the widgets.
52
53
        Returns:
54
            A :see:LocalizedValue containing all
55
            the value in several languages.
56
        """
57
58 1
        localized_value = self.value_class()
59
60 1
        for (lang_code, _), value in zip(settings.LANGUAGES, value):
61 1
            localized_value.set(lang_code, value)
62
63 1
        return localized_value
64
65
66 1
class LocalizedCharFieldForm(LocalizedFieldForm):
67
    """Form for a localized char field, allows editing
68
    the field in multiple languages."""
69
70 1
    widget = LocalizedCharFieldWidget
71 1
    value_class = LocalizedStringValue
72
73
74 1
class LocalizedTextFieldForm(LocalizedFieldForm):
75
    """Form for a localized text field, allows editing
76
    the field in multiple languages."""
77
78 1
    value_class = LocalizedStringValue
79
80
81 1
class LocalizedFileFieldForm(LocalizedFieldForm, forms.FileField):
82
    """Form for a localized file field, allows editing
83
    the field in multiple languages."""
84
85 1
    widget = LocalizedFileWidget
86 1
    field_class = forms.fields.FileField
87 1
    value_class = LocalizedFileValue
88
89 1
    def clean(self, value, initial=None):
90
        """
91
        Most part of this method is a copy of 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
92
        django.forms.MultiValueField.clean, with the exception of initial
93
        value handling (this need for correct processing FileField's).
94
        All original comments saved.
95
        """
96 1
        if initial is None:
97 1
            initial = [None for x in range(0, len(value))]
98
        else:
99 1
            if not isinstance(initial, list):
100 1
                initial = self.widget.decompress(initial)
101
102 1
        clean_data = []
103 1
        errors = []
104 1
        if not value or isinstance(value, (list, tuple)):
105 1
            if (not value or not [v for v in value if
106
                                  v not in self.empty_values]) \
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named empty_values.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
107
                    and (not initial or not [v for v in initial if
108
                                             v not in self.empty_values]):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named empty_values.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
109 1
                if self.required:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named required.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
110 1
                    raise ValidationError(self.error_messages['required'],
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named error_messages.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
111
                                          code='required')
112
        else:
113 1
            raise ValidationError(self.error_messages['invalid'],
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named error_messages.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
114
                                  code='invalid')
115 1
        for i, field in enumerate(self.fields):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named fields.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
116 1
            try:
117 1
                field_value = value[i]
118 1
            except IndexError:
119 1
                field_value = None
120 1
            try:
121 1
                field_initial = initial[i]
122 1
            except IndexError:
123 1
                field_initial = None
124 1
            if field_value in self.empty_values and \
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named empty_values.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
125
                            field_initial in self.empty_values:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named empty_values.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
126 1
                if self.require_all_fields:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named require_all_fields.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
127
                    # Raise a 'required' error if the MultiValueField is
128
                    # required and any field is empty.
129
                    if self.required:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named required.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
130
                        raise ValidationError(self.error_messages['required'],
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named error_messages.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
131
                                              code='required')
132 1
                elif field.required:
133
                    # Otherwise, add an 'incomplete' error to the list of
134
                    # collected errors and skip field cleaning, if a required
135
                    # field is empty.
136
                    if field.error_messages['incomplete'] not in errors:
137
                        errors.append(field.error_messages['incomplete'])
138
                    continue
139 1
            try:
140 1
                clean_data.append(field.clean(field_value, field_initial))
141 1
            except ValidationError as e:
142
                # Collect all validation errors in a single list, which we'll
143
                # raise at the end of clean(), rather than raising a single
144
                # exception for the first error we encounter. Skip duplicates.
145 1
                errors.extend(m for m in e.error_list if m not in errors)
146 1
        if errors:
147 1
            raise ValidationError(errors)
148
149 1
        out = self.compress(clean_data)
150 1
        self.validate(out)
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named validate.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
151 1
        self.run_validators(out)
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFileFieldForm does not seem to have a member named run_validators.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
152 1
        return out
153
154 1
    def bound_data(self, data, initial):
155 1
        bound_data = []
156 1
        if initial is None:
157 1
            initial = [None for x in range(0, len(data))]
158
        else:
159 1
            if not isinstance(initial, list):
160 1
                initial = self.widget.decompress(initial)
161 1
        for d, i in zip(data, initial):
162 1
            if d in (None, FILE_INPUT_CONTRADICTION):
163 1
                bound_data.append(i)
164
            else:
165 1
                bound_data.append(d)
166
        return bound_data
167