|
1
|
1 |
|
from typing import List |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
4
|
1 |
|
from django import forms |
|
|
|
|
|
|
5
|
1 |
|
from django.contrib.admin import widgets |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
1 |
|
from .value import LocalizedValue |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
class LocalizedFieldWidget(forms.MultiWidget): |
|
11
|
|
|
"""Widget that has an input box for every language.""" |
|
12
|
1 |
|
template_name = 'localized_fields/multiwidget.html' |
|
13
|
1 |
|
widget = forms.Textarea |
|
14
|
|
|
|
|
15
|
1 |
|
def __init__(self, *args, **kwargs): |
|
16
|
|
|
"""Initializes a new instance of :see:LocalizedFieldWidget.""" |
|
17
|
|
|
|
|
18
|
1 |
|
initial_widgets = [ |
|
19
|
|
|
self.widget |
|
20
|
|
|
for _ in settings.LANGUAGES |
|
21
|
|
|
] |
|
22
|
|
|
|
|
23
|
1 |
|
super().__init__(initial_widgets, *args, **kwargs) |
|
24
|
|
|
|
|
25
|
1 |
|
for ((lang_code, lang_name), widget) in zip(settings.LANGUAGES, self.widgets): |
|
|
|
|
|
|
26
|
1 |
|
widget.attrs['lang'] = lang_code |
|
27
|
1 |
|
widget.lang_code = lang_code |
|
28
|
1 |
|
widget.lang_name = lang_name |
|
29
|
|
|
|
|
30
|
1 |
|
def decompress(self, value: LocalizedValue) -> List[str]: |
|
|
|
|
|
|
31
|
|
|
"""Decompresses the specified value so |
|
32
|
|
|
it can be spread over the internal widgets. |
|
33
|
|
|
|
|
34
|
|
|
Arguments: |
|
35
|
|
|
value: |
|
36
|
|
|
The :see:LocalizedValue to display in this |
|
37
|
|
|
widget. |
|
38
|
|
|
|
|
39
|
|
|
Returns: |
|
40
|
|
|
All values to display in the inner widgets. |
|
41
|
|
|
""" |
|
42
|
|
|
|
|
43
|
1 |
|
result = [] |
|
44
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
45
|
1 |
|
if value: |
|
46
|
1 |
|
result.append(value.get(lang_code)) |
|
47
|
|
|
else: |
|
48
|
1 |
|
result.append(None) |
|
49
|
|
|
|
|
50
|
1 |
|
return result |
|
51
|
|
|
|
|
52
|
1 |
|
def get_context(self, name, value, attrs): |
|
53
|
1 |
|
context = super(forms.MultiWidget, self).get_context(name, value, attrs) |
|
54
|
1 |
|
if self.is_localized: |
|
|
|
|
|
|
55
|
|
|
for widget in self.widgets: |
|
|
|
|
|
|
56
|
|
|
widget.is_localized = self.is_localized |
|
|
|
|
|
|
57
|
|
|
# value is a list of values, each corresponding to a widget |
|
58
|
|
|
# in self.widgets. |
|
59
|
1 |
|
if not isinstance(value, list): |
|
60
|
1 |
|
value = self.decompress(value) |
|
61
|
|
|
|
|
62
|
1 |
|
final_attrs = context['widget']['attrs'] |
|
63
|
1 |
|
input_type = final_attrs.pop('type', None) |
|
64
|
1 |
|
id_ = final_attrs.get('id') |
|
65
|
1 |
|
subwidgets = [] |
|
66
|
1 |
|
for i, widget in enumerate(self.widgets): |
|
|
|
|
|
|
67
|
1 |
|
if input_type is not None: |
|
68
|
|
|
widget.input_type = input_type |
|
69
|
1 |
|
widget_name = '%s_%s' % (name, i) |
|
70
|
1 |
|
try: |
|
71
|
1 |
|
widget_value = value[i] |
|
72
|
|
|
except IndexError: |
|
73
|
|
|
widget_value = None |
|
74
|
1 |
|
if id_: |
|
75
|
|
|
widget_attrs = final_attrs.copy() |
|
76
|
|
|
widget_attrs['id'] = '%s_%s' % (id_, i) |
|
77
|
|
|
else: |
|
78
|
1 |
|
widget_attrs = final_attrs |
|
79
|
1 |
|
widget_attrs = self.build_widget_attrs(widget, widget_value, widget_attrs) |
|
80
|
1 |
|
widget_context = widget.get_context(widget_name, widget_value, widget_attrs)['widget'] |
|
81
|
1 |
|
widget_context.update(dict( |
|
82
|
|
|
lang_code=widget.lang_code, |
|
83
|
|
|
lang_name=widget.lang_name |
|
84
|
|
|
)) |
|
85
|
1 |
|
subwidgets.append(widget_context) |
|
86
|
1 |
|
context['widget']['subwidgets'] = subwidgets |
|
87
|
1 |
|
return context |
|
88
|
|
|
|
|
89
|
1 |
|
@staticmethod |
|
90
|
|
|
def build_widget_attrs(widget, value, attrs): |
|
91
|
1 |
|
attrs = dict(attrs) # Copy attrs to avoid modifying the argument. |
|
92
|
|
|
|
|
93
|
1 |
|
if (not widget.use_required_attribute(value) or not widget.is_required) \ |
|
94
|
|
|
and 'required' in attrs: |
|
95
|
1 |
|
del attrs['required'] |
|
96
|
|
|
|
|
97
|
1 |
|
return attrs |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
1 |
|
class LocalizedCharFieldWidget(LocalizedFieldWidget): |
|
101
|
|
|
"""Widget that has an input box for every language.""" |
|
102
|
1 |
|
widget = forms.TextInput |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
1 |
|
class LocalizedFileWidget(LocalizedFieldWidget): |
|
106
|
|
|
"""Widget that has an file input box for every language.""" |
|
107
|
1 |
|
widget = forms.ClearableFileInput |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
1 |
|
class AdminLocalizedFieldWidget(LocalizedFieldWidget): |
|
111
|
1 |
|
template_name = 'localized_fields/admin/widget.html' |
|
112
|
1 |
|
widget = widgets.AdminTextareaWidget |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
1 |
|
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget): |
|
116
|
1 |
|
widget = widgets.AdminTextInputWidget |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
1 |
|
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget): |
|
120
|
|
|
widget = widgets.AdminFileWidget |
|
121
|
|
|
|
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.