diff --git a/IndividualCalendar.py b/IndividualCalendar.py index 04e4f20..4641ef1 100644 --- a/IndividualCalendar.py +++ b/IndividualCalendar.py @@ -176,6 +176,37 @@ def get_individual_calendars_using_batch(start_date, end_date, group_members, ac return list_of_responses +def filter(events): + """ + Removes duplicates in the list of events + + Args: + events (SimpleEvent list): contains events extracted from individual calendars + + Returns: + SimpleEvent list: a filtered list of events + """ + + filtered_events = [] + events.sort() + event_to_add = events[0] + + for event in events: + if event_to_add.net_id == event.net_id and event_to_add.date.date() == event.date.date(): + event_subject_id = utils.subject_identifier(event.subject) + event_to_add_subject_id = utils.subject_identifier(event_to_add.subject) + + if event_subject_id > event_to_add_subject_id: + event_to_add = event + elif ("OUT AM" in event.subject and "OUT PM" in event_to_add.subject) or ("OUT PM" in event.subject and "OUT AM" in event_to_add.subject): + event_to_add = SimpleEvent.create_all_day_event(event_to_add.net_id, event_to_add.date) + else: + filtered_events.append(event_to_add) + event_to_add = event + + filtered_events.append(event_to_add) + return filtered_events + def process_individual_calendars(calendar, start_date, end_date): """ Creates simple event objects using the the individual calendars @@ -185,26 +216,22 @@ def process_individual_calendars(calendar, start_date, end_date): calendar (json): json object of the events within/overlap between a specified start and end date for indvidual calendars start_date (datetime): the start date of timeframe being updated - end_date (dateime): the end date of timeframe being updated + end_date (datetime): the end date of timeframe being updated Returns: list: A list of SimpleEvent objects """ - filtered_events = [] + events = [] for member in calendar['value']: net_id = member['scheduleId'].split('@')[0] - try: 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) + events.extend(events_to_add) except KeyError as e: logger.warning(f"Unable to find: " + net_id) - - #filtered_events = [] - return filtered_events + + return filter(events) diff --git a/SimpleEvent.py b/SimpleEvent.py index 3db86ef..33050e0 100644 --- a/SimpleEvent.py +++ b/SimpleEvent.py @@ -17,11 +17,12 @@ end_of_lunch = configs['end_of_lunch'] duration = configs['duration'] -@dataclass +@dataclass(order=True) class SimpleEvent: net_id : str - subject : str # Our own formatted subject "[netID] [OUT/OUT AM/OUT PM]" date : datetime + subject : str # Our own formatted subject "[netID] [OUT/OUT AM/OUT PM]" + # date : datetime # Returns a list of Simple Events # The list will return 1 item if the event is a one day event @@ -47,7 +48,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_ if start.date() == end.date(): if SimpleEvent.is_event_valid(start_date, end_date, start, end): - return [cls(net_id, SimpleEvent.get_event_subject(start, end, net_id), start)] + return [cls(net_id, start, SimpleEvent.get_event_subject(start, end, net_id))] return [] # if an event goes in here, then it's all day because the start date and end date differ by one day so it has to be at least be 1 All Day @@ -90,7 +91,7 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_ new_end = new_end.replace(hour=23,minute=59,second=59) if SimpleEvent.is_event_valid(start_date, end_date, new_start, new_end): - events.append(cls(net_id, SimpleEvent.get_event_subject(new_start, new_end, net_id), new_start)) + events.append(cls(net_id, new_start, SimpleEvent.get_event_subject(new_start, new_end, net_id))) return events @@ -120,10 +121,16 @@ def create_event_for_shared_calendar(cls, event, net_ids): return if (len(event_identifier) == 2 and (event_identifier[1] == "OUT" or event_identifier[1] == "OUT AM" or event_identifier[1] == "OUT PM")): - simple_event = cls(event_identifier[0], subject, start) + simple_event = cls(event_identifier[0], start, subject) return simple_event + + @classmethod + def create_all_day_event(cls, net_id, date): + return cls(net_id, date, net_id + " OUT") + + @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..3089b25 100644 --- a/utils.py +++ b/utils.py @@ -225,9 +225,19 @@ 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] + + Returns: + 0 for OUT AM and OUT PM events + 1 for OUT events + """ -#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