reservations: Refactor out insert_or_update_calendar_event function
This commit is contained in:
parent
0e486babb7
commit
06fd819acf
@ -63,6 +63,45 @@ def insert_calendar_event(service, resource: Resource, reservation: Reservation)
|
|||||||
reservation.save()
|
reservation.save()
|
||||||
|
|
||||||
|
|
||||||
|
def insert_or_update_calendar_event(
|
||||||
|
service, resource: Resource, reservation: Reservation
|
||||||
|
):
|
||||||
|
if not reservation.google_calendar_event_id:
|
||||||
|
logger.info(
|
||||||
|
"Event in database has no Google Calendar event ID: inserting | %s",
|
||||||
|
reservation.google_calendar_event_id,
|
||||||
|
)
|
||||||
|
insert_calendar_event(service, resource, reservation)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# this event was in Google Calendar at some point (possibly for a different
|
||||||
|
# resource/calendar), but did not appear in list(). Try to update it, then
|
||||||
|
# fall back to insert
|
||||||
|
logger.info(
|
||||||
|
"Reservation with event id not in Google Calendar: trying update | %s",
|
||||||
|
reservation.google_calendar_event_id,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
event = (
|
||||||
|
service.events()
|
||||||
|
.get(
|
||||||
|
calendarId=resource.google_calendar,
|
||||||
|
eventId=reservation.google_calendar_event_id,
|
||||||
|
)
|
||||||
|
.execute()
|
||||||
|
)
|
||||||
|
update_calendar_event(service, resource, event, reservation)
|
||||||
|
except HttpError as error:
|
||||||
|
if error.status_code == HTTPStatus.NOT_FOUND:
|
||||||
|
logger.info(
|
||||||
|
"Event in database not in Google Calendar: inserting | %s",
|
||||||
|
reservation.google_calendar_event_id,
|
||||||
|
)
|
||||||
|
insert_calendar_event(service, resource, reservation)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def sync_resource_from_google_calendar(
|
def sync_resource_from_google_calendar(
|
||||||
service, resource: Resource, now: datetime
|
service, resource: Resource, now: datetime
|
||||||
) -> set[str]:
|
) -> set[str]:
|
||||||
@ -129,54 +168,31 @@ def sync_resource_from_google_calendar(
|
|||||||
return {event["id"] for event in events}
|
return {event["id"] for event in events}
|
||||||
|
|
||||||
|
|
||||||
|
def sync_reservation_from_database(service, reservation: Reservation):
|
||||||
|
for resource in reservation.resources.all():
|
||||||
|
insert_or_update_calendar_event(service, resource, reservation)
|
||||||
|
|
||||||
|
|
||||||
def sync_resource_from_database(
|
def sync_resource_from_database(
|
||||||
service, resource: Resource, now: datetime, existing_event_ids: set[str]
|
service, resource: Resource, now: datetime, existing_event_ids: set[str]
|
||||||
):
|
):
|
||||||
reservations = resource.reservation_set.filter(end__gt=now).select_subclasses()
|
reservations = (
|
||||||
|
resource.reservation_set.filter(end__gt=now)
|
||||||
|
# skip events we already pulled from Google Calendar during this sync
|
||||||
|
.exclude(google_calendar_event_id__in=existing_event_ids)
|
||||||
|
.select_subclasses()
|
||||||
|
)
|
||||||
# TODO: this could probably be more efficient?
|
# TODO: this could probably be more efficient?
|
||||||
for reservation in reservations:
|
for reservation in reservations:
|
||||||
if not reservation.google_calendar_event_id:
|
|
||||||
logger.info(
|
|
||||||
"Event in database has no Google Calendar event ID: inserting | %s",
|
|
||||||
reservation.google_calendar_event_id,
|
|
||||||
)
|
|
||||||
insert_calendar_event(service, resource, reservation)
|
|
||||||
|
|
||||||
# reservation has an event id, so check if we already handled it earlier
|
|
||||||
elif reservation.google_calendar_event_id not in existing_event_ids:
|
|
||||||
if isinstance(reservation, ExternalReservation):
|
if isinstance(reservation, ExternalReservation):
|
||||||
logger.info(
|
logger.info(
|
||||||
"External event in database did not exist in future of Google Calendar: deleting locally | %s",
|
"External event in database did not exist in future of Google Calendar: deleting locally | %s",
|
||||||
reservation.google_calendar_event_id,
|
reservation.google_calendar_event_id,
|
||||||
)
|
)
|
||||||
reservation.delete()
|
reservation.delete()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# this event was in Google Calendar at some point (possibly for a different
|
insert_or_update_calendar_event(service, resource, reservation)
|
||||||
# resource/calendar), but did not appear in list(). Try to update it, then
|
|
||||||
# fall back to insert
|
|
||||||
logger.info(
|
|
||||||
"Reservation with event id not in Google Calendar: trying update | %s",
|
|
||||||
reservation.google_calendar_event_id,
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
event = (
|
|
||||||
service.events()
|
|
||||||
.get(
|
|
||||||
calendarId=resource.google_calendar,
|
|
||||||
eventId=reservation.google_calendar_event_id,
|
|
||||||
)
|
|
||||||
.execute()
|
|
||||||
)
|
|
||||||
update_calendar_event(service, resource, event, reservation)
|
|
||||||
except HttpError as error:
|
|
||||||
if error.status_code == HTTPStatus.NOT_FOUND:
|
|
||||||
logger.info(
|
|
||||||
"Event in database not in Google Calendar: inserting | %s",
|
|
||||||
reservation.google_calendar_event_id,
|
|
||||||
)
|
|
||||||
insert_calendar_event(service, resource, reservation)
|
|
||||||
else:
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def sync_resource(service, resource: Resource, now: datetime):
|
def sync_resource(service, resource: Resource, now: datetime):
|
||||||
|
Loading…
Reference in New Issue
Block a user