fix: results

dependabot/pip/requests-2.31.0
Pierre-Louis Guhur 1 year ago committed by guhur
parent b6c12b54b7
commit 9645a1544d

@ -320,11 +320,16 @@ def get_results(db: Session, election_ref: str) -> schemas.ResultsGet:
.group_by(models.Vote.candidate_id, models.Grade.value)
.all()
)
if db_res == []:
raise errors.NoRecordedVotes()
ballots: t.DefaultDict[int, dict[int, int]] = defaultdict(dict)
for candidate_id, grade_value, num_votes in db_res:
ballots[candidate_id][grade_value] = num_votes
merit_profile = {
c: [votes[value] for value in sorted(votes.keys(), reverse=True)]
c: {value: votes[value] for value in sorted(votes.keys(), reverse=True)}
for c, votes in ballots.items()
}

@ -38,3 +38,10 @@ class UnauthorizedError(Exception):
def __init__(self, name: str):
self.name = name
class NoRecordedVotes(Exception):
"""
We can't display results if we don't have resutls
"""

@ -52,6 +52,18 @@ async def bad_request_exception_handler(request: Request, exc: errors.NotFoundEr
)
@app.exception_handler(errors.NoRecordedVotes)
async def no_recorded_votes_exception_handler(
request: Request, exc: errors.NoRecordedVotes
):
return JSONResponse(
status_code=403,
content={
"message": f"No votes have been recorded yet"
},
)
@app.exception_handler(errors.InconsistentDatabaseError)
async def inconsistent_database_exception_handler(
request: Request, exc: errors.InconsistentDatabaseError

Loading…
Cancel
Save