diff --git a/IndividualCalendar.py b/IndividualCalendar.py index 04e4f20..0f5bad2 100644 --- a/IndividualCalendar.py +++ b/IndividualCalendar.py @@ -176,6 +176,42 @@ def get_individual_calendars_using_batch(start_date, end_date, group_members, ac return list_of_responses +def filter_events(events, events_to_add): + """ + Modifies parameters events and events_to_add if there are overlaps between them. + For example: + OUT AM and OUT PM on same day -> OUT (all day). + OUT AM and OUT (all day) -> OUT (all day) + OUT PM and OUT (all day) -> OUT (all day) + + Args: + events (SimpleEvent list): Accumulation of events from various calendars + events_to_add (SimpleEvent list): Represent one single event from the calendar. + A multiday event is broken down into x single day events. Whereas a one day event will be one event. + """ + + for new_event in events_to_add: + for event in events: + if new_event.net_id == event.net_id and new_event.date.date() == event.date.date(): + new_event_subject_id = utils.subject_identifier(new_event.subject) + event_subject_id = utils.subject_identifier(event.subject) + + if new_event_subject_id > event_subject_id: + logger.debug(f"Need to remove {event} and add {new_event} since it is ALL DAY") + events.remove(event) + elif new_event_subject_id < event_subject_id: + logger.debug(f"{event} is already added to filtered list (ALL DAY). Remove {new_event} from events list") + events_to_add.remove(new_event) + elif new_event_subject_id == 1: + logger.debug(f"{new_event} is a duplicate event of {event}. Won't be adding {new_event}") + events_to_add.remove(new_event) + else: + logger.debug(f"Need to remove {event} and NOT add {new_event} and create an all day event instead") + events.remove(event) + events.append(SimpleEvent.create_all_day_event(event.net_id, event.date)) + events_to_add.remove(new_event) + return + def process_individual_calendars(calendar, start_date, end_date): """ Creates simple event objects using the the individual calendars @@ -192,7 +228,7 @@ def process_individual_calendars(calendar, start_date, end_date): """ - filtered_events = [] + events = [] for member in calendar['value']: net_id = member['scheduleId'].split('@')[0] @@ -200,11 +236,11 @@ def process_individual_calendars(calendar, start_date, end_date): for event in member['scheduleItems']: if event['status'] != EVENT_STATUS: continue - simple_events = SimpleEvent.create_event_for_individual_calendars(event, start_date, end_date, net_id) - filtered_events.extend(simple_events) + events_to_add = SimpleEvent.create_event_for_individual_calendars(event, start_date, end_date, net_id) + filter_events(events, events_to_add) + events.extend(events_to_add) except KeyError as e: logger.warning(f"Unable to find: " + net_id) - - #filtered_events = [] - return filtered_events + + return events diff --git a/SimpleEvent.py b/SimpleEvent.py index 3db86ef..c1a5662 100644 --- a/SimpleEvent.py +++ b/SimpleEvent.py @@ -124,6 +124,12 @@ def create_event_for_shared_calendar(cls, event, net_ids): return simple_event + + @classmethod + def create_all_day_event(cls, net_id, date): + return cls(net_id, net_id + " OUT", date) + + @staticmethod # get_event_subject assumes that start and end are on the same day, so it's just checking their times to create the subject def get_event_subject(start, end, net_id): diff --git a/utils.py b/utils.py index e815844..c146661 100644 --- a/utils.py +++ b/utils.py @@ -225,9 +225,15 @@ def connection_error_handler(message, response, access_token): raise ConnectionError(message) +def subject_identifier(subject): + """ + Give value to OUT AM, OUT PM, and OUT. This creates a hierarchy/priority scheme + that can used for filtering and comparison. + subject (string): the name of the event in format: [netID] OUT [None/AM/PM] + """ -#email_list = get_email_list('org_ici', 20) -#print(email_list) -#print(get_email_list_from_ldap('org_ici')) - + if "AM" in subject or "PM" in subject: + return 0 + else: + return 1