Skip to content

Commit 0845fa7

Browse files
committed
Added watch start/end autoset
1 parent e656b24 commit 0845fa7

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

app/schemas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ class WatchResponseBase(CustomModel):
421421
duration: int = Field(examples=[24])
422422
episodes: int = Field(examples=[3])
423423
score: int = Field(examples=[8])
424+
start_date: datetime_pd | None
425+
end_date: datetime_pd | None
424426

425427

426428
class ReadResponseBase(CustomModel):

app/watch/service.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,41 @@ async def save_watch(
103103
# Create watch record if missing
104104
if not (watch := await get_anime_watch(session, anime, user)):
105105
log_type = constants.LOG_WATCH_CREATE
106+
106107
watch = AnimeWatch()
108+
watch.start_date = now
107109
watch.created = now
108110
watch.anime = anime
109111
watch.user = user
110112

113+
session.add(watch)
114+
111115
log_before = {}
112116
log_after = {}
113117

114118
# Set attributes from args to watch record
115119
for key, new_value in args.model_dump().items():
116120
# Here we add changes to log's before/after dicts only if there are changes
117121
old_value = getattr(watch, key)
122+
118123
if old_value != new_value:
119124
log_before[key] = old_value
120125
setattr(watch, key, new_value)
121126
log_after[key] = new_value
122127

128+
if (
129+
key == "status"
130+
and new_value == constants.WATCH_COMPLETED
131+
and watch.end_date is None
132+
):
133+
watch.end_date = now
134+
123135
# Calculate duration and update updated field
124136
watch.duration = calculate_watch_duration(watch)
125137
watch.updated = now
126138

127139
# Update user last list update
128140
user.updated = now
129-
session.add_all([watch, user])
130141

131142
await session.commit()
132143

tests/watch/test_watch_add.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ async def test_watch_add(
2828
)
2929

3030
assert response.status_code == status.HTTP_200_OK
31+
32+
assert response.json()["start_date"] is not None
33+
assert response.json()["end_date"] is None
34+
3135
assert response.json()["anime"]["slug"] == "bocchi-the-rock-9e172d"
3236
assert response.json()["status"] == "watching"
3337
assert response.json()["duration"] == 230 # 10 episodes * 23 minutes
@@ -72,6 +76,10 @@ async def test_watch_add(
7276
)
7377

7478
assert response.status_code == status.HTTP_200_OK
79+
80+
assert response.json()["start_date"] is not None
81+
assert response.json()["end_date"] is not None
82+
7583
assert response.json()["anime"]["slug"] == "bocchi-the-rock-9e172d"
7684
assert response.json()["status"] == "completed"
7785
assert response.json()["note"] == "Good anime!"

0 commit comments

Comments
 (0)