Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for external calendar #3295

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions integreat_cms/cms/constants/calendar.py
Original file line number Diff line number Diff line change
@@ -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")),
]
27 changes: 24 additions & 3 deletions integreat_cms/cms/forms/events/event_filter_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Comment on lines +162 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means the default behavior is that only non-imported events are shown, since both checkboxes are checkbed by default, but only the first case applies.

For me the expected behavior would be:

  • Both checked: No filter applied
  • One checked: Filter either for has external calendar or has no external calendar
  • None checked: I guess this would be invalid and treated as both checked, like for the other checkboxes?


# Filter events by the search query
if query := self.cleaned_data["query"]:
event_ids = EventTranslation.search(region, language_slug, query).values(
Expand Down
6 changes: 6 additions & 0 deletions integreat_cms/cms/templates/events/_event_filter_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
</div>
</div>
</div>
<div>
<label>
{% translate "Calendar" %}
</label>
{% render_field filter_form.imported_event|add_error_class:"border-red-500" %}
</div>
</div>
<div class="w-1/3 pr-6">
<label>
Expand Down
12 changes: 12 additions & 0 deletions integreat_cms/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ msgstr "PPT-Dokument"
msgid "PPTX document"
msgstr "PPTX-Dokument"

#: cms/constants/calendar.py
msgid "Event not from an external calendar"
msgstr "Veranstaltungen ohne externen Kalender"

#: cms/constants/calendar.py
msgid "Event from an external calendar"
msgstr "Veranstaltungen aus externen Kalender"

#: cms/constants/countries.py
msgid "Arabic"
msgstr "Arabisch"
Expand Down Expand Up @@ -5876,6 +5884,10 @@ msgstr "Zurück zur Startseite"
msgid "Time range"
msgstr "Zeitraum"

#: cms/templates/events/_event_filter_form.html
msgid "Calendar"
msgstr "Kalender"

#: cms/templates/events/_event_filter_form.html
#: cms/templates/events/event_list.html
msgid "Event location"
Expand Down
Loading