|
1
|
1 |
|
from django.conf import settings |
|
2
|
1 |
|
from django.utils import translation |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
1 |
|
class LocalizedValue: |
|
6
|
|
|
"""Represents the value of a :see:LocalizedField.""" |
|
7
|
|
|
|
|
8
|
1 |
|
def __init__(self, keys: dict=None): |
|
9
|
|
|
"""Initializes a new instance of :see:LocalizedValue. |
|
10
|
|
|
|
|
11
|
|
|
Arguments: |
|
12
|
|
|
keys: |
|
13
|
|
|
The keys to initialize this value with. Every |
|
14
|
|
|
key contains the value of this field in a |
|
15
|
|
|
different language. |
|
16
|
|
|
""" |
|
17
|
|
|
|
|
18
|
1 |
|
if isinstance(keys, str): |
|
19
|
1 |
|
setattr(self, settings.LANGUAGE_CODE, keys) |
|
20
|
|
|
else: |
|
21
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
22
|
1 |
|
value = keys.get(lang_code) if keys else None |
|
23
|
1 |
|
setattr(self, lang_code, value) |
|
24
|
|
|
|
|
25
|
1 |
|
def get(self, language: str=None) -> str: |
|
26
|
|
|
"""Gets the underlying value in the specified or |
|
27
|
|
|
primary language. |
|
28
|
|
|
|
|
29
|
|
|
Arguments: |
|
30
|
|
|
language: |
|
31
|
|
|
The language to get the value in. |
|
32
|
|
|
|
|
33
|
|
|
Returns: |
|
34
|
|
|
The value in the current language, or |
|
35
|
|
|
the primary language in case no language |
|
36
|
|
|
was specified. |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
1 |
|
language = language or settings.LANGUAGE_CODE |
|
40
|
1 |
|
return getattr(self, language, None) |
|
41
|
|
|
|
|
42
|
1 |
|
def set(self, language: str, value: str): |
|
43
|
|
|
"""Sets the value in the specified language. |
|
44
|
|
|
|
|
45
|
|
|
Arguments: |
|
46
|
|
|
language: |
|
47
|
|
|
The language to set the value in. |
|
48
|
|
|
|
|
49
|
|
|
value: |
|
50
|
|
|
The value to set. |
|
51
|
|
|
""" |
|
52
|
|
|
|
|
53
|
1 |
|
setattr(self, language, value) |
|
54
|
1 |
|
return self |
|
55
|
|
|
|
|
56
|
1 |
|
def deconstruct(self) -> dict: |
|
57
|
|
|
"""Deconstructs this value into a primitive type. |
|
58
|
|
|
|
|
59
|
|
|
Returns: |
|
60
|
|
|
A dictionary with all the localized values |
|
61
|
|
|
contained in this instance. |
|
62
|
|
|
""" |
|
63
|
|
|
|
|
64
|
1 |
|
path = 'localized_fields.fields.LocalizedValue' |
|
65
|
1 |
|
return path, [self.__dict__], {} |
|
66
|
|
|
|
|
67
|
1 |
|
def __str__(self) -> str: |
|
68
|
|
|
"""Gets the value in the current language, or falls |
|
69
|
|
|
back to the primary language if there's no value |
|
70
|
|
|
in the current language.""" |
|
71
|
|
|
|
|
72
|
1 |
|
value = self.get(translation.get_language()) |
|
73
|
|
|
|
|
74
|
1 |
|
if not value: |
|
75
|
1 |
|
value = self.get(settings.LANGUAGE_CODE) |
|
76
|
|
|
|
|
77
|
1 |
|
return value or '' |
|
78
|
|
|
|
|
79
|
1 |
|
def __eq__(self, other): |
|
80
|
|
|
"""Compares :paramref:self to :paramref:other for |
|
81
|
|
|
equality. |
|
82
|
|
|
|
|
83
|
|
|
Returns: |
|
84
|
|
|
True when :paramref:self is equal to :paramref:other. |
|
85
|
|
|
And False when they are not. |
|
86
|
|
|
""" |
|
87
|
|
|
|
|
88
|
1 |
|
for lang_code, _ in settings.LANGUAGES: |
|
89
|
1 |
|
if self.get(lang_code) != other.get(lang_code): |
|
90
|
1 |
|
return False |
|
91
|
|
|
|
|
92
|
1 |
|
return True |
|
93
|
|
|
|
|
94
|
|
|
def __repr__(self): # pragma: no cover |
|
95
|
|
|
"""Gets a textual representation of this object.""" |
|
96
|
|
|
|
|
97
|
|
|
return 'LocalizedValue<%s> 0x%s' % (self.__dict__, id(self)) |
|
98
|
|
|
|