diff --git a/src/api/app.py b/src/api/app.py index 22fc6fd..2eb35be 100644 --- a/src/api/app.py +++ b/src/api/app.py @@ -75,5 +75,5 @@ async def redirect_to_docs(request: Request): return RedirectResponse(url=request.url_for("swagger_ui_html")) -for router in routers: - app.include_router(router) +for _router in routers: + app.include_router(_router) diff --git a/src/api/bookings/routes.py b/src/api/bookings/routes.py index 3153bec..33c9235 100644 --- a/src/api/bookings/routes.py +++ b/src/api/bookings/routes.py @@ -1,8 +1,6 @@ import datetime from typing import Optional -from zlib import crc32 -import icalendar from fastapi import Query, Response from src.api.bookings import router @@ -110,47 +108,3 @@ async def get_bookings_for_week( booking_repository = Dependencies.get(AbstractBookingRepository) bookings = await booking_repository.get_bookings_for_week(start_of_week) return bookings - - -@router.get( - "/music-room.ics", - responses={ - 200: { - "description": "ICS file with schedule of the music room", - "content": {"text/calendar": {"schema": {"type": "string", "format": "binary"}}}, - }, - }, - response_class=Response, -) -async def get_music_room_ics(): - main_calendar = icalendar.Calendar( - prodid="-//one-zero-eight//InNoHassle Schedule", - version="2.0", - method="PUBLISH", - ) - main_calendar["x-wr-calname"] = "Music Room schedule from innohassle.ru" - main_calendar["x-wr-timezone"] = "Europe/Moscow" - main_calendar["x-wr-caldesc"] = "Generated by InNoHassle Schedule" - - booking_repository = Dependencies.get(AbstractBookingRepository) - from_date = datetime.date.today() - datetime.timedelta(days=14) - to_date = datetime.date.today() + datetime.timedelta(days=14) - bookings = await booking_repository.get_bookings(from_date, to_date) - dtstamp = icalendar.vDatetime(datetime.datetime.now()) - for booking in bookings: - string_to_hash = str(booking.id) - hash_ = crc32(string_to_hash.encode("utf-8")) - uid = "music-room-%x@innohassle.ru" % abs(hash_) - - vevent = icalendar.Event() - vevent.add("uid", uid) - vevent.add("dtstart", icalendar.vDatetime(booking.time_start)) - vevent.add("dtend", icalendar.vDatetime(booking.time_end)) - vevent.add("dtstamp", dtstamp) - vevent.add("location", "Music Room 020") - vevent.add("summary", f"booking @{booking.participant_alias}") - main_calendar.add_component(vevent) - - ical_bytes = main_calendar.to_ical() - - return Response(content=ical_bytes, media_type="text/calendar") diff --git a/src/api/root/__init__.py b/src/api/root/__init__.py new file mode 100644 index 0000000..6013cbd --- /dev/null +++ b/src/api/root/__init__.py @@ -0,0 +1,7 @@ +__all__ = ["router"] + +from fastapi import APIRouter + +router = APIRouter(prefix="", tags=["Root"]) + +import src.api.root.routes # noqa: E402, F401 diff --git a/src/api/root/routes.py b/src/api/root/routes.py new file mode 100644 index 0000000..95b949b --- /dev/null +++ b/src/api/root/routes.py @@ -0,0 +1,53 @@ +import datetime +from zlib import crc32 + +import icalendar +from fastapi import Response + +from src.api.dependencies import Dependencies +from src.api.root import router +from src.repositories.bookings.abc import AbstractBookingRepository + + +@router.get( + "/music-room.ics", + responses={ + 200: { + "description": "ICS file with schedule of the music room", + "content": {"text/calendar": {"schema": {"type": "string", "format": "binary"}}}, + }, + }, + response_class=Response, +) +async def get_music_room_ics(): + main_calendar = icalendar.Calendar( + prodid="-//one-zero-eight//InNoHassle Schedule", + version="2.0", + method="PUBLISH", + ) + main_calendar["x-wr-calname"] = "Music Room schedule from innohassle.ru" + main_calendar["x-wr-timezone"] = "Europe/Moscow" + main_calendar["x-wr-caldesc"] = "Generated by InNoHassle Schedule" + + booking_repository = Dependencies.get(AbstractBookingRepository) + from_date = datetime.date.today() - datetime.timedelta(days=14) + to_date = datetime.date.today() + datetime.timedelta(days=14) + bookings = await booking_repository.get_bookings(from_date, to_date) + dtstamp = icalendar.vDatetime(datetime.datetime.now()) + for booking in bookings: + string_to_hash = str(booking.id) + hash_ = crc32(string_to_hash.encode("utf-8")) + uid = "music-room-%x@innohassle.ru" % abs(hash_) + + vevent = icalendar.Event() + vevent.add("uid", uid) + vevent.add("dtstart", icalendar.vDatetime(booking.time_start)) + vevent.add("dtend", icalendar.vDatetime(booking.time_end)) + vevent.add("dtstamp", dtstamp) + vevent.add("location", "Music Room 020") + vevent.add("summary", f"booking @{booking.participant_alias}") + main_calendar.add_component(vevent) + + ical_bytes = main_calendar.to_ical() + + return Response(content=ical_bytes, media_type="text/calendar") diff --git a/src/api/routers.py b/src/api/routers.py index cb312ec..74c3bc9 100644 --- a/src/api/routers.py +++ b/src/api/routers.py @@ -1,7 +1,8 @@ from src.api.auth import router as router_auth from src.api.bookings import router as router_booking from src.api.participants import router as router_participants +from src.api.root import router as router_root -routers = [router_participants, router_booking, router_auth] +routers = [router_root, router_participants, router_booking, router_auth] __all__ = ["routers", *routers]