Skip to content
Snippets Groups Projects
Verified Commit d9fe318b authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

fix: create a timeslot with a cloned schedule for "ours-both"

parent fb845f32
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
import copy
from datetime import datetime, time, timedelta from datetime import datetime, time, timedelta
from typing import TypedDict from typing import TypedDict
...@@ -148,6 +149,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s ...@@ -148,6 +149,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s
to_create: list[TimeSlot] = [] to_create: list[TimeSlot] = []
to_update: list[TimeSlot] = [] to_update: list[TimeSlot] = []
to_delete: list[TimeSlot] = [] to_delete: list[TimeSlot] = []
to_instantiate: list[Schedule] = [] # only needed for the "ours-both" solution
errors = {} errors = {}
...@@ -249,7 +251,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s ...@@ -249,7 +251,7 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s
# - Create projected # - Create projected
# - Split existing into two # - Split existing into two
# - Set existing end time to projected start # - 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( to_create.append(
create_timeslot(timeslot["start"], timeslot["end"], new_schedule), create_timeslot(timeslot["start"], timeslot["end"], new_schedule),
) )
...@@ -258,9 +260,15 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s ...@@ -258,9 +260,15 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s
existing_ts.end = parse_datetime(timeslot["start"]) existing_ts.end = parse_datetime(timeslot["start"])
to_update.append(existing_ts) to_update.append(existing_ts)
to_create.append( cloned_schedule = copy.copy(existing_ts.schedule)
create_timeslot(timeslot["end"], existing["end"], new_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 # If there were any errors, don't make any db changes yet
# but add error messages and return already chosen solutions # but add error messages and return already chosen solutions
...@@ -319,6 +327,9 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s ...@@ -319,6 +327,9 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s
if last_date_is_unknown: if last_date_is_unknown:
new_schedule.last_date = None new_schedule.last_date = None
for cloned_schedule in to_instantiate:
cloned_schedule.save()
if to_create: if to_create:
new_schedule.save() new_schedule.save()
...@@ -326,8 +337,6 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s ...@@ -326,8 +337,6 @@ def resolve_conflicts(data: ScheduleCreateUpdateData, schedule_pk: int | None, s
timeslot.save(update_fields=["start", "end"]) timeslot.save(update_fields=["start", "end"])
for timeslot in to_create: for timeslot in to_create:
timeslot.schedule = new_schedule
# Reassign playlists # Reassign playlists
if "playlists" in data and timeslot.hash in data["playlists"]: if "playlists" in data and timeslot.hash in data["playlists"]:
timeslot.playlist_id = int(data["playlists"][timeslot.hash]) timeslot.playlist_id = int(data["playlists"][timeslot.hash])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment