Passed
Pull Request — master (#1)
by
unknown
01:42
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
        # NOTE(seroy): First fill all the keys default values,
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 self.default_value
23 1
            self.set(lang_code, value)
24
25 1
        if isinstance(keys, str):
26
            setattr(self, settings.LANGUAGE_CODE, keys)
27
28 1
    def get(self, language: str=None) -> str:
29
        """Gets the underlying value in the specified or
30
        primary language.
31
32
        Arguments:
33
            language:
34
                The language to get the value in.
35
36
        Returns:
37
            The value in the current language, or
38
            the primary language in case no language
39
            was specified.
40
        """
41
42 1
        language = language or settings.LANGUAGE_CODE
43 1
        return getattr(self, language, None)
44
45 1
    def set(self, language: str, value: str):
46
        """Sets the value in the specified language.
47
48
        Arguments:
49
            language:
50
                The language to set the value in.
51
52
            value:
53
                The value to set.
54
        """
55
56 1
        setattr(self, language, value)
57 1
        return self
58
59 1
    def deconstruct(self) -> dict:
60
        """Deconstructs this value into a primitive type.
61
62
        Returns:
63
            A dictionary with all the localized values
64
            contained in this instance.
65
        """
66
67 1
        path = 'localized_fields.fields.LocalizedValue'
68 1
        return path, [self.__dict__], {}
69
70 1
    def __str__(self) -> str:
71
        """Gets the value in the current language, or falls
72
        back to the primary language if there's no value
73
        in the current language."""
74
75 1
        value = self.get(translation.get_language())
76
77 1
        if not value:
78 1
            value = self.get(settings.LANGUAGE_CODE)
79
80 1
        return value or ''
81
82
    def __repr__(self):  # pragma: no cover
83
        """Gets a textual representation of this object."""
84
85
        return '%s<%s> 0x%s' % (self.__class__.__name__,
86
                                self.__dict__, id(self))
87
88
89 1
class LocalizedStingValue(LocalizedValue):
90 1
    default_value = ''
91
0 ignored issues
show
coding-style introduced by
Trailing newlines
Loading history...
92