forked from rc/aircox
33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
from django.contrib.admin import filters
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
__all__ = ('DateFieldFilter',)
|
|
|
|
|
|
class DateFieldFilter(filters.FieldListFilter):
|
|
template = 'admin/aircox/filters/date_filter.html'
|
|
input_type = 'date'
|
|
|
|
def __init__(self, field, request, params, model, model_admin, field_path):
|
|
self.field_generic = '%s__' % field_path
|
|
self.date_params = {k: v for k, v in params.items()
|
|
if k.startswith(self.field_generic)}
|
|
|
|
self.links = ((_('Exact'), self.field_generic + 'exact'),
|
|
(_('Since'), self.field_generic + 'gte'),
|
|
(_('Until'), self.field_generic + 'lte'))
|
|
super().__init__(field, request, params, model, model_admin, field_path)
|
|
|
|
def expected_parameters(self):
|
|
return [link[1] for link in self.links]
|
|
|
|
def choices(self, changelist):
|
|
for link in self.links:
|
|
yield {'label': link[0],
|
|
'name': link[1],
|
|
'value': self.date_params.get(link[1]),
|
|
'type': self.input_type,}
|
|
|
|
|