Passed
Pull Request — master (#33)
by
unknown
01:25
created

AdminLocalizedCharFieldWidget

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
    template_name = 'localized_fields/multiwidget.html'
14 1
    widget = forms.Textarea
15
16 1
    def __init__(self, *args, **kwargs):
17
        """Initializes a new instance of :see:LocalizedFieldWidget."""
18
19 1
        initial_widgets = [
20
            self.widget
21
            for _ in settings.LANGUAGES
22
        ]
23
24 1
        super().__init__(initial_widgets, *args, **kwargs)
25
26 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...
27 1
            widget.attrs['lang_code'] = lang_code
28 1
            widget.attrs['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
53 1
class LocalizedCharFieldWidget(LocalizedFieldWidget):
54
    """Widget that has an input box for every language."""
55 1
    widget = forms.TextInput
56
57
58 1
class LocalizedFileWidget(LocalizedFieldWidget):
59
    """Widget that has an file input box for every language."""
60 1
    widget = forms.ClearableFileInput
61
62
63 1
class AdminLocalizedFieldWidget(LocalizedFieldWidget):
64 1
    widget = widgets.AdminTextareaWidget
65 1
    template = 'localized_fields/admin/widget.html'
66
67 1
    def render(self, name, value, attrs=None):
68
        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...
69
            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...
70
                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...
71
72
        # value is a list of values, each corresponding to a widget
73
        # in self.widgets.
74
        if not isinstance(value, list):
75
            value = self.decompress(value)
76
77
        output = []
78
        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...
79
        id_ = final_attrs.get('id')
80
81
        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...
82
            try:
83
                widget_value = value[i]
84
            except IndexError:
85
                widget_value = None
86
            if id_:
87
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
88
89
            widget_attrs = self.build_widget_attrs(widget, widget_value, final_attrs)
90
            output.append(widget.render(name + '_%s' % i, widget_value, widget_attrs))
91
92
        context = {
93
            'id': final_attrs.get('id'),
94
            'name': name,
95
            'widgets': zip([code for code, lang in settings.LANGUAGES], output),
96
            'available_languages': settings.LANGUAGES
97
        }
98
99
        return render_to_string(self.template, context)
100
101 1
    @staticmethod
102
    def build_widget_attrs(widget, value, attrs):
103
        attrs = dict(attrs)  # Copy attrs to avoid modifying the argument.
104
105
        if (not widget.use_required_attribute(value) or not widget.is_required) \
106
                and 'required' in attrs:
107
            del attrs['required']
108
109
        return attrs
110
111
112 1
class AdminLocalizedCharFieldWidget(AdminLocalizedFieldWidget):
113 1
    widget = widgets.AdminTextInputWidget
114
115
116 1
class AdminLocalizedFileFieldWidget(AdminLocalizedFieldWidget):
117
    widget = widgets.AdminFileWidget
118