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
29 changes: 21 additions & 8 deletions backend/app/challenges/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ async def save_answer(

if participant is None:
raise BadRequestError("You are not subscribed to this challenge.")
if participant.submitted_at is not None:
raise BadRequestError("Challenge submission is already complete.")

await answer_service.upsert_answer(
session, participant.id, data.question_id, data.answer
Expand Down Expand Up @@ -123,24 +125,24 @@ async def submit_input(
raise NotFoundError("Challenge not found.")
if challenge.scoring_type != ChallengeScoringType.INPUT:
raise BadRequestError("Challenge does not accept input submissions.")
challenge_service.ensure_challenge_open(challenge.finishes_at)
if participant.submitted_at is not None:
return challenge_schemas.SubmissionResponse(
submitted_at=participant.submitted_at, score=participant.score
)
challenge_service.ensure_challenge_open(challenge.finishes_at)

participant.submission = data.answer
participant.submitted_at = datetime.now(UTC)
input_data = challenge.input
participant.score = (
challenge.points_value
if input_data is not None
is_correct = (
input_data is not None
and input_data.input_answer is not None
and data.answer.strip().lower() == input_data.input_answer.strip().lower()
else 0
)
participant.score = challenge.points_value if is_correct else 0
if is_correct:
participant.submitted_at = datetime.now(UTC)
await session.flush()
if participant.semana_participant is not None:
if is_correct and participant.semana_participant is not None:
await semana_service.add_points(
session,
participant.semana_participant,
Expand Down Expand Up @@ -180,6 +182,8 @@ async def save_many_answers(

if participant is None:
raise BadRequestError("You are not subscribed to this challenge.")
if participant.submitted_at is not None:
raise BadRequestError("Challenge submission is already complete.")

for answer_body in data:
await answer_service.upsert_answer(
Expand Down Expand Up @@ -224,7 +228,10 @@ async def submit_challenge(

challenge_service.ensure_challenge_open(challenge.finishes_at)
score = await challenge_service.process_submission(session, participant, challenge)
if participant.semana_participant is not None:
if (
participant.submitted_at is not None
and participant.semana_participant is not None
):
await semana_service.add_points(
session,
participant.semana_participant,
Expand Down Expand Up @@ -280,6 +287,7 @@ async def get_challenge(
questions=question_responses,
is_participant=participant is not None,
submitted_at=participant.submitted_at if participant else None,
score=participant.score if participant else None,
)


Expand Down Expand Up @@ -310,6 +318,11 @@ async def get_ranking(
]


@router.get(
"",
response_model=Page[challenge_schemas.ChallengePublicResponse],
include_in_schema=False,
)
@router.get("/", response_model=Page[challenge_schemas.ChallengePublicResponse])
async def list_challenges(
session: Annotated[AsyncSession, Depends(get_session)],
Expand Down
3 changes: 2 additions & 1 deletion backend/app/challenges/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AnswerBody(BaseModel):


class SubmissionResponse(BaseModel):
submitted_at: datetime
submitted_at: datetime | None
score: int


Expand Down Expand Up @@ -185,6 +185,7 @@ class ChallengeResponse(ChallengeImageMixin):
questions: list[QuestionResponse] = Field(default_factory=list)
is_participant: bool = False
submitted_at: datetime | None = None
score: int | None = None


class ChallengePublicResponse(ChallengeImageMixin):
Expand Down
3 changes: 2 additions & 1 deletion backend/app/challenges/services/challenge.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ async def process_submission(
else 0
)

participant.submitted_at = datetime.now(UTC)
is_complete = bool(question_count) and (correct_count or 0) == question_count
participant.submitted_at = datetime.now(UTC) if is_complete else None
participant.score = score

await session.flush()
Expand Down
1 change: 1 addition & 0 deletions backend/app/semana/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ParticipationWeek(BaseModel):
semana_id: int
nome: str
ano: int
nickname: str | None
pontos: int
horas: int

Expand Down
9 changes: 8 additions & 1 deletion backend/app/semana/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ async def get_user_participation(session: AsyncSession, user_id):
Semana.id,
Semana.nome,
Semana.ano,
SemanaParticipant.nickname,
func.coalesce(func.sum(PointEvent.amount), 0).label("pontos"),
)
.join(SemanaParticipant, SemanaParticipant.semana_id == Semana.id)
Expand All @@ -125,7 +126,12 @@ async def get_user_participation(session: AsyncSession, user_id):
SemanaParticipant.deleted_at.is_(None),
PointEvent.deleted_at.is_(None) | (PointEvent.id.is_(None)),
)
.group_by(Semana.id, Semana.nome, Semana.ano)
.group_by(
Semana.id,
Semana.nome,
Semana.ano,
SemanaParticipant.nickname,
)
)
hours_rows = await session.execute(
select(
Expand Down Expand Up @@ -155,6 +161,7 @@ async def get_user_participation(session: AsyncSession, user_id):
"ano": point_row.ano
if point_row
else (await get_semana(session, semana_id)).ano,
"nickname": point_row.nickname if point_row else None,
"pontos": int(point_row.pontos) if point_row else 0,
"horas": int(hours.get(semana_id, 0)),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def test_list_challenges_paginated(

c = as_user(user)

r = await c.get("api/v1/challenge/", params={"limit": 2, "offset": 0})
r = await c.get("api/v1/challenge", params={"limit": 2, "offset": 0})
assert r.status_code == 200
body = r.json()
assert body["total"] == 5
Expand Down
Loading
Loading