Skip to content

Commit 875393f

Browse files
committed
fix: enhance episode repeat handling with historical state revert #489
1 parent ab768af commit 875393f

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/app/models.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,12 +1126,35 @@ def unwatch(self, episode_number):
11261126
)
11271127

11281128
if episode.repeats > 0:
1129-
episode.repeats -= 1
1130-
episode.save(update_fields=["repeats"])
1131-
logger.info(
1132-
"%s watch count decreased.",
1133-
episode,
1134-
)
1129+
# Get the historical records for this episode
1130+
history = episode.history.all()
1131+
1132+
if history.count() > 1:
1133+
# Get the previous historical record (second latest)
1134+
previous_record = history[1]
1135+
1136+
# Revert to previous state without creating new history
1137+
episode.repeats = previous_record.repeats
1138+
episode.end_date = previous_record.end_date
1139+
episode.save_without_historical_record()
1140+
1141+
# Delete the latest historical record (the one we're reverting from)
1142+
latest_record = history.first()
1143+
latest_record.delete()
1144+
1145+
logger.info(
1146+
"%s reverted to previous state (repeats: %s)",
1147+
episode,
1148+
episode.repeats,
1149+
)
1150+
else:
1151+
# Fallback to original behavior if no previous history exists
1152+
episode.repeats -= 1
1153+
episode.save_without_historical_record(update_fields=["repeats"])
1154+
logger.info(
1155+
"%s watch count decreased (no history to revert).",
1156+
episode,
1157+
)
11351158
else:
11361159
episode.delete()
11371160
logger.info(

0 commit comments

Comments
 (0)