Passed
Push — master ( b3d709...395126 )
by Swen
02:16
created

LocalizedValue.__eq__()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.9256

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 19
ccs 6
cts 9
cp 0.6667
crap 5.9256
rs 8.5454
1 1
from django.conf import settings
0 ignored issues
show
Configuration introduced by
The import django.conf could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
2 1
from django.utils import translation
0 ignored issues
show
Configuration introduced by
The import django.utils could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
3
4
5 1
class LocalizedValue(dict):
6
    """Represents the value of a :see:LocalizedField."""
7
8 1
    def __init__(self, keys: dict=None):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class dict is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
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
                self.set(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 super().get(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
        self[language] = value
54 1
        self.__dict__.update(self)
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 1
    def __eq__(self, other):
81
        """Compares :paramref:self to :paramref:other for
82
        equality.
83
84
        Returns:
85
            True when :paramref:self is equal to :paramref:other.
86
            And False when they are not.
87
        """
88
89 1
        if not isinstance(other, type(self)):
90
            if isinstance(other, str):
91
                return self.__str__() == other
92
            return False
93
94 1
        for lang_code, _ in settings.LANGUAGES:
95 1
            if self.get(lang_code) != other.get(lang_code):
96 1
                return False
97
98 1
        return True
99
100 1
    def __ne__(self, other):
101
        """Compares :paramref:self to :paramerf:other for
102
        in-equality.
103
104
        Returns:
105
            True when :paramref:self is not equal to :paramref:other.
106
            And False when they are.
107
        """
108
109 1
        return not self.__eq__(other)
110
111 1
    def __setattr__(self, language: str, value: str):
112
        """Sets the value for a language with the specified name.
113
114
        Arguments:
115
            language:
116
                The language to set the value in.
117
118
            value:
119
                The value to set.
120
        """
121
122 1
        self.set(language, value)
123
124
    def __repr__(self):  # pragma: no cover
125
        """Gets a textual representation of this object."""
126
127
        return 'LocalizedValue<%s> 0x%s' % (dict(self), id(self))
128