Passed
Push — master ( 83605c...084f5d )
by Swen
02:08 queued 30s
created

LocalizedFieldWidget.get_context()   D

Complexity

Conditions 8

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 8.9003

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
rs 4
ccs 22
cts 29
cp 0.7586
cc 8
crap 8.9003
1 1
from typing import List
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 import forms
0 ignored issues
show
Configuration introduced by
The import django 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 1
from django.contrib.admin import widgets
0 ignored issues
show
Configuration introduced by
The import django.contrib.admin 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...
6
7 1
from .value import LocalizedValue
8
9
10 1
class LocalizedFieldWidget(forms.MultiWidget):
11
    """Widget that has an input box for every language."""
12 1
    template_name = 'localized_fields/multiwidget.html'
13 1
    widget = forms.Textarea
14
15 1
    def __init__(self, *args, **kwargs):
16
        """Initializes a new instance of :see:LocalizedFieldWidget."""
17
18 1
        initial_widgets = [
19
            self.widget
20
            for _ in settings.LANGUAGES
21
        ]
22
23 1
        super().__init__(initial_widgets, *args, **kwargs)
24
25 1
        for ((lang_code, lang_name), widget) in zip(settings.LANGUAGES, self.widgets):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

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...
26 1
            widget.attrs['lang'] = lang_code
27 1
            widget.lang_code = lang_code
28 1
            widget.lang_name = lang_name
29
30 1
    def decompress(self, value: LocalizedValue) -> List[str]:
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
31
        """Decompresses the specified value so
32
        it can be spread over the internal widgets.
33
34
        Arguments:
35
            value:
36
                The :see:LocalizedValue to display in this
37
                widget.
38
39
        Returns:
40
            All values to display in the inner widgets.
41
        """
42
43 1
        result = []
44 1
        for lang_code, _ in settings.LANGUAGES:
45 1
            if value:
46 1
                result.append(value.get(lang_code))
47
            else:
48 1
                result.append(None)
49
50 1
        return result
51
52 1
    def get_context(self, name, value, attrs):
53 1
        context = super(forms.MultiWidget, self).get_context(name, value, attrs)
54 1
        if self.is_localized:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named is_localized.

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...
55
            for widget in self.widgets:
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

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...
56
                widget.is_localized = self.is_localized
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named is_localized.

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...
57
        # value is a list of values, each corresponding to a widget
58
        # in self.widgets.
59 1
        if not isinstance(value, list):
60 1
            value = self.decompress(value)
61
62 1
        final_attrs = context['widget']['attrs']
63 1
        input_type = final_attrs.pop('type', None)
64 1
        id_ = final_attrs.get('id')
65 1
        subwidgets = []
66 1
        for i, widget in enumerate(self.widgets):
0 ignored issues
show
Bug introduced by
The Instance of LocalizedFieldWidget does not seem to have a member named widgets.

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...
67 1
            if input_type is not None:
68
                widget.input_type = input_type
69 1
            widget_name = '%s_%s' % (name, i)
70 1
            try:
71 1
                widget_value = value[i]
72
            except IndexError:
73
                widget_value = None
74 1
            if id_:
75
                widget_attrs = final_attrs.copy()
76
                widget_attrs['id'] = '%s_%s' % (id_, i)
77
            else:
78 1
                widget_attrs = final_attrs
79 1
            widget_attrs = self.build_widget_attrs(widget, widget_value, widget_attrs)
80 1
            widget_context = widget.get_context(widget_name, widget_value, widget_attrs)['widget']
81 1
            widget_context.update(dict(
82
                lang_code=widget.lang_code,
83
                lang_name=widget.lang_name
84
            ))
85 1
            subwidgets.append(widget_context)
86 1
        context['widget']['subwidgets'] = subwidgets
87 1
        return context
88
89 1
    @staticmethod
90
    def build_widget_attrs(widget, value, attrs):
91 1
        attrs = dict(attrs)  # Copy attrs to avoid modifying the argument.
92
93 1
        if (not widget.use_required_attribute(value) or not widget.is_required) \
94
                and 'required' in attrs:
95 1
            del attrs['required']
96
97 1
        return attrs
98
99
100 1
class LocalizedCharFieldWidget(LocalizedFieldWidget):
101
    """Widget that has an input box for every language."""
102 1
    widget = forms.TextInput
103
104
105 1
class LocalizedFileWidget(LocalizedFieldWidget):
106
    """Widget that has an file input box for every language."""
107 1
    widget = forms.ClearableFileInput
108
109
110 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
111 1
    template_name = 'localized_fields/admin/widget.html'
112 1
    widget = widgets.AdminTextareaWidget
113
114
115 1
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget):
116 1
    widget = widgets.AdminTextInputWidget
117
118
119 1
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget):
120
    widget = widgets.AdminFileWidget
121