Beispiel #1
0
def write_rounds(report, election: Election):
    write_heading(report, "ROUNDS")
    report.writerow([
        "Round Number",
        "Contest Name",
        "Targeted?",
        "Sample Size",
        "Risk Limit Met?",
        "P-Value",
        "Start Time",
        "End Time",
        "Audited Votes",
    ])
    for round in election.rounds:
        for contest in election.contests:
            round_contest = next(rc for rc in round.round_contests
                                 if rc.contest_id == contest.id)
            report.writerow([
                round.round_num,
                contest.name,
                pretty_targeted(contest.is_targeted),
                round_contest.sample_size,
                pretty_boolean(round_contest.is_complete),
                round_contest.end_p_value,
                isoformat(round.created_at),
                isoformat(round.ended_at),
                pretty_audited_votes(contest, round_contest),
            ])
Beispiel #2
0
def serialize_round(round: Round) -> dict:
    return {
        "id": round.id,
        "roundNum": round.round_num,
        "startedAt": isoformat(round.created_at),
        "endedAt": isoformat(round.ended_at),
        "isAuditComplete": is_audit_complete(round.id),
    }
Beispiel #3
0
def auth_me():
    user_type, user_key = get_loggedin_user()
    if user_type in [UserType.AUDIT_ADMIN, UserType.JURISDICTION_ADMIN]:
        user = User.query.filter_by(email=user_key).one()
        return jsonify(
            type=user_type,
            email=user.email,
            organizations=[{
                "id":
                org.id,
                "name":
                org.name,
                "elections": [serialize_election(e) for e in org.elections],
            } for org in user.organizations],
            jurisdictions=[{
                "id": j.id,
                "name": j.name,
                "election": serialize_election(j.election)
            } for j in user.jurisdictions],
        )
    elif user_type == UserType.AUDIT_BOARD:
        audit_board = AuditBoard.query.get(user_key)
        return jsonify(
            type=user_type,
            id=audit_board.id,
            jurisdictionId=audit_board.jurisdiction_id,
            roundId=audit_board.round_id,
            name=audit_board.name,
            members=serialize_members(audit_board),
            signedOffAt=isoformat(audit_board.signed_off_at),
        )
    else:
        return Unauthorized()
Beispiel #4
0
def serialize_file_processing(file: File) -> Dict[str, Any]:
    if file.processing_error:
        status = ProcessingStatus.ERRORED
    elif file.processing_completed_at:
        status = ProcessingStatus.PROCESSED
    elif file.processing_started_at:
        status = ProcessingStatus.PROCESSING
    else:
        status = ProcessingStatus.READY_TO_PROCESS

    return {
        "status": status,
        "startedAt": isoformat(file.processing_started_at),
        "completedAt": isoformat(file.processing_completed_at),
        "error": file.processing_error,
    }
Beispiel #5
0
def serialize_election(election):
    return {
        "id": election.id,
        "auditName": election.audit_name,
        "electionName": election.election_name,
        "state": election.state,
        "electionDate": isoformat(election.election_date),
        "isMultiJurisdiction": election.is_multi_jurisdiction,
    }
Beispiel #6
0
def serialize_audit_board(audit_board: AuditBoard,
                          round_status: JSONDict) -> JSONDict:
    return {
        "id": audit_board.id,
        "name": audit_board.name,
        "passphrase": audit_board.passphrase,
        "signedOffAt": isoformat(audit_board.signed_off_at),
        "currentRoundStatus": round_status,
    }
Beispiel #7
0
def audit_status(election_id=None):
    election = get_election(election_id)

    return jsonify(
        organizationId=election.organization_id,
        name=election.election_name,
        online=election.online,
        frozenAt=isoformat(election.frozen_at),
        riskLimit=election.risk_limit,
        randomSeed=election.random_seed,
        isMultiJurisdiction=election.is_multi_jurisdiction,
        contests=[{
            "id":
            contest.id,
            "name":
            contest.name,
            "isTargeted":
            contest.is_targeted,
            "choices": [{
                "id": choice.id,
                "name": choice.name,
                "numVotes": choice.num_votes
            } for choice in contest.choices],
            "totalBallotsCast":
            contest.total_ballots_cast,
            "numWinners":
            contest.num_winners,
            "votesAllowed":
            contest.votes_allowed,
        } for contest in election.contests],
        jurisdictions=[
            {
                "id":
                j.id,
                "name":
                j.name,
                "contests": [c.id for c in j.contests],
                "auditBoards": [{
                    "id": audit_board.id,
                    "name": audit_board.name,
                    "members": serialize_members(audit_board),
                    "passphrase": audit_board.passphrase,
                } for audit_board in j.audit_boards],
                "ballotManifest": {
                    "file":
                    serialize_file(j.manifest_file)
                    if j.manifest_file else None,
                    "processing":
                    serialize_file_processing(j.manifest_file)
                    if j.manifest_file else None,
                    "numBallots":
                    j.manifest_num_ballots,
                    "numBatches":
                    j.manifest_num_batches,
                    # Deprecated fields.
                    "filename":
                    j.manifest_file.name if j.manifest_file else None,
                    "uploadedAt":
                    isoformat(j.manifest_file.uploaded_at)
                    if j.manifest_file else None,
                },
                "batches": [{
                    "id": batch.id,
                    "name": batch.name,
                    "numBallots": batch.num_ballots,
                    "storageLocation": batch.storage_location,
                    "tabulator": batch.tabulator,
                } for batch in j.batches],
            } for j in election.jurisdictions
        ],
        rounds=[
            {
                "id":
                round.id,
                "startedAt":
                isoformat(round.created_at),
                "endedAt":
                isoformat(round.ended_at),
                "contests": [
                    {
                        "id":
                        round_contest.contest_id,
                        "endMeasurements": {
                            "pvalue": round_contest.end_p_value,
                            "isComplete": round_contest.is_complete,
                        },
                        "results": {
                            result.contest_choice_id: result.result
                            for result in round_contest.results
                        },
                        "sampleSizeOptions":
                        json.loads(round_contest.sample_size_options
                                   or "null"),
                        "sampleSize":
                        round_contest.sample_size,
                    }
                    # pylint: disable=no-member
                    # (seems like a pylint bug)
                    for round_contest in round.round_contests
                ],
            } for round in election.rounds
        ],
    )
Beispiel #8
0
def serialize_file(file: File) -> Dict[str, Any]:
    return {
        "name": file.name,
        "uploadedAt": isoformat(file.uploaded_at),
    }