Passed
Pull Request — master (#34)
by Swen
02:27 queued 01:02
created

AdminLocalizedFieldWidget.render()   D

Complexity

Conditions 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 62.8719

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 33
ccs 1
cts 20
cp 0.05
rs 4
cc 8
crap 62.8719

1 Method

Rating   Name   Duplication   Size   Complexity  
A LocalizedFieldWidget.build_widget_attrs() 0 9 4
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_code'] = lang_code
27 1
            widget.attrs['lang_name'] = lang_name
28
29 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...
30
        """Decompresses the specified value so
31
        it can be spread over the internal widgets.
32
33
        Arguments:
34
            value:
35
                The :see:LocalizedValue to display in this
36
                widget.
37
38
        Returns:
39
            All values to display in the inner widgets.
40
        """
41
42 1
        result = []
43 1
        for lang_code, _ in settings.LANGUAGES:
44 1
            if value:
45 1
                result.append(value.get(lang_code))
46
            else:
47 1
                result.append(None)
48
49 1
        return result
50
51 1
    def get_context(self, name, value, attrs):
52 1
        context = super(forms.MultiWidget, self).get_context(name, value, attrs)
53 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...
54
            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...
55
                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...
56
        # value is a list of values, each corresponding to a widget
57
        # in self.widgets.
58 1
        if not isinstance(value, list):
59 1
            value = self.decompress(value)
60
61 1
        final_attrs = context['widget']['attrs']
62 1
        input_type = final_attrs.pop('type', None)
63 1
        id_ = final_attrs.get('id')
64 1
        subwidgets = []
65 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...
66 1
            if input_type is not None:
67
                widget.input_type = input_type
68 1
            widget_name = '%s_%s' % (name, i)
69 1
            try:
70 1
                widget_value = value[i]
71
            except IndexError:
72
                widget_value = None
73 1
            if id_:
74
                widget_attrs = final_attrs.copy()
75
                widget_attrs['id'] = '%s_%s' % (id_, i)
76
            else:
77 1
                widget_attrs = final_attrs
78 1
            widget_attrs = self.build_widget_attrs(widget, widget_value, widget_attrs)
79 1
            subwidgets.append(widget.get_context(widget_name, widget_value, widget_attrs)['widget'])
80 1
        context['widget']['subwidgets'] = subwidgets
81 1
        return context
82
83 1
    @staticmethod
84
    def build_widget_attrs(widget, value, attrs):
85 1
        attrs = dict(attrs)  # Copy attrs to avoid modifying the argument.
86
87 1
        if (not widget.use_required_attribute(value) or not widget.is_required) \
88
                and 'required' in attrs:
89 1
            del attrs['required']
90
91 1
        return attrs
92
93
94 1
class LocalizedCharFieldWidget(LocalizedFieldWidget):
95
    """Widget that has an input box for every language."""
96 1
    widget = forms.TextInput
97
98
99 1
class LocalizedFileWidget(LocalizedFieldWidget):
100
    """Widget that has an file input box for every language."""
101 1
    widget = forms.ClearableFileInput
102
103
104 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
105 1
    template_name = 'localized_fields/admin/widget.html'
106 1
    widget = widgets.AdminTextareaWidget
107
108
109 1
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget):
110 1
    widget = widgets.AdminTextInputWidget
111
112
113 1
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget):
114
    widget = widgets.AdminFileWidget
115