#37: Retrouver une émission passée ou à venir #48

Merged
thomas merged 3 commits from fix-1.0-37 into develop-1.0 2022-05-21 11:24:09 +00:00
2 changed files with 36 additions and 11 deletions
Showing only changes of commit ff398d8e7f - Show all commits

View File

@ -1,6 +1,7 @@
from django.db import models from django.db import models
from django.contrib.admin import filters from django.contrib.admin import filters
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils.http import urlencode
__all__ = ('DateFieldFilter', 'DateTimeField') __all__ = ('DateFieldFilter', 'DateTimeField')
@ -15,25 +16,41 @@ class DateFieldFilter(filters.FieldListFilter):
self.field_generic = '%s__' % field_path self.field_generic = '%s__' % field_path
self.date_params = {k: v for k, v in params.items() self.date_params = {k: v for k, v in params.items()
if k.startswith(self.field_generic)} if k.startswith(self.field_generic)}
self.links = ((_('Exact'), self.field_generic + 'exact'),
(_('Since'), self.field_generic + 'gte'), # links as: (label, param, input_type|None, value)
(_('Until'), self.field_generic + 'lte')) self.links = [(_('Exact'), self.field_generic + 'exact', self.input_type),
(_('Since'), self.field_generic + 'gte', self.input_type),
(_('Until'), self.field_generic + 'lte', self.input_type)]
if field.null:
self.links.insert(0, (_('None'), self.field_generic + 'isnull', None, '1'))
self.query_attrs = {k:v for k,v in request.GET.items()
if k not in self.date_params}
self.query_string = urlencode(self.query_attrs)
super().__init__(field, request, params, model, model_admin, field_path) super().__init__(field, request, params, model, model_admin, field_path)
def expected_parameters(self): def expected_parameters(self):
return [link[1] for link in self.links] return [link[1] for link in self.links]
def choices(self, changelist): def choices(self, changelist):
yield {'label': _('Any'),
'type': None,
'query_string': self.query_string}
for link in self.links: for link in self.links:
yield {'label': link[0], value = len(link) > 3 and link[3] or self.date_params.get(link[1])
'name': link[1], yield {
'value': self.date_params.get(link[1]), 'label': link[0], 'name': link[1], 'value': value,
'type': self.input_type,} 'type': link[2],
'query_attrs': self.query_attrs,
'query_string': urlencode({link[1]: value}) + '&' + self.query_string
if value else self.query_string,
}
class DateTimeFieldFilter(DateFieldFilter): class DateTimeFieldFilter(DateFieldFilter):
""" Display datetime input """ """ Display datetime input """
input_type = 'datetime' input_type = 'datetime-local'
filters.FieldListFilter.register( filters.FieldListFilter.register(

View File

@ -5,14 +5,22 @@
{% block content %} {% block content %}
<ul> <ul>
{% for choice in choices %} {% for choice in choices %}
<div> <li>
<form method="GET"> {% if choice.type %}
<form method="GET" action="?{{ choice.query_string }}"
onsubmit="return this.{{ choice.name }}.value ? true : false"">
<label for="filter-{{ choice.name }}">{{ choice.label }}: </label> <label for="filter-{{ choice.name }}">{{ choice.label }}: </label>
<input id="filter-{{ choice.name }}" type="{{ choice.type }}" name="{{ choice.name }}" <input id="filter-{{ choice.name }}" type="{{ choice.type }}" name="{{ choice.name }}"
value="{{ choice.value }}" {{ choice.extra }} /> value="{{ choice.value }}" {{ choice.extra }} />
{% for k, v in choice.query_attrs.items %}
<input type="hidden" name="{{k}}" value="{{v}}" />
{% endfor %}
<button class="button"><img src="{% static "admin/img/search.svg" %}" /></button> <button class="button"><img src="{% static "admin/img/search.svg" %}" /></button>
</form> </form>
</div> {% else %}
<a href="?{{ choice.query_string }}">{{ choice.label }}</a>
{% endif %}
</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endblock %} {% endblock %}