select_multiple pep8

This commit is contained in:
Artur Barseghyan 2016-09-20 15:39:47 +02:00
parent 6e8ada7f7e
commit b52724927f
7 changed files with 97 additions and 65 deletions

View file

@ -4,6 +4,7 @@ __copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('default_app_config', 'UID',)
default_app_config = 'fobi.contrib.plugins.form_elements.fields.select_multiple.apps.Config'
default_app_config = 'fobi.contrib.plugins.form_elements.fields.' \
'select_multiple.apps.Config'
UID = 'select_multiple'

View file

@ -8,6 +8,8 @@ try:
from django.apps import AppConfig
class Config(AppConfig):
"""Config."""
name = 'fobi.contrib.plugins.form_elements.fields.select_multiple'
label = 'fobi_contrib_plugins_form_elements_fields_select_multiple'

View file

@ -1,15 +1,17 @@
from django.conf import settings
from . import defaults
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.conf'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('get_setting',)
from django.conf import settings
from . import defaults
def get_setting(setting, override=None):
"""
"""Get setting.
Get a setting from
`fobi.contrib.plugins.form_elements.fields.select_multiple` conf module,
falling back to the default.
@ -23,7 +25,11 @@ def get_setting(setting, override=None):
"""
if override is not None:
return override
if hasattr(settings, 'FOBI_FORM_ELEMENT_SELECT_MULTIPLE_{0}'.format(setting)):
return getattr(settings, 'FOBI_FORM_ELEMENT_SELECT_MULTIPLE_{0}'.format(setting))
if hasattr(settings,
'FOBI_FORM_ELEMENT_SELECT_MULTIPLE_{0}'.format(setting)):
return getattr(
settings,
'FOBI_FORM_ELEMENT_SELECT_MULTIPLE_{0}'.format(setting)
)
else:
return getattr(defaults, setting)

View file

@ -1,9 +1,10 @@
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.defaults'
from fobi.constants import SUBMIT_VALUE_AS_REPR
__title__ = 'fobi.contrib.plugins.form_elements.fields.' \
'select_multiple.defaults'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SUBMIT_VALUE_AS',)
from fobi.constants import SUBMIT_VALUE_AS_REPR
SUBMIT_VALUE_AS = SUBMIT_VALUE_AS_REPR

View file

@ -1,9 +1,3 @@
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.fobi_form_elements'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SelectMultipleInputPlugin',)
from django.forms.fields import MultipleChoiceField
from django.forms.widgets import SelectMultiple
from django.utils.translation import ugettext_lazy as _
@ -11,28 +5,33 @@ from django.utils.translation import ugettext_lazy as _
from fobi.base import FormFieldPlugin, form_element_plugin_registry, get_theme
from fobi.constants import (
SUBMIT_VALUE_AS_VAL, SUBMIT_VALUE_AS_REPR
)
)
from fobi.helpers import get_select_field_choices, safe_text
from . import UID
from .forms import SelectMultipleInputForm
from .settings import SUBMIT_VALUE_AS
__title__ = 'fobi.contrib.plugins.form_elements.fields.' \
'select_multiple.fobi_form_elements'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SelectMultipleInputPlugin',)
theme = get_theme(request=None, as_instance=True)
class SelectMultipleInputPlugin(FormFieldPlugin):
"""
Select multiple field plugin.
"""
"""Select multiple field plugin."""
uid = UID
name = _("Select multiple")
group = _("Fields")
form = SelectMultipleInputForm
def get_form_field_instances(self, request=None):
"""
Get form field instances.
"""
"""Get form field instances."""
choices = get_select_field_choices(self.data.choices)
kwargs = {
@ -41,7 +40,9 @@ class SelectMultipleInputPlugin(FormFieldPlugin):
'initial': self.data.initial,
'required': self.data.required,
'choices': choices,
'widget': SelectMultiple(attrs={'class': theme.form_element_html_class}),
'widget': SelectMultiple(
attrs={'class': theme.form_element_html_class}
),
}
return [(self.data.name, MultipleChoiceField, kwargs)]

View file

@ -1,21 +1,21 @@
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.forms'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2015 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SelectMultipleInputForm',)
from django import forms
from django.utils.translation import ugettext_lazy as _
from fobi.base import BaseFormFieldPluginForm, get_theme
from fobi.helpers import validate_initial_for_multiple_choices
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.forms'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SelectMultipleInputForm',)
theme = get_theme(request=None, as_instance=True)
class SelectMultipleInputForm(forms.Form, BaseFormFieldPluginForm):
"""
Form for ``SelectMultipleInputPlugin``.
"""
"""Form for ``SelectMultipleInputPlugin``."""
plugin_data_fields = [
("label", ""),
("name", ""),
@ -28,52 +28,72 @@ class SelectMultipleInputForm(forms.Form, BaseFormFieldPluginForm):
label = forms.CharField(
label=_("Label"),
required=True,
widget=forms.widgets.TextInput(attrs={'class': theme.form_element_html_class})
widget=forms.widgets.TextInput(
attrs={'class': theme.form_element_html_class}
)
)
name = forms.CharField(
label=_("Name"),
required=True,
widget=forms.widgets.TextInput(attrs={'class': theme.form_element_html_class})
widget=forms.widgets.TextInput(
attrs={'class': theme.form_element_html_class}
)
)
choices = forms.CharField(
label = _("Choices"),
label=_("Choices"),
required=False,
help_text = _("Enter single values/pairs per line. Example:<code><br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;1<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;2<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;alpha, Alpha<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;beta, Beta<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;omega"
"</code><br/>"
"It finally transforms into the following HTML code:<code><br/>"
'&nbsp;&nbsp;&nbsp;&nbsp;&lt;select id="id_NAME_OF_THE_ELEMENT" name="NAME_OF_THE_ELEMENT"&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value="1"&gt;1&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value="2"&gt;2&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value="alpha"&gt;Alpha&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value="beta"&gt;Beta&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;option value="omega"&gt;omega&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;'
"</code>"),
widget=forms.widgets.Textarea(attrs={'class': theme.form_element_html_class})
help_text=_("Enter single values/pairs per line. Example:<code><br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;1<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;2<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;alpha, Alpha<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;beta, Beta<br/>"
"&nbsp;&nbsp;&nbsp;&nbsp;omega"
"</code><br/>"
"It finally transforms into the following HTML "
"code:<code><br/>"
'&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;select id="id_NAME_OF_THE_ELEMENT" '
'name="NAME_OF_THE_ELEMENT"&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;option value="1"&gt;1&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;option value="2"&gt;2&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;option value="alpha"&gt;Alpha&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;option value="beta"&gt;Beta&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
'&lt;option value="omega"&gt;omega&lt;/option&gt;<br/>'
'&nbsp;&nbsp;&nbsp;&nbsp;&lt;/select&gt;'
"</code>"),
widget=forms.widgets.Textarea(
attrs={'class': theme.form_element_html_class}
)
)
help_text = forms.CharField(
label=_("Help text"),
required=False,
widget=forms.widgets.Textarea(attrs={'class': theme.form_element_html_class})
widget=forms.widgets.Textarea(
attrs={'class': theme.form_element_html_class}
)
)
initial = forms.CharField(
label=_("Initial"),
required=False,
widget=forms.widgets.TextInput(attrs={'class': theme.form_element_html_class})
widget=forms.widgets.TextInput(
attrs={'class': theme.form_element_html_class}
)
)
required = forms.BooleanField(
label=_("Required"),
required=False,
widget=forms.widgets.CheckboxInput(attrs={'class': theme.form_element_checkbox_html_class})
widget=forms.widgets.CheckboxInput(
attrs={'class': theme.form_element_checkbox_html_class}
)
)
def clean_initial(self):
"""
Validating the initial value.
"""
return validate_initial_for_multiple_choices(self, 'choices', 'initial')
"""Validating the initial value."""
return validate_initial_for_multiple_choices(self,
'choices',
'initial')

View file

@ -1,13 +1,14 @@
__title__ = 'fobi.contrib.plugins.form_elements.fields.select_multiple.settings'
from fobi.helpers import validate_submit_value_as
from .conf import get_setting
__title__ = 'fobi.contrib.plugins.form_elements.fields.' \
'select_multiple.settings'
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
__copyright__ = '2014-2016 Artur Barseghyan'
__license__ = 'GPL 2.0/LGPL 2.1'
__all__ = ('SUBMIT_VALUE_AS',)
from fobi.helpers import validate_submit_value_as
from .conf import get_setting
SUBMIT_VALUE_AS = get_setting('SUBMIT_VALUE_AS')
validate_submit_value_as(SUBMIT_VALUE_AS)