Passed
Push — master ( 5a4f44...92a53b )
by Swen
01:57
created

LocalizedValueDescriptor.__set__()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 9.4285
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 six, 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 LocalizedValueDescriptor:
6
    """
7
    The descriptor for the localized value attribute on the model instance.
8
    Returns a :see:LocalizedValue when accessed so you can do stuff like::
9
10
        >>> from myapp.models import MyModel
11
        >>> instance = MyModel()
12
        >>> instance.value.en = 'English value'
13
14
    Assigns a strings to active language key in :see:LocalizedValue on
15
    assignment so you can do::
16
17
        >>> from django.utils import translation
18
        >>> from myapp.models import MyModel
19
20
        >>> translation.activate('nl')
21
        >>> instance = MyModel()
22
        >>> instance.title = 'dutch title'
23
        >>> print(instance.title.nl) # prints 'dutch title'
24
    """
25
26 1
    def __init__(self, field):
27
        """Initializes a new instance of :see:LocalizedValueDescriptor."""
28
29 1
        self.field = field
30
31 1
    def __get__(self, instance, cls=None):
32 1
        if instance is None:
33
            return self
34
35
        # This is slightly complicated, so worth an explanation.
36
        # `instance.localizedvalue` needs to ultimately return some instance of
37
        # `LocalizedValue`, probably a subclass.
38
39
        # The instance dict contains whatever was originally assigned
40
        # in __set__.
41
42 1
        if self.field.name in instance.__dict__:
43 1
            value = instance.__dict__[self.field.name]
44
        elif instance.pk is not None:
45
            instance.refresh_from_db(fields=[self.field.name])
46
            value = getattr(instance, self.field.name)
47
        else:
48
            value = None
49
50 1
        if value is None:
51 1
            attr = self.field.attr_class()
52 1
            instance.__dict__[self.field.name] = attr
53
54 1
        if isinstance(value, dict):
55 1
            attr = self.field.attr_class(value)
56 1
            instance.__dict__[self.field.name] = attr
57
58 1
        return instance.__dict__[self.field.name]
59
60 1
    def __set__(self, instance, value):
61 1
        if isinstance(value, six.string_types):
62
            language = translation.get_language() or settings.LANGUAGE_CODE
63
            self.__get__(instance).set(language, value) # pylint: disable=no-member
0 ignored issues
show
introduced by
Locally disabling no-member (E1101)
Loading history...
64
        else:
65
            instance.__dict__[self.field.name] = value
66