Commit 9ba2c185 authored by Fabio Manganiello's avatar Fabio Manganiello
Browse files

Better logic for timezone handling/conversion in calendar plugin

parent 0a3fd406
Pipeline #131 passed with stages
in 6 minutes and 3 seconds
......@@ -5,6 +5,7 @@
import datetime
import dateutil.parser
import requests
from typing import Union, Optional
from platypush.plugins import Plugin, action
from platypush.plugins.calendar import CalendarInterface
......@@ -29,7 +30,19 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
self.url = url
@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 {
'id': str(event.get('uid')) if event.get('uid') else None,
'kind': 'calendar#event',
......@@ -44,15 +57,15 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
'displayName': event.get('organizer').params['cn']
} if event.get('organizer') else None,
'created': event.get('created').dt.isoformat() if event.get('created') else None,
'updated': event.get('last-modified').dt.isoformat() if event.get('last-modified') else None,
'created': cls._convert_timestamp(event, 'created'),
'updated': cls._convert_timestamp(event, 'last-modified'),
'start': {
'dateTime': event.get('dtstart').dt.isoformat() if event.get('dtstart') else None,
'timeZone': 'UTC', # TODO Support multiple timezone IDs
'dateTime': cls._convert_timestamp(event, 'dtstart'),
'timeZone': 'UTC',
},
'end': {
'dateTime': event.get('dtend').dt.isoformat() if event.get('dtend') else None,
'dateTime': cls._convert_timestamp(event, 'dtend'),
'timeZone': 'UTC',
},
}
......@@ -81,9 +94,8 @@ class CalendarIcalPlugin(Plugin, CalendarInterface):
if (
event['status'] != 'cancelled'
and event['end']['dateTime']
and dateutil.parser.parse(event['end']['dateTime']).replace(tzinfo=pytz.timezone('UTC')) >=
datetime.datetime.now(pytz.timezone('UTC'))
and event['end'].get('dateTime')
and event['end']['dateTime'] >= datetime.datetime.now(pytz.timezone('UTC'))
and (
(only_participating
and event.get('responseStatus') in [None, 'accepted', 'tentative'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment