Skip to content

Commit

Permalink
Change current_start_time whenever remaining_time is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
luisa-beerboom committed Mar 13, 2024
1 parent 0a60513 commit d830206
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,29 @@ def validate_instance(self, instance: dict[str, Any]) -> None:
raise ActionException(field + " is not allowed to be set.")

def update_instance(self, instance: dict[str, Any]) -> dict[str, Any]:
if "remaining_time" in instance and "spoken_time" in instance:
raise ActionException(
"Cannot set remaining_time and spoken_time at the same time."
)
if "initial_time" in instance:
for field in ("current_start_time", "spoken_time", "remaining_time"):
if field in instance:
raise ActionException(
f"Cannot set initial_time and {field} at the same time."
)
instance["remaining_time"] = instance["initial_time"]
elif "remaining_time" in instance:
if "spoken_time" in instance:
raise ActionException(
"Cannot set remaining_time and spoken_time at the same time."
)
db_instance = self.datastore.get(
fqid_from_collection_and_id(self.model.collection, instance["id"]),
["current_start_time", "remaining_time"],
)

db_instance = self.datastore.get(
fqid_from_collection_and_id(self.model.collection, instance["id"]),
["current_start_time", "remaining_time", "remaining_time"],
)

if spoken_time := instance.pop("spoken_time", None):
instance["remaining_time"] = db_instance["remaining_time"] - spoken_time

if "remaining_time" in instance and "current_start_time" not in instance:
if db_instance.get("current_start_time") is not None:
instance["current_start_time"] = db_instance["current_start_time"] - (
instance["remaining_time"] - db_instance.get("remaining_time", 0)
)

if spoken_time := instance.pop("spoken_time", None):
db_instance = self.datastore.get(
fqid_from_collection_and_id(self.model.collection, instance["id"]),
["remaining_time"],
)
instance["remaining_time"] = db_instance["remaining_time"] - spoken_time
return instance
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,35 @@ def test_set_remaining_time_with_started_speech(self) -> None:
{"remaining_time": 700, "current_start_time": 50},
)

def test_set_initial_time_with_started_speech(self) -> None:
self.set_models(
{"structure_level_list_of_speakers/3": {"current_start_time": 250}}
)
response = self.request(
"structure_level_list_of_speakers.update",
{"id": 3, "initial_time": 700},
)
self.assert_status_code(response, 200)
self.assert_model_exists(
"structure_level_list_of_speakers/3",
{"initial_time": 700, "remaining_time": 700, "current_start_time": 50},
)

def test_set_spoken_time_with_started_speech(self) -> None:
self.set_models(
{"structure_level_list_of_speakers/3": {"current_start_time": 250}}
)
response = self.request(
"structure_level_list_of_speakers.update",
{"id": 3, "spoken_time": 200},
internal=True,
)
self.assert_status_code(response, 200)
self.assert_model_exists(
"structure_level_list_of_speakers/3",
{"remaining_time": 300, "current_start_time": 450},
)

def test_set_internal_fields_externally(self) -> None:
for field in ("current_start_time", "spoken_time"):
response = self.request(
Expand Down

0 comments on commit d830206

Please sign in to comment.