From e0c860c8c8c6070d42a9f9a74bdc62c2cc08f2b4 Mon Sep 17 00:00:00 2001 From: JoeyStk Date: Thu, 14 Nov 2024 17:44:36 +0100 Subject: [PATCH] Add filter for external calendar --- integreat_cms/cms/constants/calendar.py | 30 +++++++++++++++++++ .../cms/forms/events/event_filter_form.py | 27 +++++++++++++++-- .../templates/events/_event_filter_form.html | 6 ++++ integreat_cms/locale/de/LC_MESSAGES/django.po | 12 ++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 integreat_cms/cms/constants/calendar.py diff --git a/integreat_cms/cms/constants/calendar.py b/integreat_cms/cms/constants/calendar.py new file mode 100644 index 0000000000..4ad4d72577 --- /dev/null +++ b/integreat_cms/cms/constants/calendar.py @@ -0,0 +1,30 @@ +""" +This module contains all string representations of calendar filter options, used by :class:`~integreat_cms.cms.forms.events.event_filter_form.EventFilterForm` and +:class:`~integreat_cms.cms.views.events.event_list_view.EventListView`: + +The module also contains a constant :const:`~integreat_cms.cms.constants.calendar.DATATYPE` which contains the type of the constant values linked to the strings and is used for correctly +instantiating :class:`django.forms.TypedMultipleChoiceField` instances in :class:`~integreat_cms.cms.forms.events.event_filter_form.EventFilterForm`. +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +from django.utils.translation import gettext_lazy as _ + +if TYPE_CHECKING: + from typing import Final + + from django.utils.functional import Promise + +DATATYPE: Final = int + +EVENT_NOT_FROM_EXTERNAL_CALENDAR: Final = 1 +#: Events that are not recurring, i.e. take place only once +EVENT_FROM_EXTERNAL_CALENDAR: Final = 2 + +#: Choices to use these constants in a database field +CHOICES: Final[list[tuple[int, Promise]]] = [ + (EVENT_NOT_FROM_EXTERNAL_CALENDAR, _("Event not from an external calendar")), + (EVENT_FROM_EXTERNAL_CALENDAR, _("Event from an external calendar")), +] diff --git a/integreat_cms/cms/forms/events/event_filter_form.py b/integreat_cms/cms/forms/events/event_filter_form.py index 97ea8f495e..3979e5d096 100644 --- a/integreat_cms/cms/forms/events/event_filter_form.py +++ b/integreat_cms/cms/forms/events/event_filter_form.py @@ -10,12 +10,13 @@ from typing import TYPE_CHECKING from django import forms +from django.utils.translation import gettext_lazy as _ if TYPE_CHECKING: - from ..models import Region - from ..models.events.event import EventQuerySet + from ...models import Region + from ...models.events.event import EventQuerySet -from ...constants import all_day, events_time_range, recurrence +from ...constants import all_day, calendar, events_time_range, recurrence from ...models import EventTranslation from ..custom_filter_form import CustomFilterForm @@ -69,6 +70,15 @@ class EventFilterForm(CustomFilterForm): initial=[events_time_range.UPCOMING], required=False, ) + + imported_event = forms.TypedMultipleChoiceField( + widget=forms.CheckboxSelectMultiple(attrs={"class": "default-checked"}), + choices=calendar.CHOICES, + initial=[key for (key, val) in calendar.CHOICES], + coerce=calendar.DATATYPE, + required=False, + ) + poi_id = forms.IntegerField(widget=forms.HiddenInput, initial=-1, required=False) query = forms.CharField(required=False) @@ -148,6 +158,17 @@ def apply( elif recurrence.NOT_RECURRING in self.cleaned_data["recurring"]: # Only non-recurring events events = events.filter(recurrence_rule__isnull=True) + + if ( + calendar.EVENT_NOT_FROM_EXTERNAL_CALENDAR + in self.cleaned_data["imported_event"] + ): + events = events.filter(external_calendar__isnull=True) + elif ( + calendar.EVENT_FROM_EXTERNAL_CALENDAR in self.cleaned_data["imported_event"] + ): + events = events.filter(external_calendar__isnull=False) + # Filter events by the search query if query := self.cleaned_data["query"]: event_ids = EventTranslation.search(region, language_slug, query).values( diff --git a/integreat_cms/cms/templates/events/_event_filter_form.html b/integreat_cms/cms/templates/events/_event_filter_form.html index bb5a70b37b..470c07400f 100644 --- a/integreat_cms/cms/templates/events/_event_filter_form.html +++ b/integreat_cms/cms/templates/events/_event_filter_form.html @@ -31,6 +31,12 @@ +
+ + {% render_field filter_form.imported_event|add_error_class:"border-red-500" %} +