Skip to content

Commit

Permalink
feat: generate calendars for league events
Browse files Browse the repository at this point in the history
  • Loading branch information
brownben committed Apr 10, 2024
1 parent ba2dd6e commit 76a5107
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"fastapi~=0.110.1",
"google-auth~=2.29.0",
"httpx~=0.27.0",
"ics~=0.7.2",
"piccolo==0.119.0",
"uvicorn~=0.29.0",
"requests~=2.31.0"
Expand Down
17 changes: 17 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ anyio==4.3.0
# via
# httpx
# starlette
arrow==1.3.0
# via ics
async-timeout==4.0.3
# via asyncpg
asyncpg==0.29.0
attrs==23.2.0
# via ics
black==24.3.0
# via piccolo
cachetools==5.3.3
Expand Down Expand Up @@ -43,6 +47,7 @@ h11==0.14.0
httpcore==1.0.5
# via httpx
httpx==0.27.0
ics==0.7.2
idna==3.6
# via
# anyio
Expand Down Expand Up @@ -74,9 +79,17 @@ pydantic==1.10.15
# via
# fastapi
# piccolo
python-dateutil==2.9.0.post0
# via
# arrow
# ics
requests==2.31.0
rsa==4.9
# via google-auth
six==1.16.0
# via
# ics
# python-dateutil
sniffio==1.3.1
# via
# anyio
Expand All @@ -85,6 +98,10 @@ starlette==0.37.2
# via fastapi
targ==0.4.0
# via piccolo
tatsu==5.12.0
# via ics
types-python-dateutil==2.9.0.20240316
# via arrow
typing-extensions==4.11.0
# via
# fastapi
Expand Down
44 changes: 44 additions & 0 deletions backend/src/routes/leagues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
from typing import Awaitable, Iterable

from fastapi import Depends, Path
from fastapi.responses import StreamingResponse
from fastapi.routing import APIRouter
from ics import Calendar
from ics import Event as CalendarEvent

from ..database import (
Competitors,
Expand Down Expand Up @@ -165,6 +168,47 @@ async def get_league_events(
return events


@router.get("/{league_name}/events/calendar")
async def get_league_events_calendar(
league_name: str = Path(
title="League Name",
description="Name of the league to get the results for",
example="Sprintelope 2021",
),
) -> StreamingResponse:
league, events = await asyncio.gather(
Leagues.get_by_name(league_name),
Events.get_by_league(league_name),
)

if not league:
raise HTTP_404(f"Couldn't find league with name `{league_name}`")

calendar = Calendar()

for event in events:
description = ""

if event.part_of:
description += f"Part of {event.part_of}\n"
if event.more_information:
description += f"\n{event.more_information}"

calendar_event = CalendarEvent(
name=f"{league.name} - {event.name}",
begin=event.date,
description=description,
location=event.name,
url=event.website,
organizer=event.organiser,
)
calendar_event.make_all_day()

calendar.events.add(calendar_event)

return StreamingResponse(calendar.serialize_iter(), media_type="text/calendar")


def get_results_for_event(
event: LeagueEvent, league_class: LeagueClass
) -> Awaitable[Iterable[Result]]:
Expand Down

0 comments on commit 76a5107

Please sign in to comment.