Skip to content

Commit

Permalink
Fix multiday event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PhongT16 committed Nov 1, 2023
1 parent d80fd86 commit 364bac2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
11 changes: 10 additions & 1 deletion IndividualCalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,22 @@ def process_individual_calendars(calendar, start_date, end_date):
filtered_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)

if (net_id == "pttran3"):
print(event)
for a in simple_events:
print(a)
print("-----------------------------------------")


except KeyError as e:
logger.warning(f"Unable to find: " + net_id)

return filtered_events
57 changes: 52 additions & 5 deletions SimpleEvent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from dataclasses import dataclass
from datetime import datetime
from datetime import time
from datetime import timedelta
import utils


# If a multiday event starts at 12:00 AM on a Monday and ends at 5PM on a Wednesday. Then only two events will be created:
# ALL-DAY for Monday and Tuesday, but no events for Wednesday
# If a multiday event starts at 11:00 AM on a Monday and ends at 5PM on a Wednesday. Then only three events will be created:
# ALL-DAY for Tuesday, OUT AM for Monday and OUT PM for Wednesday
configs = utils.get_configurations()
# AM_config = configs['AM_config']
# PM_config = configs['PM_config']
Expand Down Expand Up @@ -49,25 +53,68 @@ def create_event_for_individual_calendars(cls, event, start_date, end_date, net_
# 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
# Automatically All Day
dates_interval = end - start

if (net_id == "pttran3"):
print(f"dates_interval: {dates_interval}")

for i in range(dates_interval.days + 1): # The plus accounts for the last day of the multiday event. Even if it's just one All-Day
add_on = 0
remaining_hours = dates_interval.seconds // 3600

if (remaining_hours!= 0):
print("first add")
add_on = add_on + 1

# if (start.time() >= )

midnight_start_time = time(0, 0, 0, 0)

if (net_id == "pttran3"):
print(f"midnight_start_time: {midnight_start_time}")
print(f"start time of event: {start.time()}")


if (start.time() != midnight_start_time and end.time() <= start.time()):
print("second add")
add_on = add_on + 1

if (net_id == "pttran3"):
print(f"Going over {dates_interval.days + add_on} days")


for i in range(dates_interval.days + add_on): # The plus accounts for the last day of the multiday event. Even if it's just one All-Day

#new_date = start + timedelta(days=i)

# new_start and new_end are just changing the time
new_start = start + timedelta(days=i)
new_end = start + timedelta(days=i)
new_end = end + timedelta(days=i)
new_end = new_end.replace(year=new_start.year, month=new_start.month, day=new_start.day)
if (net_id == "pttran3"):
print(f"org new start: {new_start}")
print(f"org new end: {new_end}")


# Adjust the time so that we can create an accurate subject for the split up event
# Manipulating the time for the simple event here

if (i == 0):
#print("here 1")
new_end = new_end.replace(hour=23,minute=59,second=59)
elif (i == dates_interval.days):
elif (i == dates_interval.days + add_on - 1):
#print("here 2")
new_start = new_start.replace(hour=0,minute=0,second=0)
if (new_end.time() == midnight_start_time):
new_end = new_end.replace(hour=23,minute=59,second=59)
else:
#print("here 3")
new_start = new_start.replace(hour=0,minute=0,second=0)
new_end = new_end.replace(hour=23,minute=59,second=59)

if (net_id == "pttran3"):
print(f"new start: {new_start}")
print(f"new end: {new_end}")
print("++++++++++++++++++++++++++++++++++++++++++++")

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))

Expand Down Expand Up @@ -186,7 +233,7 @@ def is_AM(start, end):
return False

if start_time < start_of_workday:
start_time= start_of_workday
start_time = start_of_workday

if end_time > start_of_lunch:
end_time = start_of_lunch
Expand Down
1 change: 1 addition & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,5 @@ def connection_error_handler(message, response, access_token):

#email_list = get_email_list('org_ici', 20)
#print(email_list)
#print(get_email_list_from_ldap('org_ici'))

0 comments on commit 364bac2

Please sign in to comment.