From 35c55091af19e7f34f4262ecfe06052ce2d49d5d Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 29 Apr 2020 23:47:12 +0100 Subject: [PATCH] Move button select widgets to wagtail.admin.widgets --- wagtail/admin/filters.py | 45 +------------------------- wagtail/admin/widgets/__init__.py | 1 + wagtail/admin/widgets/button_select.py | 44 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 wagtail/admin/widgets/button_select.py diff --git a/wagtail/admin/filters.py b/wagtail/admin/filters.py index a713be6c15..127bf2eadf 100644 --- a/wagtail/admin/filters.py +++ b/wagtail/admin/filters.py @@ -4,52 +4,9 @@ from django.utils.translation import gettext_lazy as _ from django_filters.widgets import SuffixedMultiWidget from wagtail.admin.staticfiles import versioned_static +from wagtail.admin.widgets import AdminDateInput, BooleanButtonSelect, ButtonSelect from wagtail.core.models import Page, Task, TaskState, Workflow, WorkflowState -from .widgets import AdminDateInput - - -class ButtonSelect(forms.Select): - """ - A select widget for fields with choices. Displays as a list of buttons. - """ - input_type = 'hidden' - template_name = 'wagtailadmin/widgets/button_select.html' - option_template_name = 'wagtailadmin/widgets/button_select_option.html' - - -class BooleanButtonSelect(ButtonSelect): - """ - A select widget for boolean fields. Displays as three buttons. "All", "Yes" and "No". - """ - def __init__(self, attrs=None): - choices = ( - ('', _("All")), - ('true', _("Yes")), - ('false', _("No")), - ) - super().__init__(attrs, choices) - - def format_value(self, value): - try: - return { - True: ['true'], False: ['false'], - 'true': ['true'], 'false': ['false'], - }[value] - except KeyError: - return '' - - def value_from_datadict(self, data, files, name): - value = data.get(name) - return { - True: True, - 'True': True, - 'False': False, - False: False, - 'true': True, - 'false': False, - }.get(value) - class DateRangePickerWidget(SuffixedMultiWidget): """ diff --git a/wagtail/admin/widgets/__init__.py b/wagtail/admin/widgets/__init__.py index 5a50e10b6d..75a0e1e080 100644 --- a/wagtail/admin/widgets/__init__.py +++ b/wagtail/admin/widgets/__init__.py @@ -1,5 +1,6 @@ from wagtail.admin.widgets.auto_height_text import * # NOQA from wagtail.admin.widgets.button import * # NOQA +from wagtail.admin.widgets.button_select import * # NOQA from wagtail.admin.widgets.chooser import * # NOQA from wagtail.admin.widgets.datetime import * # NOQA from wagtail.admin.widgets.tags import * # NOQA diff --git a/wagtail/admin/widgets/button_select.py b/wagtail/admin/widgets/button_select.py new file mode 100644 index 0000000000..63fff44d04 --- /dev/null +++ b/wagtail/admin/widgets/button_select.py @@ -0,0 +1,44 @@ +from django import forms +from django.utils.translation import gettext_lazy as _ + + +class ButtonSelect(forms.Select): + """ + A select widget for fields with choices. Displays as a list of buttons. + """ + input_type = 'hidden' + template_name = 'wagtailadmin/widgets/button_select.html' + option_template_name = 'wagtailadmin/widgets/button_select_option.html' + + +class BooleanButtonSelect(ButtonSelect): + """ + A select widget for boolean fields. Displays as three buttons. "All", "Yes" and "No". + """ + def __init__(self, attrs=None): + choices = ( + ('', _("All")), + ('true', _("Yes")), + ('false', _("No")), + ) + super().__init__(attrs, choices) + + def format_value(self, value): + try: + return { + True: ['true'], False: ['false'], + 'true': ['true'], 'false': ['false'], + }[value] + except KeyError: + return '' + + def value_from_datadict(self, data, files, name): + value = data.get(name) + return { + True: True, + 'True': True, + 'False': False, + False: False, + 'true': True, + 'false': False, + }.get(value)