From d9fe318bdc9a3c2cc7a961eff2db49e7ffd4ad71 Mon Sep 17 00:00:00 2001 From: Ernesto Rico Schmidt <ernesto@helsinki.at> Date: Wed, 15 May 2024 16:02:23 -0400 Subject: [PATCH] fix: create a timeslot with a cloned schedule for "ours-both" --- program/services.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/program/services.py b/program/services.py index 9ab08144..3431480e 100644 --- a/program/services.py +++ b/program/services.py @@ -17,6 +17,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # +import copy from datetime import datetime, time, timedelta from typing import TypedDict @@ -148,6 +149,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s to_create: list[TimeSlot] = [] to_update: list[TimeSlot] = [] to_delete: list[TimeSlot] = [] + to_instantiate: list[Schedule] = [] # only needed for the "ours-both" solution errors = {} @@ -249,7 +251,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s # - Create projected # - Split existing into two # - Set existing end time to projected start - # - Create another one with start = projected end and end = existing end + # - Clone the existing with start = projected end and end = existing end to_create.append( create_timeslot(timeslot["start"], timeslot["end"], new_schedule), ) @@ -258,9 +260,15 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s existing_ts.end = parse_datetime(timeslot["start"]) to_update.append(existing_ts) - to_create.append( - create_timeslot(timeslot["end"], existing["end"], new_schedule), - ) + cloned_schedule = copy.copy(existing_ts.schedule) + + cloned_schedule.pk = None + cloned_schedule.start_time = parse_datetime(timeslot["end"]) + cloned_schedule.end_time = parse_datetime(existing["end"]) + + # keep track of the schedule to instantiate it before creating a timeslot if needed + to_instantiate.append(cloned_schedule) + to_create.append(create_timeslot(timeslot["end"], existing["end"], cloned_schedule)) # If there were any errors, don't make any db changes yet # but add error messages and return already chosen solutions @@ -319,6 +327,9 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s if last_date_is_unknown: new_schedule.last_date = None + for cloned_schedule in to_instantiate: + cloned_schedule.save() + if to_create: new_schedule.save() @@ -326,8 +337,6 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s timeslot.save(update_fields=["start", "end"]) for timeslot in to_create: - timeslot.schedule = new_schedule - # Reassign playlists if "playlists" in data and timeslot.hash in data["playlists"]: timeslot.playlist_id = int(data["playlists"][timeslot.hash]) -- GitLab