0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Implemented date range filter

This commit is contained in:
Karl Hobley 2020-03-20 14:57:49 +00:00 committed by Matt Westcott
parent 5c5809fffc
commit a5cea048ff
3 changed files with 32 additions and 1 deletions

View File

@ -1,9 +1,12 @@
import django_filters
from django import forms
from django.utils.translation import gettext_lazy as _
from django_filters.widgets import SuffixedMultiWidget
from wagtail.core.models import Page
from .widgets import AdminDateInput
class ButtonSelect(forms.Select):
input_type = 'hidden'
@ -11,6 +14,20 @@ class ButtonSelect(forms.Select):
option_template_name = 'wagtailadmin/widgets/button_select_option.html'
class DateRangePickerWidget(SuffixedMultiWidget):
template_name = 'wagtailadmin/widgets/daterange_input.html'
suffixes = ['after', 'before']
def __init__(self, attrs=None):
widgets = (AdminDateInput(attrs={'placeholder': _("Date from")}), AdminDateInput(attrs={'placeholder': _("Date to")}))
super().__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.start, value.stop]
return [None, None]
class WagtailFilterSet(django_filters.FilterSet):
@classmethod
@ -21,11 +38,18 @@ class WagtailFilterSet(django_filters.FilterSet):
params.setdefault('widget', ButtonSelect)
params.setdefault('empty_label', _("All"))
elif filter_class in [django_filters.DateFilter, django_filters.DateTimeFilter]:
params.setdefault('widget', AdminDateInput)
elif filter_class == django_filters.DateFromToRangeFilter:
params.setdefault('widget', DateRangePickerWidget)
return filter_class, params
class LockedPagesReportFilterSet(WagtailFilterSet):
locked_at = django_filters.DateFromToRangeFilter(widget=DateRangePickerWidget)
class Meta:
model = Page
fields = ['title', 'locked_by', 'locked_at']
fields = ['locked_by', 'locked_at']

View File

@ -50,4 +50,10 @@
{% block extra_css %}
<link rel="stylesheet" href="{% versioned_static 'wagtailadmin/css/layouts/report.css' %}" type="text/css" />
{{ filters.form.media.css }}
{% endblock %}
{% block extra_js %}
{{ filters.form.media.js }}
{% endblock %}

View File

@ -0,0 +1 @@
{% for widget in widget.subwidgets %}{% include widget.template_name %}{% endfor %}