diff --git a/src/agenda_culturel/templates/agenda_culturel/filter-inc.html b/src/agenda_culturel/templates/agenda_culturel/filter-inc.html index b73321d..4bcfb65 100644 --- a/src/agenda_culturel/templates/agenda_culturel/filter-inc.html +++ b/src/agenda_culturel/templates/agenda_culturel/filter-inc.html @@ -18,6 +18,7 @@ {% for s in filter.get_status_names %} {{ s }} {% endfor %} + {{ filter.get_recurrence_filtering }} {% else %} Filtrer {% endif %} diff --git a/src/agenda_culturel/views.py b/src/agenda_culturel/views.py index 0b21416..e31eeda 100644 --- a/src/agenda_culturel/views.py +++ b/src/agenda_culturel/views.py @@ -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"]