Better logic for timezone handling/conversion in calendar plugin

This commit is contained in:
Fabio Manganiello 2022-01-05 13:31:07 +01:00
parent 0a3fd4065a
commit 9ba2c18595

View file

@ -5,6 +5,7 @@
import datetime import datetime
import dateutil.parser import dateutil.parser
import requests import requests
from typing import Union, Optional
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
from platypush.plugins.calendar import CalendarInterface from platypush.plugins.calendar import CalendarInterface
@ -29,7 +30,19 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
self.url = url self.url = url
@staticmethod @staticmethod
def _translate_event(event): def _convert_timestamp(event, attribute: str) -> datetime.datetime:
import pytz
t = event.get(attribute)
if not t:
return
return (
dateutil.parser.isoparse(t.dt.isoformat())
.replace(tzinfo=pytz.timezone('UTC'))
)
@classmethod
def _translate_event(cls, event):
return { return {
'id': str(event.get('uid')) if event.get('uid') else None, 'id': str(event.get('uid')) if event.get('uid') else None,
'kind': 'calendar#event', 'kind': 'calendar#event',
@ -44,15 +57,15 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
'displayName': event.get('organizer').params['cn'] 'displayName': event.get('organizer').params['cn']
} if event.get('organizer') else None, } if event.get('organizer') else None,
'created': event.get('created').dt.isoformat() if event.get('created') else None, 'created': cls._convert_timestamp(event, 'created'),
'updated': event.get('last-modified').dt.isoformat() if event.get('last-modified') else None, 'updated': cls._convert_timestamp(event, 'last-modified'),
'start': { 'start': {
'dateTime': event.get('dtstart').dt.isoformat() if event.get('dtstart') else None, 'dateTime': cls._convert_timestamp(event, 'dtstart'),
'timeZone': 'UTC', # TODO Support multiple timezone IDs 'timeZone': 'UTC',
}, },
'end': { 'end': {
'dateTime': event.get('dtend').dt.isoformat() if event.get('dtend') else None, 'dateTime': cls._convert_timestamp(event, 'dtend'),
'timeZone': 'UTC', 'timeZone': 'UTC',
}, },
} }
@ -81,9 +94,8 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
if ( if (
event['status'] != 'cancelled' event['status'] != 'cancelled'
and event['end']['dateTime'] and event['end'].get('dateTime')
and dateutil.parser.parse(event['end']['dateTime']).replace(tzinfo=pytz.timezone('UTC')) >= and event['end']['dateTime'] >= datetime.datetime.now(pytz.timezone('UTC'))
datetime.datetime.now(pytz.timezone('UTC'))
and ( and (
(only_participating (only_participating
and event.get('responseStatus') in [None, 'accepted', 'tentative']) and event.get('responseStatus') in [None, 'accepted', 'tentative'])