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 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 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
def create(tallySheetId, body): request_body = RequestBody(body) tallySheet, tallySheetVersion = TallySheet.create_latest_version( tallySheetId=tallySheetId, tallySheetCode=TallySheetCodeEnum.CE_201 ) 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 = area.get_associated_areas(AreaTypeEnum.PollingDivision, electionId=electionId)[0].areaId tally_sheet_content = request_body.get("content") if tally_sheet_content is not None: is_complete = True for party_count_body in tally_sheet_content: party_count_body = RequestBody(party_count_body) areaId = party_count_body.get("areaId") ballotsIssued = party_count_body.get("ballotsIssued") ballotsReceived = party_count_body.get("ballotsReceived") ballotsSpoilt = party_count_body.get("ballotsSpoilt") ballotsUnused = party_count_body.get("ballotsUnused") ordinaryBallotCountFromBoxCount = party_count_body.get("ordinaryBallotCountFromBoxCount") tenderedBallotCountFromBoxCount = party_count_body.get("tenderedBallotCountFromBoxCount") ordinaryBallotCountFromBallotPaperAccount = party_count_body.get( "ordinaryBallotCountFromBallotPaperAccount") tenderedBallotCountFromBallotPaperAccount = party_count_body.get( "tenderedBallotCountFromBallotPaperAccount") if (areaId and ballotsIssued and ballotsReceived and ballotsSpoilt and ballotsUnused and ordinaryBallotCountFromBoxCount and tenderedBallotCountFromBallotPaperAccount and tenderedBallotCountFromBallotPaperAccount and tenderedBallotCountFromBoxCount and ordinaryBallotCountFromBallotPaperAccount) is not None: tallySheetVersionRow = tallySheetVersion.add_row( areaId=areaId, ballotsIssued=ballotsIssued, ballotsReceived=ballotsReceived, ballotsSpoilt=ballotsSpoilt, ballotsUnused=ballotsUnused, ordinaryBallotCountFromBoxCount=ordinaryBallotCountFromBoxCount, tenderedBallotCountFromBoxCount=tenderedBallotCountFromBoxCount, ordinaryBallotCountFromBallotPaperAccount=ordinaryBallotCountFromBallotPaperAccount, tenderedBallotCountFromBallotPaperAccount=tenderedBallotCountFromBallotPaperAccount ) else: is_complete = False for issued_ballot_box_id in party_count_body.get("ballotBoxesIssued"): if issued_ballot_box_id is not None: tallySheetVersionRow.add_issued_ballot_box(issued_ballot_box_id) else: is_complete = False for received_ballot_box_id in party_count_body.get("ballotBoxesReceived"): if received_ballot_box_id is not None: tallySheetVersionRow.add_received_ballot_box(received_ballot_box_id) else: is_complete = False ballotCount = party_count_body.get("ordinaryBallotCountFromBoxCount") pollingStationId = party_count_body.get("areaId") if election is not None: existingStatus = StatusCE201.get_status_record( electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, pollingStationId=pollingStationId, ) if existingStatus is None: StatusCE201.create( voteType=voteType, status=status, electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, pollingStationId=pollingStationId, ballotCount=ballotCount ) else: existingStatus.voteType = voteType, existingStatus.electionId = electionId, existingStatus.electoralDistrictId = electoralDistrictId, existingStatus.pollingDivisionId = pollingDivisionId, existingStatus.countingCentreId = countingCentreId, existingStatus.pollingStationId = pollingStationId, existingStatus.ballotCount = ballotCount if is_complete: tallySheetVersion.set_complete() db.session.commit() return TallySheetVersionSchema().dump(tallySheetVersion).data
def create(tallySheetId, body): request_body = RequestBody(body) tallySheet, tallySheetVersion = TallySheet.create_latest_version( tallySheetId=tallySheetId, tallySheetCode=TallySheetCodeEnum.CE_201_PV) 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 total_number_of_a_packets_found = 0 tally_sheet_content = request_body.get("content") if tally_sheet_content is not None: is_complete = True for row in tally_sheet_content: tally_sheet_content_item = RequestBody(row) ballotBoxId = tally_sheet_content_item.get("ballotBoxId") numberOfPacketsInserted = tally_sheet_content_item.get( "numberOfPacketsInserted") numberOfAPacketsFound = tally_sheet_content_item.get( "numberOfAPacketsFound") if (ballotBoxId and numberOfPacketsInserted and numberOfAPacketsFound) is not None: row = tallySheetVersion.add_row( ballotBoxId=ballotBoxId, numberOfPacketsInserted=numberOfPacketsInserted, numberOfAPacketsFound=numberOfAPacketsFound) total_number_of_a_packets_found += row.numberOfAPacketsFound else: is_complete = False if is_complete: tallySheetVersion.set_complete() # for postal votes pollingStation is not available there for all results aggregated ballotCount = total_number_of_a_packets_found pollingStationId = None if election is not None: existingStatus = StatusCE201.get_status_record( electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, pollingStationId=pollingStationId, ) if existingStatus is None: StatusCE201.create(voteType=voteType, status=status, electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, pollingStationId=pollingStationId, ballotCount=ballotCount) else: existingStatus.voteType = voteType, existingStatus.electionId = electionId, existingStatus.electoralDistrictId = electoralDistrictId, existingStatus.pollingDivisionId = pollingDivisionId, existingStatus.countingCentreId = countingCentreId, existingStatus.pollingStationId = pollingStationId, existingStatus.ballotCount = ballotCount tally_sheet_summary = request_body.get("summary") time_of_commencement_of_count = tally_sheet_summary.get( "timeOfCommencementOfCount") if time_of_commencement_of_count is not None: # Remove the colon from %z [1] following [2] # [1] http://strftime.org/ # [2] https://stackoverflow.com/questions/30999230/parsing-timezone-with-colon if ":" == time_of_commencement_of_count[-3:-2]: time_of_commencement_of_count = time_of_commencement_of_count[:-3] + time_of_commencement_of_count[ -2:] try: time_of_commencement_of_count = datetime.datetime.strptime( time_of_commencement_of_count, '%Y-%m-%dT%H:%M:%S%z') except Exception as e: time_of_commencement_of_count = None else: time_of_commencement_of_count = None # To remove the colon between time zone comming from openapi date-time. # This is since the colon is not accepted in python datetime # if ":" == time_of_commencement_of_count[-3:-2]: # time_of_commencement_of_count = time_of_commencement_of_count[:-3] + time_of_commencement_of_count[-2:] tallySheetVersion.add_summary( situation=tally_sheet_summary.get("situation"), timeOfCommencementOfCount=time_of_commencement_of_count, numberOfAPacketsFound=total_number_of_a_packets_found, numberOfACoversRejected=tally_sheet_summary.get( "numberOfACoversRejected"), numberOfBCoversRejected=tally_sheet_summary.get( "numberOfBCoversRejected"), numberOfValidBallotPapers=tally_sheet_summary.get( "numberOfValidBallotPapers")) db.session.commit() return TallySheetVersionSchema().dump(tallySheetVersion).data