|
1
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
2
|
1 |
|
from django.utils import translation |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
1 |
|
class LocalizedValue(dict): |
|
6
|
|
|
"""Represents the value of a :see:LocalizedField.""" |
|
7
|
1 |
|
default_value = None |
|
8
|
|
|
|
|
9
|
1 |
|
def __init__(self, keys: dict=None): |
|
|
|
|
|
|
10
|
|
|
"""Initializes a new instance of :see:LocalizedValue. |
|
11
|
|
|
|
|
12
|
|
|
Arguments: |
|
13
|
|
|
keys: |
|
14
|
|
|
The keys to initialize this value with. Every |
|
15
|
|
|
key contains the value of this field in a |
|
16
|
|
|
different language. |
|
17
|
|
|
""" |
|
18
|
|
|
|
|
19
|
|
|
# NOTE(seroy): First fill all the keys with default value, |
|
20
|
|
|
# in order to attributes will be for each language |
|
21
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
22
|
1 |
|
value = keys.get(lang_code) if isinstance(keys, dict) else \ |
|
23
|
|
|
self.default_value |
|
24
|
1 |
|
self.set(lang_code, value) |
|
25
|
|
|
|
|
26
|
1 |
|
if isinstance(keys, str): |
|
27
|
1 |
|
setattr(self, settings.LANGUAGE_CODE, keys) |
|
28
|
|
|
|
|
29
|
1 |
|
def get(self, language: str=None) -> str: |
|
30
|
|
|
"""Gets the underlying value in the specified or |
|
31
|
|
|
primary language. |
|
32
|
|
|
|
|
33
|
|
|
Arguments: |
|
34
|
|
|
language: |
|
35
|
|
|
The language to get the value in. |
|
36
|
|
|
|
|
37
|
|
|
Returns: |
|
38
|
|
|
The value in the current language, or |
|
39
|
|
|
the primary language in case no language |
|
40
|
|
|
was specified. |
|
41
|
|
|
""" |
|
42
|
|
|
|
|
43
|
1 |
|
language = language or settings.LANGUAGE_CODE |
|
44
|
1 |
|
return super().get(language, None) |
|
45
|
|
|
|
|
46
|
1 |
|
def set(self, language: str, value: str): |
|
47
|
|
|
"""Sets the value in the specified language. |
|
48
|
|
|
|
|
49
|
|
|
Arguments: |
|
50
|
|
|
language: |
|
51
|
|
|
The language to set the value in. |
|
52
|
|
|
|
|
53
|
|
|
value: |
|
54
|
|
|
The value to set. |
|
55
|
|
|
""" |
|
56
|
|
|
|
|
57
|
1 |
|
self[language] = value |
|
58
|
1 |
|
self.__dict__.update(self) |
|
59
|
1 |
|
return self |
|
60
|
|
|
|
|
61
|
1 |
|
def deconstruct(self) -> dict: |
|
62
|
|
|
"""Deconstructs this value into a primitive type. |
|
63
|
|
|
|
|
64
|
|
|
Returns: |
|
65
|
|
|
A dictionary with all the localized values |
|
66
|
|
|
contained in this instance. |
|
67
|
|
|
""" |
|
68
|
|
|
|
|
69
|
1 |
|
path = 'localized_fields.localized_value.%s' % self.__class__.__name__ |
|
70
|
1 |
|
return path, [self.__dict__], {} |
|
71
|
|
|
|
|
72
|
1 |
|
def __str__(self) -> str: |
|
73
|
|
|
"""Gets the value in the current language, or falls |
|
74
|
|
|
back to the primary language if there's no value |
|
75
|
|
|
in the current language.""" |
|
76
|
|
|
|
|
77
|
1 |
|
value = self.get(translation.get_language()) |
|
78
|
|
|
|
|
79
|
1 |
|
if not value: |
|
80
|
1 |
|
value = self.get(settings.LANGUAGE_CODE) |
|
81
|
|
|
|
|
82
|
1 |
|
return value or '' |
|
83
|
|
|
|
|
84
|
1 |
|
def __eq__(self, other): |
|
85
|
|
|
"""Compares :paramref:self to :paramref:other for |
|
86
|
|
|
equality. |
|
87
|
|
|
|
|
88
|
|
|
Returns: |
|
89
|
|
|
True when :paramref:self is equal to :paramref:other. |
|
90
|
|
|
And False when they are not. |
|
91
|
|
|
""" |
|
92
|
|
|
|
|
93
|
1 |
|
if not isinstance(other, type(self)): |
|
94
|
1 |
|
if isinstance(other, str): |
|
95
|
1 |
|
return self.__str__() == other |
|
96
|
1 |
|
return False |
|
97
|
|
|
|
|
98
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
99
|
1 |
|
if self.get(lang_code) != other.get(lang_code): |
|
100
|
1 |
|
return False |
|
101
|
|
|
|
|
102
|
1 |
|
return True |
|
103
|
|
|
|
|
104
|
1 |
|
def __ne__(self, other): |
|
105
|
|
|
"""Compares :paramref:self to :paramerf:other for |
|
106
|
|
|
in-equality. |
|
107
|
|
|
|
|
108
|
|
|
Returns: |
|
109
|
|
|
True when :paramref:self is not equal to :paramref:other. |
|
110
|
|
|
And False when they are. |
|
111
|
|
|
""" |
|
112
|
|
|
|
|
113
|
1 |
|
return not self.__eq__(other) |
|
114
|
|
|
|
|
115
|
1 |
|
def __setattr__(self, language: str, value: str): |
|
116
|
|
|
"""Sets the value for a language with the specified name. |
|
117
|
|
|
|
|
118
|
|
|
Arguments: |
|
119
|
|
|
language: |
|
120
|
|
|
The language to set the value in. |
|
121
|
|
|
|
|
122
|
|
|
value: |
|
123
|
|
|
The value to set. |
|
124
|
|
|
""" |
|
125
|
|
|
|
|
126
|
1 |
|
self.set(language, value) |
|
127
|
|
|
|
|
128
|
|
|
def __repr__(self): # pragma: no cover |
|
129
|
|
|
"""Gets a textual representation of this object.""" |
|
130
|
|
|
|
|
131
|
|
|
return '%s<%s> 0x%s' % (self.__class__.__name__, |
|
132
|
|
|
self.__dict__, id(self)) |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
1 |
|
class LocalizedStingValue(LocalizedValue): |
|
136
|
1 |
|
default_value = '' |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
1 |
|
class LocalizedFileValue(LocalizedValue): |
|
140
|
1 |
|
def __getattr__(self, name: str): |
|
141
|
|
|
"""Proxies access to attributes to attributes of LocalizedFile""" |
|
142
|
|
|
|
|
143
|
1 |
|
value = self.get(translation.get_language()) |
|
144
|
1 |
|
if hasattr(value, name): |
|
145
|
1 |
|
return getattr(value, name) |
|
146
|
1 |
|
raise AttributeError("'{}' object has no attribute '{}'". |
|
147
|
|
|
format(self.__class__.__name__, name)) |
|
148
|
|
|
|
|
149
|
1 |
|
def __str__(self) -> str: |
|
150
|
|
|
"""Returns string representation of value""" |
|
151
|
1 |
|
return str(super().__str__()) |
|
152
|
|
|
|
|
153
|
1 |
|
def localized(self): |
|
154
|
|
|
"""Returns value for current language""" |
|
155
|
|
|
return self.get(translation.get_language()) |
|
156
|
|
|
|
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.