Passed
Pull Request — master (#31)
by Swen
01:21
created

LocalizedValueDescriptor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 9
c 3
b 1
f 0
dl 0
loc 61
ccs 21
cts 24
cp 0.875
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 1
B __get__() 0 28 6
A __set__() 0 6 2
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 1
        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 1
            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 1
            language = translation.get_language() or settings.LANGUAGE_CODE
63 1
            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