Passed
Push — master ( a9037b...23c6f9 )
by Swen
02:05
created

AdminLocalizedFieldWidget   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 17.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 10
ccs 5
cts 28
cp 0.1786
wmc 12

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build_widget_attrs() 0 6 4
D render() 0 27 8
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 1
from django.template.loader import render_to_string
0 ignored issues
show
Configuration introduced by
The import django.template.loader 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...
7
8 1
from .localized_value import LocalizedValue
9
10
11 1
class LocalizedFieldWidget(forms.MultiWidget):
12
    """Widget that has an input box for every language."""
13 1
    widget = forms.Textarea
14
15 1
    def __init__(self, *args, **kwargs):
16
        """Initializes a new instance of :see:LocalizedFieldWidget."""
17
18 1
        widgets = []
0 ignored issues
show
Comprehensibility Bug introduced by
widgets is re-defining a name which is already available in the outer-scope (previously defined on line 5).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
19
20 1
        for _ in settings.LANGUAGES:
21 1
            widgets.append(self.widget)
22
23 1
        super(LocalizedFieldWidget, self).__init__(widgets, *args, **kwargs)
24
25 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...
26
        """Decompresses the specified value so
27
        it can be spread over the internal widgets.
28
29
        Arguments:
30
            value:
31
                The :see:LocalizedValue to display in this
32
                widget.
33
34
        Returns:
35
            All values to display in the inner widgets.
36
        """
37
38 1
        result = []
39
40 1
        for lang_code, _ in settings.LANGUAGES:
41 1
            if value:
42 1
                result.append(value.get(lang_code))
43
            else:
44 1
                result.append(None)
45
46 1
        return result
47
48
49 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
50 1
    widget = widgets.AdminTextareaWidget
51 1
    template = 'localized_fields/admin/widget.html'
52
53 1
    def render(self, name, value, attrs=None):
54
        if self.is_localized:
0 ignored issues
show
Bug introduced by
The Instance of AdminLocalizedFieldWidget 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 AdminLocalizedFieldWidget 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 AdminLocalizedFieldWidget 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
        if not isinstance(value, list):
60
            value = self.decompress(value)
61
        output = []
62
        final_attrs = self.build_attrs(attrs)
0 ignored issues
show
Bug introduced by
The Instance of AdminLocalizedFieldWidget does not seem to have a member named build_attrs.

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...
63
        id_ = final_attrs.get('id')
64
        for i, widget in enumerate(self.widgets):
0 ignored issues
show
Bug introduced by
The Instance of AdminLocalizedFieldWidget 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...
65
            try:
66
                widget_value = value[i]
67
            except IndexError:
68
                widget_value = None
69
            if id_:
70
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
71
            widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs)
72
            output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs))
73
        context = {
74
            'id': final_attrs.get('id'),
75
            'name': name,
76
            'widgets': zip([code for code, lang in settings.LANGUAGES], output),
77
            'available_languages': settings.LANGUAGES
78
        }
79
        return render_to_string(self.template, context)
80
81 1
    def build_widget_attrs(self, widget, value, attrs):
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...
82
        attrs = dict(attrs)  # Copy attrs to avoid modifying the argument.
83
        if (not widget.use_required_attribute(value) or not widget.is_required) \
84
                and 'required' in attrs:
85
            del attrs['required']
86
        return attrs
87