def lock(tallySheetId, body):
    request_body = RequestBody(body)
    tallySheetVersionId = request_body.get("lockedVersionId")

    tally_sheet = TallySheet.get_by_id(tallySheetId=tallySheetId)

    if tally_sheet is None:
        raise NotFoundException("Tally sheet not found (tallySheetId=%d)" %
                                tallySheetId)

    tally_sheet_version = TallySheetVersion.get_by_id(
        tallySheetVersionId=tallySheetVersionId, tallySheetId=tallySheetId)

    if tally_sheet_version is None:
        raise NotFoundException(
            message="Tally sheet version not found (tallySheetVersionId=%d)" %
            tallySheetVersionId,
            code=MESSAGE_CODE_TALLY_SHEET_VERSION_NOT_FOUND)

    if not tally_sheet_version.isComplete:
        raise NotFoundException(
            message="Incomplete tally sheet version (tallySheetVersionId=%d)" %
            tallySheetVersionId,
            code=
            MESSAGE_CODE_TALLY_SHEET_INCOMPLETE_TALLY_SHEET_CANNOT_BE_LOCKED)

    tally_sheet.set_locked_version(tally_sheet_version)

    if tally_sheet.tallySheetCode in [
            TallySheetCodeEnum.CE_201, TallySheetCodeEnum.CE_201_PV
    ]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusCE201.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Verified"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_41]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE41.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Verified"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_34_CO]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE34.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Verified"

    db.session.commit()

    return TallySheetSchema().dump(tally_sheet).data, 201
def submit(tallySheetId, body):
    request_body = RequestBody(body)
    tallySheetVersionId = request_body.get("submittedVersionId")

    tally_sheet: TallySheetModel = TallySheet.get_by_id(tallySheetId=tallySheetId)

    if tally_sheet is None:
        raise NotFoundException(
            message="Tally sheet not found (tallySheetId=%d)" % tallySheetId,
            code=MESSAGE_CODE_TALLY_SHEET_NOT_FOUND
        )

    if tally_sheet.tallySheetCode not in [TallySheetCodeEnum.PRE_41, TallySheetCodeEnum.CE_201,
                                          TallySheetCodeEnum.CE_201_PV, TallySheetCodeEnum.PRE_34_CO]:
        raise ForbiddenException(
            message="Submit operation is not supported for this tally sheet type.",
            code=MESSAGE_CODE_TALLY_SHEET_SUBMIT_IS_NOT_SUPPORTED
        )

    tally_sheet_version = TallySheetVersion.get_by_id(tallySheetVersionId=tallySheetVersionId,
                                                      tallySheetId=tallySheetId)

    if tally_sheet_version is None:
        raise NotFoundException(
            message="Tally sheet version not found (tallySheetVersionId=%d)" % tallySheetVersionId,
            code=MESSAGE_CODE_TALLY_SHEET_VERSION_NOT_FOUND
        )

    tally_sheet.set_submitted_version(tally_sheet_version)

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.CE_201, TallySheetCodeEnum.CE_201_PV]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusCE201.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_41]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE41.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_34_CO]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE34.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    db.session.commit()

    return TallySheetSchema().dump(tally_sheet).data, 201
def unlock(tallySheetId):
    tally_sheet = TallySheet.get_by_id(tallySheetId=tallySheetId)

    if tally_sheet is None:
        NotFoundException(message="Tally sheet not found (tallySheetId=%d)" %
                          tallySheetId,
                          code=MESSAGE_CODE_TALLY_SHEET_NOT_FOUND)

    # TODO refactor
    tally_sheet.submissionProof.open()

    tally_sheet.set_locked_version(None)

    if tally_sheet.tallySheetCode in [
            TallySheetCodeEnum.CE_201, TallySheetCodeEnum.CE_201_PV
    ]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusCE201.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_41]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE41.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    if tally_sheet.tallySheetCode in [TallySheetCodeEnum.PRE_34_CO]:
        election = tally_sheet.submission.election
        electionId = election.parentElectionId
        countingCentreId = tally_sheet.areaId
        results = StatusPRE34.get_status_records(electionId, countingCentreId)

        for item in results:
            item.status = "Submitted"

    db.session.commit()

    return TallySheetSchema().dump(tally_sheet).data, 201
Example #4
0
def create(tallySheetId, body):
    request_body = RequestBody(body)
    tallySheet, tallySheetVersion = TallySheet.create_latest_version(
        tallySheetId=tallySheetId, tallySheetCode=TallySheetCodeEnum.PRE_41)

    election = tallySheet.submission.election
    voteType = election.electionName
    status = "Entered"
    electionId = election.parentElectionId
    countingCentreId = tallySheet.areaId
    area = Area.get_by_id(areaId=countingCentreId)
    electoralDistrictId = area.get_associated_areas(
        AreaTypeEnum.ElectoralDistrict, electionId=electionId)[0].areaId
    pollingDivisionId = None
    pollingDivisionResult = area.get_associated_areas(
        AreaTypeEnum.PollingDivision, electionId=electionId)
    if len(pollingDivisionResult) > 0:
        pollingDivisionId = area.get_associated_areas(
            AreaTypeEnum.PollingDivision, electionId=electionId)[0].areaId

    tally_sheet_content = request_body.get("content")
    is_complete = True
    if tally_sheet_content is not None:

        for row in tally_sheet_content:
            party_count_body = RequestBody(row)
            candidateId = party_count_body.get("candidateId")
            count = party_count_body.get("count")
            countInWords = party_count_body.get("countInWords")

            if (candidateId and count and countInWords) is not None:
                tallySheetVersion.add_row(candidateId=candidateId,
                                          count=count,
                                          countInWords=countInWords)
            else:
                is_complete = False

            voteCount = party_count_body.get("count")
            pollingStationId = party_count_body.get("areaId")
            candidateId = party_count_body.get("candidateId")
            if election is not None:
                existingStatus = StatusPRE41.get_status_record(
                    electionId=electionId,
                    electoralDistrictId=electoralDistrictId,
                    pollingDivisionId=pollingDivisionId,
                    countingCentreId=countingCentreId,
                    pollingStationId=pollingStationId,
                    candidateId=candidateId)
                if existingStatus is None:
                    StatusPRE41.create(
                        voteType=voteType,
                        status=status,
                        electionId=electionId,
                        electoralDistrictId=electoralDistrictId,
                        pollingDivisionId=pollingDivisionId,
                        countingCentreId=countingCentreId,
                        pollingStationId=pollingStationId,
                        voteCount=voteCount,
                        candidateId=candidateId,
                    )
                else:
                    existingStatus.voteType = voteType,
                    existingStatus.electionId = electionId,
                    existingStatus.electoralDistrictId = electoralDistrictId,
                    existingStatus.pollingDivisionId = pollingDivisionId,
                    existingStatus.countingCentreId = countingCentreId,
                    existingStatus.pollingStationId = pollingStationId,
                    existingStatus.voteCount = voteCount,
                    existingStatus.candidateId = candidateId

    tally_sheet_summary_body = request_body.get("summary")
    if tally_sheet_summary_body is not None:
        electionId = tallySheetVersion.submission.electionId
        rejectedVoteCount = tally_sheet_summary_body.get("rejectedVoteCount")

        if (electionId and rejectedVoteCount) is not None:
            tallySheetVersion.add_invalid_vote_count(
                electionId=electionId, rejectedVoteCount=rejectedVoteCount)
        else:
            is_complete = False

        voteCount = tally_sheet_summary_body.get("rejectedVoteCount")
        candidateId = None
        pollingStationId = None
        if election is not None:
            existingStatus = StatusPRE41.get_status_record(
                electionId=election.parentElectionId,
                electoralDistrictId=electoralDistrictId,
                pollingDivisionId=pollingDivisionId,
                countingCentreId=countingCentreId,
                candidateId=candidateId)
            if existingStatus is None:
                StatusPRE41.create(
                    voteType=voteType,
                    status=status,
                    electionId=election.parentElectionId,
                    electoralDistrictId=electoralDistrictId,
                    pollingDivisionId=pollingDivisionId,
                    countingCentreId=countingCentreId,
                    pollingStationId=pollingStationId,
                    voteCount=voteCount,
                    candidateId=candidateId,
                )
            else:
                existingStatus.voteType = voteType,
                existingStatus.electionId = election.parentElectionId,
                existingStatus.electoralDistrictId = electoralDistrictId,
                existingStatus.pollingDivisionId = pollingDivisionId,
                existingStatus.countingCentreId = countingCentreId,
                existingStatus.voteCount = voteCount,
                existingStatus.pollingStationId = pollingStationId,
                existingStatus.candidateId = candidateId

    if is_complete:
        tallySheetVersion.set_complete()
    db.session.commit()

    return TallySheetVersionSchema().dump(tallySheetVersion).data