|
1
|
1 |
|
from typing import Optional, Union, Dict |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
4
|
1 |
|
from django.db.utils import IntegrityError |
|
|
|
|
|
|
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)) |
|
|
|
|
|
|
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
|
|
|
|
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.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.