|
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
|
1 |
|
from django.template.loader import render_to_string |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
1 |
|
from .localized_value import LocalizedValue |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class LocalizedFieldWidget(forms.MultiWidget): |
|
12
|
|
|
"""Widget that has an input box for every language.""" |
|
13
|
1 |
|
widget = forms.Textarea |
|
14
|
|
|
|
|
15
|
1 |
|
def __init__(self, *args, **kwargs): |
|
16
|
|
|
"""Initializes a new instance of :see:LocalizedFieldWidget.""" |
|
17
|
|
|
|
|
18
|
1 |
|
widgets = [] |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
1 |
|
for _ in settings.LANGUAGES: |
|
21
|
1 |
|
widgets.append(self.widget) |
|
22
|
|
|
|
|
23
|
1 |
|
super(LocalizedFieldWidget, self).__init__(widgets, *args, **kwargs) |
|
24
|
|
|
|
|
25
|
1 |
|
def decompress(self, value: LocalizedValue) -> List[str]: |
|
|
|
|
|
|
26
|
|
|
"""Decompresses the specified value so |
|
27
|
|
|
it can be spread over the internal widgets. |
|
28
|
|
|
|
|
29
|
|
|
Arguments: |
|
30
|
|
|
value: |
|
31
|
|
|
The :see:LocalizedValue to display in this |
|
32
|
|
|
widget. |
|
33
|
|
|
|
|
34
|
|
|
Returns: |
|
35
|
|
|
All values to display in the inner widgets. |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
1 |
|
result = [] |
|
39
|
|
|
|
|
40
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
41
|
1 |
|
if value: |
|
42
|
1 |
|
result.append(value.get(lang_code)) |
|
43
|
|
|
else: |
|
44
|
1 |
|
result.append(None) |
|
45
|
|
|
|
|
46
|
1 |
|
return result |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
1 |
|
class AdminLocalizedFieldWidget(LocalizedFieldWidget): |
|
50
|
1 |
|
widget = widgets.AdminTextareaWidget |
|
51
|
1 |
|
template = 'localized_fields/admin/widget.html' |
|
52
|
|
|
|
|
53
|
1 |
|
def render(self, name, value, attrs=None): |
|
54
|
|
|
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
|
|
|
if not isinstance(value, list): |
|
60
|
|
|
value = self.decompress(value) |
|
61
|
|
|
output = [] |
|
62
|
|
|
final_attrs = self.build_attrs(attrs) |
|
|
|
|
|
|
63
|
|
|
id_ = final_attrs.get('id') |
|
64
|
|
|
for i, widget in enumerate(self.widgets): |
|
|
|
|
|
|
65
|
|
|
try: |
|
66
|
|
|
widget_value = value[i] |
|
67
|
|
|
except IndexError: |
|
68
|
|
|
widget_value = None |
|
69
|
|
|
if id_: |
|
70
|
|
|
final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) |
|
71
|
|
|
widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs) |
|
72
|
|
|
output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs)) |
|
73
|
|
|
context = { |
|
74
|
|
|
'id': final_attrs.get('id'), |
|
75
|
|
|
'name': name, |
|
76
|
|
|
'widgets': zip([code for code, lang in settings.LANGUAGES], output), |
|
77
|
|
|
'available_languages': settings.LANGUAGES |
|
78
|
|
|
} |
|
79
|
|
|
return render_to_string(self.template, context) |
|
80
|
|
|
|
|
81
|
1 |
|
def build_widget_attrs(self, widget, value, attrs): |
|
|
|
|
|
|
82
|
|
|
attrs = dict(attrs) # Copy attrs to avoid modifying the argument. |
|
83
|
|
|
if (not widget.use_required_attribute(value) or not widget.is_required) \ |
|
84
|
|
|
and 'required' in attrs: |
|
85
|
|
|
del attrs['required'] |
|
86
|
|
|
return attrs |
|
87
|
|
|
|
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.