Passed
Pull Request — master (#1)
by
unknown
01:52
created

LocalizedStingValue

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 2
ccs 2
cts 2
cp 1
wmc 0
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 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 1
        if isinstance(keys, str):
20
            setattr(self, settings.LANGUAGE_CODE, keys)
21
        else:
22 1
            for lang_code, _ in settings.LANGUAGES:
23 1
                value = keys.get(lang_code) if keys else self.default_value
24 1
                self.set(lang_code, value)
25
26 1
    def get(self, language: str=None) -> str:
27
        """Gets the underlying value in the specified or
28
        primary language.
29
30
        Arguments:
31
            language:
32
                The language to get the value in.
33
34
        Returns:
35
            The value in the current language, or
36
            the primary language in case no language
37
            was specified.
38
        """
39
40 1
        language = language or settings.LANGUAGE_CODE
41 1
        return getattr(self, language, None)
42
43 1
    def set(self, language: str, value: str):
44
        """Sets the value in the specified language.
45
46
        Arguments:
47
            language:
48
                The language to set the value in.
49
50
            value:
51
                The value to set.
52
        """
53
54 1
        setattr(self, language, value)
55 1
        return self
56
57 1
    def deconstruct(self) -> dict:
58
        """Deconstructs this value into a primitive type.
59
60
        Returns:
61
            A dictionary with all the localized values
62
            contained in this instance.
63
        """
64
65 1
        path = 'localized_fields.fields.LocalizedValue'
66 1
        return path, [self.__dict__], {}
67
68 1
    def __str__(self) -> str:
69
        """Gets the value in the current language, or falls
70
        back to the primary language if there's no value
71
        in the current language."""
72
73 1
        value = self.get(translation.get_language())
74
75 1
        if not value:
76 1
            value = self.get(settings.LANGUAGE_CODE)
77
78 1
        return value or ''
79
80
    def __repr__(self):  # pragma: no cover
81
        """Gets a textual representation of this object."""
82
83
        return '%s<%s> 0x%s' % (self.__class__.__name__,
84
                                self.__dict__, id(self))
85
86
87 1
class LocalizedStingValue(LocalizedValue):
88
    default_value = ''
89