Skip to content

Commit

Permalink
chore: move /music-room.ics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dantetemplar committed Feb 13, 2024
1 parent fb6f47a commit 16d7d85
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
46 changes: 0 additions & 46 deletions src/api/bookings/routes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
7 changes: 7 additions & 0 deletions src/api/root/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__all__ = ["router"]

from fastapi import APIRouter

router = APIRouter(prefix="", tags=["Root"])

import src.api.root.routes # noqa: E402, F401
53 changes: 53 additions & 0 deletions src/api/root/routes.py
Original file line number Diff line number Diff line change
@@ -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")
3 changes: 2 additions & 1 deletion src/api/routers.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 16d7d85

Please sign in to comment.