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 %} {% for s in filter.get_status_names %}
{{ s }} {{ s }}
{% endfor %} {% endfor %}
{{ filter.get_recurrence_filtering }}
{% else %} {% else %}
Filtrer Filtrer
{% endif %} {% endif %}

View File

@ -69,6 +69,8 @@ class TagCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
class EventFilter(django_filters.FilterSet): 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", exclude_tags = django_filters.MultipleChoiceFilter(label="Exclure les étiquettes",
choices=[(t, t) for t in Event.get_all_tags()], choices=[(t, t) for t in Event.get_all_tags()],
lookup_expr='icontains', lookup_expr='icontains',
@ -82,6 +84,9 @@ class EventFilter(django_filters.FilterSet):
field_name="tags", field_name="tags",
widget=TagCheckboxSelectMultiple) 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", category = django_filters.ModelMultipleChoiceFilter(label="Filtrer par catégories",
field_name="category__id", field_name="category__id",
@ -97,13 +102,17 @@ class EventFilter(django_filters.FilterSet):
class Meta: class Meta:
model = Event model = Event
fields = ["category", "tags", "exclude_tags", "status"] fields = ["category", "tags", "exclude_tags", "status", "recurrences"]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if not kwargs["request"].user.is_authenticated: if not kwargs["request"].user.is_authenticated:
self.form.fields.pop("status") 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): def get_url(self):
if isinstance(self.form.data, QueryDict): 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"]] return [dict(Event.STATUS.choices)[s] for s in self.form.cleaned_data["status"]]
else: else:
return [] 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): def is_active(self, only_categories=False):
if only_categories: if only_categories:
@ -139,7 +153,7 @@ class EventFilter(django_filters.FilterSet):
else: else:
if "status" in self.form.cleaned_data and len(self.form.cleaned_data["status"]) != 0: if "status" in self.form.cleaned_data and len(self.form.cleaned_data["status"]) != 0:
return True 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): def is_selected(self, cat):
return cat in self.form.cleaned_data["category"] return cat in self.form.cleaned_data["category"]