Passed
Push — master ( fb233e...1b1d24 )
by Swen
04:43
created

LocalizedIntegerField   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
dl 0
loc 72
ccs 40
cts 44
cp 0.9091
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A to_python() 0 5 1
A from_db_value() 0 13 3
B _convert_localized_value() 0 16 5
F get_prep_value() 0 30 9
1 1
from typing import Optional, Union, Dict
0 ignored issues
show
Configuration introduced by
The import typing 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
3 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...
4 1
from django.db.utils import IntegrityError
0 ignored issues
show
Configuration introduced by
The import django.db.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...
5
6 1
from .field import LocalizedField
7 1
from ..value import LocalizedValue, LocalizedIntegerValue
8
9
10 1
class LocalizedIntegerField(LocalizedField):
11
    """Stores integers as a localized value."""
12
13 1
    attr_class = LocalizedIntegerValue
14
15 1
    @classmethod
16 1
    def from_db_value(cls, value, *_) -> Optional[LocalizedIntegerValue]:
17 1
        db_value = super().from_db_value(value)
18 1
        if db_value is None:
19
            return db_value
20
21
        # if we were used in an expression somehow then it might be
22
        # that we're returning an individual value or an array, so
23
        # we should not convert that into an :see:LocalizedIntegerValue
24 1
        if not isinstance(db_value, LocalizedValue):
25
            return db_value
26
27 1
        return cls._convert_localized_value(db_value)
28
29 1
    def to_python(self, value: Union[Dict[str, int], int, None]) -> LocalizedIntegerValue:
30
        """Converts the value from a database value into a Python value."""
31
32
        db_value = super().to_python(value)
33
        return self._convert_localized_value(db_value)
34
35 1
    def get_prep_value(self, value: LocalizedIntegerValue) -> dict:
36
        """Gets the value in a format to store into the database."""
37
38
        # apply default values
39 1
        default_values = LocalizedIntegerValue(self.default)
0 ignored issues
show
Bug introduced by
The Instance of LocalizedIntegerField does not seem to have a member named default.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
40 1
        if isinstance(value, LocalizedIntegerValue):
41 1
            for lang_code, _ in settings.LANGUAGES:
42 1
                local_value = value.get(lang_code)
43 1
                if local_value is None:
44 1
                    value.set(lang_code, default_values.get(lang_code, None))
45
46 1
        prepped_value = super().get_prep_value(value)
47 1
        if prepped_value is None:
48 1
            return None
49
50
        # make sure all values are proper integers
51 1
        for lang_code, _ in settings.LANGUAGES:
52 1
            local_value = prepped_value[lang_code]
53 1
            try:
54 1
                if local_value is not None:
55 1
                    int(local_value)
56 1
            except (TypeError, ValueError):
57 1
                raise IntegrityError('non-integer value in column "%s.%s" violates '
58
                                     'integer constraint' % (self.name, lang_code))
0 ignored issues
show
Bug introduced by
The Instance of LocalizedIntegerField does not seem to have a member named name.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
59
60
            # convert to a string before saving because the underlying
61
            # type is hstore, which only accept strings
62 1
            prepped_value[lang_code] = str(local_value) if local_value is not None else None
63
64 1
        return prepped_value
65
66 1
    @staticmethod
67 1
    def _convert_localized_value(value: LocalizedValue) -> LocalizedIntegerValue:
68
        """Converts from :see:LocalizedValue to :see:LocalizedIntegerValue."""
69
70 1
        integer_values = {}
71 1
        for lang_code, _ in settings.LANGUAGES:
72 1
            local_value = value.get(lang_code, None)
73 1
            if local_value is None or local_value.strip() == '':
74 1
                local_value = None
75
76 1
            try:
77 1
                integer_values[lang_code] = int(local_value)
78 1
            except (ValueError, TypeError):
79 1
                integer_values[lang_code] = None
80
81
        return LocalizedIntegerValue(integer_values)
82