|
1
|
1 |
|
from typing import List |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
1 |
|
from django import forms |
|
|
|
|
|
|
4
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
5
|
1 |
|
from django.core.exceptions import ValidationError |
|
|
|
|
|
|
6
|
1 |
|
from django.forms.widgets import FILE_INPUT_CONTRADICTION |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
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): |
|
|
|
|
|
|
43
|
1 |
|
w.is_required = f.required |
|
44
|
|
|
|
|
45
|
1 |
|
def compress(self, value: List[str]) -> value_class: |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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]) \ |
|
|
|
|
|
|
107
|
|
|
and (not initial or not [v for v in initial if |
|
108
|
|
|
v not in self.empty_values]): |
|
|
|
|
|
|
109
|
1 |
|
if self.required: |
|
|
|
|
|
|
110
|
1 |
|
raise ValidationError(self.error_messages['required'], |
|
|
|
|
|
|
111
|
|
|
code='required') |
|
112
|
|
|
else: |
|
113
|
1 |
|
raise ValidationError(self.error_messages['invalid'], |
|
|
|
|
|
|
114
|
|
|
code='invalid') |
|
115
|
1 |
|
for i, field in enumerate(self.fields): |
|
|
|
|
|
|
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 \ |
|
|
|
|
|
|
125
|
|
|
field_initial in self.empty_values: |
|
|
|
|
|
|
126
|
1 |
|
if self.require_all_fields: |
|
|
|
|
|
|
127
|
|
|
# Raise a 'required' error if the MultiValueField is |
|
128
|
|
|
# required and any field is empty. |
|
129
|
|
|
if self.required: |
|
|
|
|
|
|
130
|
|
|
raise ValidationError(self.error_messages['required'], |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
151
|
1 |
|
self.run_validators(out) |
|
|
|
|
|
|
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
|
|
|
|
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.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.