feat: enhance calendar ICS generation with event type filtering
All checks were successful
CI / lint-and-test (push) Successful in 22s

- Added support for filtering calendar events by type in the ICS generation API endpoint, allowing users to specify whether to include only duty shifts or all event types (duty, unavailable, vacation).
- Updated the `get_duties_for_user` function to accept an optional `event_types` parameter, enabling more flexible data retrieval based on user preferences.
- Enhanced unit tests to cover the new event type filtering functionality, ensuring correct behavior and reliability of the ICS generation process.
This commit is contained in:
2026-02-20 17:47:52 +03:00
parent e25eb7be2f
commit aa89494bd5
12 changed files with 160 additions and 30 deletions

View File

@@ -3,6 +3,7 @@
import logging
import re
from datetime import date, timedelta
from typing import Literal
import duty_teller.config as config
from fastapi import Depends, FastAPI, Request
@@ -91,16 +92,20 @@ def list_calendar_events(
"/api/calendar/ical/{token}.ics",
summary="Personal calendar ICS",
description=(
"Returns an ICS calendar with only the subscribing user's duties. "
"Returns an ICS calendar with the subscribing user's events. "
"By default only duty shifts are included; use query parameter events=all "
"for all event types (duty, unavailable, vacation). "
"No Telegram auth; access is by secret token in the URL."
),
)
def get_personal_calendar_ical(
token: str,
events: Literal["duty", "all"] = "duty",
session: Session = Depends(get_db_session),
) -> Response:
"""
Return ICS calendar with only the subscribing user's duties.
Return ICS calendar with the subscribing user's events.
Default: only duty shifts. Use ?events=all for duty, unavailable, vacation.
No Telegram auth; access is by secret token in the URL.
"""
if not _is_valid_calendar_token(token):
@@ -111,8 +116,9 @@ def get_personal_calendar_ical(
today = date.today()
from_date = (today - timedelta(days=365)).strftime("%Y-%m-%d")
to_date = (today + timedelta(days=365 * 2)).strftime("%Y-%m-%d")
event_types = ["duty"] if events == "duty" else None
duties_with_name = get_duties_for_user(
session, user.id, from_date=from_date, to_date=to_date
session, user.id, from_date=from_date, to_date=to_date, event_types=event_types
)
ics_bytes = build_personal_ics(duties_with_name)
return Response(