|
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_code'] = lang_code |
|
27
|
1 |
|
widget.attrs['lang_name'] = lang_name |
|
28
|
|
|
|
|
29
|
1 |
|
def decompress(self, value: LocalizedValue) -> List[str]: |
|
|
|
|
|
|
30
|
|
|
"""Decompresses the specified value so |
|
31
|
|
|
it can be spread over the internal widgets. |
|
32
|
|
|
|
|
33
|
|
|
Arguments: |
|
34
|
|
|
value: |
|
35
|
|
|
The :see:LocalizedValue to display in this |
|
36
|
|
|
widget. |
|
37
|
|
|
|
|
38
|
|
|
Returns: |
|
39
|
|
|
All values to display in the inner widgets. |
|
40
|
|
|
""" |
|
41
|
|
|
|
|
42
|
1 |
|
result = [] |
|
43
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
44
|
1 |
|
if value: |
|
45
|
1 |
|
result.append(value.get(lang_code)) |
|
46
|
|
|
else: |
|
47
|
1 |
|
result.append(None) |
|
48
|
|
|
|
|
49
|
1 |
|
return result |
|
50
|
|
|
|
|
51
|
1 |
|
@staticmethod |
|
52
|
|
|
def build_widget_attrs(widget, value, attrs): |
|
53
|
|
|
attrs = dict(attrs) # Copy attrs to avoid modifying the argument. |
|
54
|
|
|
|
|
55
|
|
|
if (not widget.use_required_attribute(value) or not widget.is_required) \ |
|
56
|
|
|
and 'required' in attrs: |
|
57
|
|
|
del attrs['required'] |
|
58
|
|
|
|
|
59
|
|
|
return attrs |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
class LocalizedCharFieldWidget(LocalizedFieldWidget): |
|
63
|
|
|
"""Widget that has an input box for every language.""" |
|
64
|
1 |
|
widget = forms.TextInput |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
1 |
|
class LocalizedFileWidget(LocalizedFieldWidget): |
|
68
|
|
|
"""Widget that has an file input box for every language.""" |
|
69
|
1 |
|
widget = forms.ClearableFileInput |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
1 |
|
class AdminLocalizedFieldWidget(LocalizedFieldWidget): |
|
73
|
1 |
|
template_name = 'localized_fields/admin/widget.html' |
|
74
|
1 |
|
widget = widgets.AdminTextareaWidget |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget): |
|
78
|
1 |
|
widget = widgets.AdminTextInputWidget |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
1 |
|
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget): |
|
82
|
|
|
widget = widgets.AdminFileWidget |
|
83
|
|
|
|
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.