On filtre sur la récurrence

Fix #106
This commit is contained in:
Jean-Marie Favreau 2024-04-24 15:06:46 +02:00
parent 8f9bd567af
commit 510ce9f69c
2 changed files with 17 additions and 2 deletions

View File

@ -18,6 +18,7 @@
{% for s in filter.get_status_names %}
{{ s }}
{% endfor %}
{{ filter.get_recurrence_filtering }}
{% else %}
Filtrer
{% endif %}

View File

@ -69,6 +69,8 @@ class TagCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
class EventFilter(django_filters.FilterSet):
RECURRENT_CHOICES = [("remove_recurrent", "Masquer les événements récurrents"), ("only_recurrent", "Montrer uniquement les événements récurrents")]
exclude_tags = django_filters.MultipleChoiceFilter(label="Exclure les étiquettes",
choices=[(t, t) for t in Event.get_all_tags()],
lookup_expr='icontains',
@ -82,6 +84,9 @@ class EventFilter(django_filters.FilterSet):
field_name="tags",
widget=TagCheckboxSelectMultiple)
recurrences = django_filters.ChoiceFilter(label="Filtrer par récurrence",
choices=RECURRENT_CHOICES,
method="filter_recurrences")
category = django_filters.ModelMultipleChoiceFilter(label="Filtrer par catégories",
field_name="category__id",
@ -97,13 +102,17 @@ class EventFilter(django_filters.FilterSet):
class Meta:
model = Event
fields = ["category", "tags", "exclude_tags", "status"]
fields = ["category", "tags", "exclude_tags", "status", "recurrences"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not kwargs["request"].user.is_authenticated:
self.form.fields.pop("status")
def filter_recurrences(self, queryset, name, value):
# construct the full lookup expression
lookup = '__'.join([name, 'isnull'])
return queryset.filter(**{lookup: value == "remove_recurrent"})
def get_url(self):
if isinstance(self.form.data, QueryDict):
@ -132,6 +141,11 @@ class EventFilter(django_filters.FilterSet):
return [dict(Event.STATUS.choices)[s] for s in self.form.cleaned_data["status"]]
else:
return []
def get_recurrence_filtering(self):
if "recurrences" in self.form.cleaned_data:
return dict(self.RECURRENT_CHOICES)[self.form.cleaned_data["recurrences"]]
else:
return ""
def is_active(self, only_categories=False):
if only_categories:
@ -139,7 +153,7 @@ class EventFilter(django_filters.FilterSet):
else:
if "status" in self.form.cleaned_data and len(self.form.cleaned_data["status"]) != 0:
return True
return len(self.form.cleaned_data["category"]) != 0 or len(self.form.cleaned_data["tags"]) != 0 or len(self.form.cleaned_data["exclude_tags"]) != 0
return len(self.form.cleaned_data["category"]) != 0 or len(self.form.cleaned_data["tags"]) != 0 or len(self.form.cleaned_data["exclude_tags"]) != 0 or len(self.form.cleaned_data["recurrences"]) != 0
def is_selected(self, cat):
return cat in self.form.cleaned_data["category"]