def get_events(): logger.debug("Received call to Events controller from {0}".format(str(flask.request.remote_addr))) calendar = fetch_calendar('events') events = [] for event in calendar.walk('VEVENT'): start_datetime = event['dtstart'].dt end_datetime = event['dtend'].dt try: location = event['location'] except KeyError: # For some reason, the Google Calendar API does not include the LOCATION key on certain calendars (e.g. Holidays), thus raising a KeyError # when trying to retrieve the location from the calendar. So we must catch the error and assign the value an empty string if raised. location = "" # event['dtstart'].dt and event['dtend'].dt will return a date object and not a datetime object if the event is a full-day event. # Since a date object does not have a date() method, calling date() on an all-day event will fail. if isinstance(start_datetime, datetime.datetime): start_date = start_datetime.date() else: start_date = start_datetime if datetime.date.today() <= start_date: events.append( { 'summary' : event['summary'], 'description' : event['description'], 'location' : location, 'start' : str(start_datetime), 'end' : str(end_datetime)}) logger.info("Calendar request succeeded and yielded {0} events".format(len(events))) return flask.jsonify(results=events)
def get_holidays(): logger.debug("Received call to Holidays controller from {0}".format(str(flask.request.remote_addr))) if settings.getboolean('calendar', 'hide_holidays'): return flask.jsonify(results=[]) calendar = fetch_calendar('holidays') events = [] for event in calendar.walk('VEVENT'): if datetime.date.today() <= event['dtstart'].dt: events.append({ 'summary' : event['summary'], 'start' : str(event['dtstart'].dt), 'end' : str(event['dtend'].dt)}) logger.info("Holiday request succeeded and yielded {0} holidays".format(len(events))) return flask.jsonify(results=events)