Passed
Pull Request — master (#15)
by
unknown
02:07
created

LocalizedFileWidget

Complexity

Total Complexity 0

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
wmc 0
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 LocalizedCharFieldWidget(LocalizedFieldWidget):
50
    """Widget that has an input box for every language."""
51 1
    widget = forms.TextInput
52
53
54 1
class LocalizedFileWidget(LocalizedFieldWidget):
55
    """Widget that has an file input box for every language."""
56 1
    widget = forms.ClearableFileInput
57
58
59 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
60 1
    widget = widgets.AdminTextareaWidget
61 1
    template = 'localized_fields/admin/widget.html'
62
63 1
    def render(self, name, value, attrs=None):
64
        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...
65
            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...
66
                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...
67
        # value is a list of values, each corresponding to a widget
68
        # in self.widgets.
69
        if not isinstance(value, list):
70
            value = self.decompress(value)
71
        output = []
72
        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...
73
        id_ = final_attrs.get('id')
74
        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...
75
            try:
76
                widget_value = value[i]
77
            except IndexError:
78
                widget_value = None
79
            if id_:
80
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
81
            widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs)
82
            output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs))
83
        context = {
84
            'id': final_attrs.get('id'),
85
            'name': name,
86
            'widgets': zip([code for code, lang in settings.LANGUAGES], output),
87
            'available_languages': settings.LANGUAGES
88
        }
89
        return render_to_string(self.template, context)
90
91 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...
92
        attrs = dict(attrs)  # Copy attrs to avoid modifying the argument.
93
        if (not widget.use_required_attribute(value) or not widget.is_required) \
94
                and 'required' in attrs:
95
            del attrs['required']
96
        return attrs
97
98
99 1
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget):
100 1
    widget = widgets.AdminTextInputWidget
101
102
103 1
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget):
104
    widget = widgets.AdminFileWidget
105