-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reorganise code and split it
- Loading branch information
Showing
16 changed files
with
278 additions
and
222 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
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,36 @@ | ||
from datetime import datetime | ||
from typing import Any, cast | ||
|
||
from icalendar import Event | ||
|
||
from calendar_connector.consts import EVENT_TYPE | ||
from calendar_connector.datetime_utils import get_current_timestamp | ||
from calendar_connector.event_utils.date import extract_event_dates | ||
from calendar_connector.event_utils.description import extract_event_description | ||
from calendar_connector.event_utils.location import extract_event_location | ||
from calendar_connector.event_utils.summary import extract_event_summary | ||
from calendar_connector.normalize import normalize | ||
|
||
|
||
def event_to_calendar_event(team_name: str, event_data: EVENT_TYPE) -> Event: | ||
event = Event() | ||
event.add("uid", str(event_data["id"]) + f"@sporteasy.net") | ||
extract_event_location(event_data, event) | ||
extract_event_dates(event_data, event) | ||
extract_event_summary(event_data, event, team_name) | ||
extract_event_description(event_data, event) | ||
|
||
event.add("class", "PUBLIC") | ||
current_timestamp = get_current_timestamp() | ||
event.add("sequence", current_timestamp) | ||
event.add("transp", "OPAQUE") | ||
|
||
# todo: change this if possible | ||
event.add("created", datetime(2020, 1, 1, 1, 1, 1)) | ||
event.add("last-modified", datetime(2020, 1, 1, 1, 1, 1)) | ||
|
||
category_name = normalize( | ||
cast(dict[str, str | Any], event_data["category"])["localized_name"] | ||
) | ||
|
||
return event |
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,21 @@ | ||
from datetime import datetime, timedelta | ||
from typing import cast | ||
|
||
from icalendar import Event | ||
|
||
from calendar_connector.consts import EVENT_TYPE, TIMEZONE | ||
|
||
|
||
def extract_event_dates(event_data: EVENT_TYPE, event: Event) -> None: | ||
start_date = datetime.fromisoformat(cast(str, event_data["start_at"])) | ||
start_date = start_date.astimezone(TIMEZONE) | ||
end_date_str = cast(str | None, event_data["end_at"]) | ||
if end_date_str is not None: | ||
end_date = datetime.fromisoformat(end_date_str) | ||
else: | ||
end_date = start_date + timedelta(hours=2) | ||
end_date = end_date.astimezone(TIMEZONE) | ||
|
||
event.add("dtstart", start_date) | ||
event.add("dtstamp", start_date) | ||
event.add("dtend", end_date) |
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,45 @@ | ||
from typing import cast | ||
|
||
from icalendar import Event | ||
|
||
from calendar_connector.consts import EVENT_TYPE, ORDER_PRESENT | ||
from calendar_connector.event_utils.score import extract_scores | ||
|
||
|
||
def _extract_attendee_description(event_data: EVENT_TYPE) -> str: | ||
attendance_groups = cast( | ||
list[dict[str, int | str]] | None, event_data["attendance_groups"] | ||
) | ||
attendee = "" | ||
if attendance_groups is not None: | ||
attendance_group_list: list[tuple[int, str, int]] = [] | ||
for ppc in attendance_groups: | ||
slug_sort_value = ORDER_PRESENT.get(cast(str, ppc["slug_name"]), 0) | ||
attendance_group_list.append( | ||
( | ||
slug_sort_value, | ||
cast(str, ppc["localized_name"]), | ||
cast(int, ppc["count"]), | ||
) | ||
) | ||
attendance_group_list.sort(reverse=True) | ||
|
||
attendee = ", ".join( | ||
[ | ||
f"{localized_name}: {count}" | ||
for _, localized_name, count in attendance_group_list | ||
] | ||
) | ||
|
||
return attendee | ||
|
||
|
||
def extract_event_description(event_data: EVENT_TYPE, event: Event) -> None: | ||
description = "" | ||
score = extract_scores(event_data) | ||
if score is not None: | ||
description += f"{score}\n" | ||
attendee = _extract_attendee_description(event_data) | ||
description += attendee | ||
|
||
event.add("description", description.strip()) |
Oops, something went wrong.