- endpointy /workdays/add, is-workday, next, previous, between - /holidays, /countries (filtr eu), /markets, /health, /version - data svatku z knihovny holidays 0.109, vsech 27 zemi EU vcetne regionu - vychozi zeme CZ a cestina pres DEFAULT_COUNTRY a DEFAULT_LANGUAGE - vlastni vikend, extra_holidays a extra_workdays v requestu - testy logiky, API a testy proti realnym kalendarum - CLAUDE.md s povinnym overenim vysledku proti realite - dokumentace ve slozce documentation - .gitignore: vyjimka pro app/lib
70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
"""Holiday lists and the catalogue of supported calendars."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
from ..errors import OutOfRangeError
|
|
from ..models import (
|
|
CalendarDescription,
|
|
CalendarListResponse,
|
|
ErrorResponse,
|
|
HolidayItem,
|
|
HolidaysResponse,
|
|
)
|
|
from ..services.calendars import holidays_in_year, list_countries, list_markets
|
|
from .params import ResolvedCalendar, calendar_params, weekday_name
|
|
|
|
router = APIRouter(
|
|
tags=["holidays"],
|
|
responses={400: {"model": ErrorResponse, "description": "Neplatny vstup"}},
|
|
)
|
|
|
|
|
|
@router.get("/holidays", response_model=HolidaysResponse, summary="Svatky v roce")
|
|
def holiday_list(
|
|
calendar: Annotated[ResolvedCalendar, Depends(calendar_params)],
|
|
year: Annotated[int | None, Query(description="Rok, vychozi je aktualni.", examples=[2026])] = None,
|
|
) -> HolidaysResponse:
|
|
"""Svatky kalendare v jednom roce. Parametry extra_holidays a extra_workdays se tu neuplatni."""
|
|
info = calendar.info
|
|
wanted = year if year is not None else date.today().year
|
|
if not info.min_date.year <= wanted <= info.max_date.year:
|
|
raise OutOfRangeError(
|
|
f"Rok {wanted} je mimo rozsah kalendare {info.min_date.year} az {info.max_date.year}.",
|
|
{"year": wanted, "min": info.min_date.year, "max": info.max_date.year},
|
|
)
|
|
weekend = calendar.calendar.weekend
|
|
items = [
|
|
HolidayItem(
|
|
date=day,
|
|
weekday=weekday_name(day),
|
|
names=list(names),
|
|
is_weekend=day.weekday() in weekend,
|
|
)
|
|
for day, names in holidays_in_year(info.spec, wanted).items()
|
|
]
|
|
return HolidaysResponse(calendar=calendar.ref(), year=wanted, holidays=items)
|
|
|
|
|
|
def _filtered(items: tuple[dict, ...], eu: bool) -> CalendarListResponse:
|
|
selected = [item for item in items if item["eu"]] if eu else list(items)
|
|
return CalendarListResponse(items=[CalendarDescription.model_validate(item) for item in selected])
|
|
|
|
|
|
@router.get("/countries", response_model=CalendarListResponse, summary="Podporovane zeme")
|
|
def countries(
|
|
eu: Annotated[bool, Query(description="Jen clenske staty EU.")] = False,
|
|
) -> CalendarListResponse:
|
|
"""Zeme s regiony, kategoriemi svatku, jazyky a rozsahem let, ktere data pokryvaji."""
|
|
return _filtered(list_countries(), eu)
|
|
|
|
|
|
@router.get("/markets", response_model=CalendarListResponse, summary="Podporovane financni kalendare")
|
|
def markets() -> CalendarListResponse:
|
|
"""Financni kalendare, napriklad XECB (TARGET2) nebo burzy."""
|
|
return _filtered(list_markets(), eu=False)
|