|
1
|
1 |
|
from django.conf import settings |
|
|
|
|
|
|
2
|
1 |
|
from django.utils import six, translation |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
64
|
|
|
else: |
|
65
|
|
|
instance.__dict__[self.field.name] = value |
|
66
|
|
|
|
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.