mirror of
https://git.bolliret.ch/pcs/pcs-website
synced 2026-01-18 15:41:37 +01:00
Attempting to handle multi-day events on main site also. Tomorrow we will see how well that worked
This commit is contained in:
parent
d3094d8d53
commit
d44272d9b4
2 changed files with 57 additions and 6 deletions
|
|
@ -2,16 +2,67 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date, timedelta
|
||||||
from typing import Optional, NamedTuple
|
from typing import Optional, NamedTuple
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from copy import deepcopy
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from icalendar import Calendar
|
from icalendar import Calendar, Event
|
||||||
import recurring_ical_events
|
import recurring_ical_events
|
||||||
import locale
|
import locale
|
||||||
|
|
||||||
|
|
||||||
|
#This is duplicated code from calendar-fetcher.py
|
||||||
|
def split_multiday_events(events):
|
||||||
|
"""
|
||||||
|
Split multi-day events into separate daily events.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
events: Iterator of iCal events
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List of events where multi-day events are split into daily events
|
||||||
|
"""
|
||||||
|
split_events = []
|
||||||
|
|
||||||
|
for event in events:
|
||||||
|
# Get start and end dates
|
||||||
|
start = event.get('dtstart').dt
|
||||||
|
end = event.get('dtend').dt if event.get('dtend') else start
|
||||||
|
|
||||||
|
# Convert datetime to date if needed
|
||||||
|
if isinstance(start, datetime):
|
||||||
|
start = start.date()
|
||||||
|
if isinstance(end, datetime):
|
||||||
|
end = end.date()
|
||||||
|
|
||||||
|
# If it's a single day event or not a date/datetime, add as is
|
||||||
|
if not isinstance(start, date) or not isinstance(end, date) or start == end:
|
||||||
|
split_events.append(event)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# For multi-day events, create separate events for each day
|
||||||
|
current_date = start
|
||||||
|
while current_date < end:
|
||||||
|
# Create a copy of the original event
|
||||||
|
daily_event = Event()
|
||||||
|
for key in event:
|
||||||
|
daily_event[key] = deepcopy(event[key])
|
||||||
|
|
||||||
|
|
||||||
|
# Update the date for this instance
|
||||||
|
daily_event['dtstart'].dt = current_date
|
||||||
|
|
||||||
|
if 'dtend' in daily_event:
|
||||||
|
daily_event['dtend'].dt = current_date + timedelta(days=1)
|
||||||
|
|
||||||
|
split_events.append(daily_event)
|
||||||
|
current_date += timedelta(days=1)
|
||||||
|
return split_events
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class EventDetails:
|
class EventDetails:
|
||||||
weekday: str
|
weekday: str
|
||||||
|
|
@ -83,8 +134,8 @@ page.html
|
||||||
|
|
||||||
def get_next_event(self, calendar: Calendar) -> Optional[EventDetails]:
|
def get_next_event(self, calendar: Calendar) -> Optional[EventDetails]:
|
||||||
try:
|
try:
|
||||||
events = recurring_ical_events.of(calendar).after(datetime.now())
|
events = split_multiday_events(recurring_ical_events.of(calendar).after(datetime.now()))
|
||||||
event = next(events)
|
event = events[0]
|
||||||
start = event.get('dtstart').dt
|
start = event.get('dtstart').dt
|
||||||
|
|
||||||
return EventDetails(
|
return EventDetails(
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from icalendar import Calendar, Event
|
||||||
import locale
|
import locale
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
#This is duplicated code from calendar-fetcher-main.py
|
||||||
def split_multiday_events(events):
|
def split_multiday_events(events):
|
||||||
"""
|
"""
|
||||||
Split multi-day events into separate daily events.
|
Split multi-day events into separate daily events.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue