Passed
Push — master ( f7c14f...06873a )
by Swen
02:26
created

AdminLocalizedFileFieldWidget

Complexity

Total Complexity 0

Size/Duplication

Total Lines 2
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 2
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 .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
        initial_widgets = [
19
            self.widget
20
            for _ in settings.LANGUAGES
21
        ]
22
23 1
        super().__init__(initial_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 1
        for lang_code, _ in settings.LANGUAGES:
40 1
            if value:
41 1
                result.append(value.get(lang_code))
42
            else:
43 1
                result.append(None)
44
45 1
        return result
46
47
48 1
class LocalizedCharFieldWidget(LocalizedFieldWidget):
49
    """Widget that has an input box for every language."""
50 1
    widget = forms.TextInput
51
52
53 1
class LocalizedFileWidget(LocalizedFieldWidget):
54
    """Widget that has an file input box for every language."""
55 1
    widget = forms.ClearableFileInput
56
57
58 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
59 1
    widget = widgets.AdminTextareaWidget
60 1
    template = 'localized_fields/admin/widget.html'
61
62 1
    def render(self, name, value, attrs=None):
63
        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...
64
            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...
65
                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...
66
        # value is a list of values, each corresponding to a widget
67
        # in self.widgets.
68
        if not isinstance(value, list):
69
            value = self.decompress(value)
70
        output = []
71
        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...
72
        id_ = final_attrs.get('id')
73
        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...
74
            try:
75
                widget_value = value[i]
76
            except IndexError:
77
                widget_value = None
78
            if id_:
79
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
80
            widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs)
81
            output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs))
82
        context = {
83
            'id': final_attrs.get('id'),
84
            'name': name,
85
            'widgets': zip([code for code, lang in settings.LANGUAGES], output),
86
            'available_languages': settings.LANGUAGES
87
        }
88
        return render_to_string(self.template, context)
89
90 1
    @staticmethod
91
    def build_widget_attrs(widget, value, attrs):
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