Test Failed
Push — master ( 752e17...8c83fa )
by Swen
02:59
created

LocalizedIntegerField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
dl 0
loc 59
ccs 18
cts 36
cp 0.5
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A from_db_value() 0 13 3
B _convert_localized_value() 0 16 5
B get_prep_value() 0 17 5
A to_python() 0 5 1
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
        db_value = super().from_db_value(value)
18
        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
        if not isinstance(db_value, LocalizedValue):
25
            return db_value
26
27
        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: LocalizedValue) -> dict:
36
        """Gets the value in a format to store into the database."""
37
38 1
        prepped_value = super().get_prep_value(value)
39 1
        if prepped_value is None:
40 1
            return None
41
42
        # make sure all values are proper integers
43 1
        for lang_code, _ in settings.LANGUAGES:
44 1
            try:
45 1
                if prepped_value[lang_code] is not None:
46 1
                    int(prepped_value[lang_code])
47 1
            except (TypeError, ValueError):
48 1
                raise IntegrityError('non-integer value in column "%s.%s" violates '
49
                                     '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...
50
51 1
        return prepped_value
52
53 1
    @staticmethod
54 1
    def _convert_localized_value(value: LocalizedValue) -> LocalizedIntegerValue:
55
        """Converts from :see:LocalizedValue to :see:LocalizedIntegerValue."""
56
57
        integer_values = {}
58
        for lang_code, _ in settings.LANGUAGES:
59
            local_value = value.get(lang_code, None)
60
            if local_value is None or local_value.strip() == '':
61
                local_value = None
62
63
            try:
64
                integer_values[lang_code] = int(local_value)
65
            except (ValueError, TypeError):
66
                integer_values[lang_code] = None
67
68
        return LocalizedIntegerValue(integer_values)
69