-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move
/music-room.ics
endpoint
- Loading branch information
1 parent
fb6f47a
commit 16d7d85
Showing
5 changed files
with
64 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |