Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/api/schema/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class ExerciseRead(ExerciseCreate):
id: int


class ExerciseWithSkipUnlockTime(ExerciseRead):
class ExerciseWithUnlockTimestamps(ExerciseRead):
skip_unlock_time: datetime
next_grading_allowed_at: datetime


class SystemState(BaseModel):
Expand Down
22 changes: 13 additions & 9 deletions app/api/v1/exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.schema.exercise import ExerciseRead, ExerciseCreate, TestCaseRead, TestCaseCreate, \
ExerciseWithSkipUnlockTime
ExerciseWithUnlockTimestamps
from app.db.database import get_session
from app.db.model import Tan
from app.db.model.exercise import Exercise, ExerciseProgress, Competition, TestCase
Expand All @@ -21,10 +21,10 @@


@router.get("/current",
response_model=ExerciseWithSkipUnlockTime,
response_model=ExerciseWithUnlockTimestamps,
status_code=status.HTTP_200_OK)
async def get_current_exercise(tan_code: str, session: AsyncSession = Depends(get_session),
now: datetime = Depends(get_datetime_now)) -> ExerciseWithSkipUnlockTime | Response:
now: datetime = Depends(get_datetime_now)) -> ExerciseWithUnlockTimestamps | Response:
statement = select(Tan).where(Tan.code == tan_code)
result = await session.execute(statement)
tan = result.scalars().first()
Expand Down Expand Up @@ -69,8 +69,9 @@ async def get_current_exercise(tan_code: str, session: AsyncSession = Depends(ge
result = await session.execute(stmt)
exercise = result.scalars().first()

return ExerciseWithSkipUnlockTime(**exercise.to_dict(),
skip_unlock_time=(now + timedelta(minutes=exercise.skip_delay)))
return ExerciseWithUnlockTimestamps(**exercise.to_dict(),
skip_unlock_time=(now + timedelta(minutes=exercise.skip_delay)),
next_grading_allowed_at=now)
else:
stmt = (select(Exercise)
.join(Competition, Competition.first_exercise_id == Exercise.id)
Expand All @@ -92,15 +93,18 @@ async def get_current_exercise(tan_code: str, session: AsyncSession = Depends(ge

await session.refresh(first_exercise)

return ExerciseWithSkipUnlockTime(**first_exercise.to_dict(),
skip_unlock_time=(now + timedelta(minutes=first_exercise.skip_delay)))
return ExerciseWithUnlockTimestamps(**first_exercise.to_dict(),
skip_unlock_time=(now + timedelta(minutes=first_exercise.skip_delay)),
next_grading_allowed_at=now)

exercise, progress = exercise_and_progress

logging.info(progress.start_time.tzinfo)

return ExerciseWithSkipUnlockTime(**exercise.to_dict(),
skip_unlock_time=(progress.start_time + timedelta(minutes=exercise.skip_delay)))
return ExerciseWithUnlockTimestamps(**exercise.to_dict(),
skip_unlock_time=(
progress.start_time + timedelta(minutes=exercise.skip_delay)),
next_grading_allowed_at=progress.next_grading_allowed_at)


@router.post("/current/skip", status_code=status.HTTP_204_NO_CONTENT)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def test_get_current_exercise(self):

exercise_1 = dict(EXERCISES[1])
exercise_1["skip_unlock_time"] = datetime(year=2025, month=10, day=7, hour=19, minute=35, second=0).isoformat()
exercise_1["next_grading_allowed_at"] = datetime(year=2025, month=10, day=7, hour=19, minute=35,
second=0).isoformat()

print(response.json())

assert response.status_code == 200
assert response.json() == exercise_1
Expand All @@ -90,6 +94,7 @@ def test_get_current_exercise_with_missing_current_progress_entry_1(self):

exercise_2 = dict(EXERCISES[2])
exercise_2["skip_unlock_time"] = "2025-10-07T19:40:00Z"
exercise_2["next_grading_allowed_at"] = "2025-10-07T19:35:00Z"

assert response.status_code == 200
assert response.json() == exercise_2
Expand All @@ -104,6 +109,7 @@ def test_get_current_exercise_with_missing_current_progress_entry_2(self):

exercise_0 = dict(EXERCISES[0])
exercise_0["skip_unlock_time"] = "2025-10-07T19:40:00Z"
exercise_0["next_grading_allowed_at"] = "2025-10-07T19:35:00Z"

assert response.status_code == 200
assert response.json() == exercise_0
Expand Down