Merge pull request #477 from grafana/shift-notifications-for-web-schedule

Fix slack notification for web schedules
This commit is contained in:
Yulya Artyukhina 2022-09-08 12:03:41 +03:00 committed by GitHub
commit 84bcbb375f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 25 deletions

View file

@ -9,9 +9,9 @@ from django.utils import timezone
from apps.schedules.ical_events import ical_events
from apps.schedules.ical_utils import (
calculate_shift_diff,
event_start_end_all_day_with_respect_to_type,
get_icalendar_tz_or_utc,
get_usernames_from_ical_event,
ical_date_to_datetime,
is_icals_equal,
memoized_users_in_ical,
)
@ -35,12 +35,7 @@ def get_current_shifts_from_ical(calendar, schedule, min_priority=0):
usernames, priority = get_usernames_from_ical_event(event)
users = memoized_users_in_ical(tuple(usernames), schedule.organization)
if len(users) > 0:
event_start, start_all_day = ical_date_to_datetime(
event["DTSTART"].dt,
calendar_tz,
start=True,
)
event_end, end_all_day = ical_date_to_datetime(event["DTEND"].dt, calendar_tz, start=False)
event_start, event_end, all_day_event = event_start_end_all_day_with_respect_to_type(event, calendar_tz)
if event["UID"] in shifts:
existing_event = shifts[event["UID"]]
@ -50,7 +45,7 @@ def get_current_shifts_from_ical(calendar, schedule, min_priority=0):
"users": [u.pk for u in users],
"start": event_start,
"end": event_end,
"all_day": start_all_day,
"all_day": all_day_event,
"priority": priority + min_priority, # increase priority for overrides
"priority_increased_by": min_priority,
}
@ -70,19 +65,14 @@ def get_next_shifts_from_ical(calendar, schedule, min_priority=0, days_to_lookup
usernames, priority = get_usernames_from_ical_event(event)
users = memoized_users_in_ical(tuple(usernames), schedule.organization)
if len(users) > 0:
event_start, start_all_day = ical_date_to_datetime(
event["DTSTART"].dt,
calendar_tz,
start=True,
)
event_end, end_all_day = ical_date_to_datetime(event["DTEND"].dt, calendar_tz, start=False)
event_start, event_end, all_day_event = event_start_end_all_day_with_respect_to_type(event, calendar_tz)
# next_shifts are not stored in db so we can use User objects directly
shifts[f"{event_start.timestamp()}_{event['UID']}"] = {
"users": users,
"start": event_start,
"end": event_end,
"all_day": start_all_day,
"all_day": all_day_event,
"priority": priority + min_priority, # increase priority for overrides
"priority_increased_by": min_priority,
}

View file

@ -259,15 +259,10 @@ def list_of_empty_shifts_in_schedule(schedule, start_date, end_date):
checked_events.add(event_hash)
all_day = False
if type(event[ICAL_DATETIME_START].dt) == datetime.date:
# Convert all-day events start and end from date to datetime with calendar's tz
start, _ = ical_date_to_datetime(event["DTSTART"].dt, calendar_tz, start=True)
end, _ = ical_date_to_datetime(event["DTEND"].dt, calendar_tz, start=False)
all_day = True
else:
start = event[ICAL_DATETIME_START].dt.astimezone(pytz.UTC)
end = event[ICAL_DATETIME_END].dt.astimezone(pytz.UTC)
start, end, all_day = event_start_end_all_day_with_respect_to_type(event, calendar_tz)
if not all_day:
start = start.astimezone(pytz.UTC)
end = end.astimezone(pytz.UTC)
empty_shifts_per_calendar.append(
EmptyShift(
@ -578,7 +573,7 @@ def list_of_gaps_in_schedule(schedule, start_date, end_date):
end_datetime,
)
for event in events:
start, end = start_end_with_respect_to_all_day(event, calendar_tz)
start, end, _ = event_start_end_all_day_with_respect_to_type(event, calendar_tz)
intervals.append(DatetimeInterval(start, end))
return detect_gaps(intervals, start_datetime, end_datetime)
@ -615,6 +610,16 @@ def start_end_with_respect_to_all_day(event, calendar_tz):
return start, end
def event_start_end_all_day_with_respect_to_type(event, calendar_tz):
all_day = False
if type(event[ICAL_DATETIME_START].dt) == datetime.date:
start, end = start_end_with_respect_to_all_day(event, calendar_tz)
all_day = True
else:
start, end = ical_events.get_start_and_end_with_respect_to_event_type(event)
return start, end, all_day
def convert_windows_timezone_to_iana(tz_name):
"""
Conversion info taken from https://raw.githubusercontent.com/unicode-org/cldr/main/common/supplemental/windowsZones.xml