def __init__(self, tallySheetVersion, areaId, ballotBoxesIssued, ballotBoxesReceived, ballotsIssued, ballotsReceived, ballotsSpoilt, ballotsUnused, ordinaryBallotCountFromBoxCount, tenderedBallotCountFromBoxCount, ordinaryBallotCountFromBallotPaperAccount, tenderedBallotCountFromBallotPaperAccount): area = Area.get_by_id(areaId=areaId) if area is None: raise NotFoundException("Area not found. (areaId=%d)" % areaId) if area.electionId != tallySheetVersion.submission.electionId: raise NotFoundException( "Area is not registered for the given election. (areaId=%d)" % areaId) super(TallySheetVersionRow_CE_201_Model, self).__init__( tallySheetVersionId=tallySheetVersion.tallySheetVersionId, areaId=areaId, # ballotBoxesIssued=ballotBoxesIssued, # ballotBoxesReceived=ballotBoxesReceived, ballotsIssued=ballotsIssued, ballotsReceived=ballotsReceived, ballotsSpoilt=ballotsSpoilt, ballotsUnused=ballotsUnused, ordinaryBallotCountFromBoxCount=ordinaryBallotCountFromBoxCount, tenderedBallotCountFromBoxCount=tenderedBallotCountFromBoxCount, ordinaryBallotCountFromBallotPaperAccount= ordinaryBallotCountFromBallotPaperAccount, tenderedBallotCountFromBallotPaperAccount= tenderedBallotCountFromBallotPaperAccount) db.session.add(self) db.session.flush()
def __init__(self, tallySheetVersion, areaId, ballotsIssued, ballotsReceived, ballotsSpoilt, ballotsUnused, boxCountOrdinary, boxCountTendered, ballotPaperAccountOrdinary, ballotPaperAccountTendered): area = Area.get_by_id(areaId=areaId) if area is None: raise NotFoundException("Area not found. (areaId=%d)" % areaId) if area.electionId != tallySheetVersion.submission.electionId: raise NotFoundException( "Area is not registered for the given election. (areaId=%d)" % areaId) super(TallySheetVersionRow_CE_201_Model, self).__init__( tallySheetVersionId=tallySheetVersion.tallySheetVersionId, areaId=areaId, ballotsIssued=ballotsIssued, ballotsReceived=ballotsReceived, ballotsSpoilt=ballotsSpoilt, ballotsUnused=ballotsUnused, boxCountOrdinary=boxCountOrdinary, boxCountTendered=boxCountTendered, ballotPaperAccountOrdinary=ballotPaperAccountOrdinary, ballotPaperAccountTendered=ballotPaperAccountTendered) db.session.add(self) db.session.commit()
def html(self, title="", total_registered_voters=None): tallySheetVersion = self.tallySheetVersion polling_station_wise_number_of_ballots_recieved = self.get_polling_station_wise_number_of_ballots_recieved( ) # polling_station_wise_number_of_spoilt_ballot_papers = self.get_polling_station_wise_number_of_spoilt_ballot_papers() # polling_station_wise_number_of_issued_ballot_papers = self.get_polling_station_wise_number_of_issued_ballot_papers() # polling_station_wise_number_of_unused_ballot_papers = self.get_polling_station_wise_number_of_unused_ballot_papers() # polling_station_wise_number_of_ordinary_ballots_in_ballot_paper_account = self.get_polling_station_wise_number_of_ordinary_ballots_in_ballot_paper_account() polling_station_wise_number_of_ordinary_ballots_in_ballot_box = self.get_polling_station_wise_number_of_ordinary_ballots_in_ballot_box( ) stamp = tallySheetVersion.stamp polling_divisions = Area.get_associated_areas( tallySheetVersion.tallySheet.area, AreaTypeEnum.PollingDivision) polling_division_name = "" if len(polling_divisions) > 0: polling_division_name = polling_divisions[0].areaName if tallySheetVersion.tallySheet.election.voteType != NonPostal: polling_division_name = tallySheetVersion.tallySheet.election.voteType totalBallotBoxCount = 0 content = { "election": { "electionName": tallySheetVersion.tallySheet.election.get_official_name() }, "stamp": { "createdAt": stamp.createdAt, "createdBy": stamp.createdBy, "barcodeString": stamp.barcodeString }, "tallySheetCode": "CE-201", "electoralDistrict": Area.get_associated_areas( tallySheetVersion.tallySheet.area, AreaTypeEnum.ElectoralDistrict)[0].areaName, "pollingDivision": polling_division_name, "countingCentre": tallySheetVersion.tallySheet.area.areaName, "parliamentaryElection": 1, "data": [], "totalBallotBoxCount": totalBallotBoxCount } # Appending polling station wise ballot counts for polling_station_wise_number_of_ballots_recieved_item_index, polling_station_wise_number_of_ballots_recieved_item in polling_station_wise_number_of_ballots_recieved.iterrows( ): data_row = [] polling_station = Area.get_by_id( areaId=polling_station_wise_number_of_ballots_recieved_item .areaId) polling_districts = Area.get_associated_areas( polling_station, AreaTypeEnum.PollingDistrict) data_row.append(", ".join([ polling_district.areaName for polling_district in polling_districts ])) data_row.append( polling_station_wise_number_of_ballots_recieved_item. areaName) # data_row.append(to_comma_seperated_num(polling_station_wise_number_of_ballots_recieved_item.numValue)) # data_row.append(to_comma_seperated_num(polling_station_wise_number_of_spoilt_ballot_papers["numValue"].values[ # polling_station_wise_number_of_ballots_recieved_item_index])) # data_row.append(to_comma_seperated_num(polling_station_wise_number_of_issued_ballot_papers["numValue"].values[ # polling_station_wise_number_of_ballots_recieved_item_index])) # data_row.append(to_comma_seperated_num(polling_station_wise_number_of_unused_ballot_papers["numValue"].values[ # polling_station_wise_number_of_ballots_recieved_item_index])) # data_row.append(to_comma_seperated_num(polling_station_wise_number_of_ordinary_ballots_in_ballot_paper_account["numValue"].values[ # polling_station_wise_number_of_ballots_recieved_item_index])) data_row.append( to_comma_seperated_num( polling_station_wise_number_of_ordinary_ballots_in_ballot_box[ "numValue"]. values[ polling_station_wise_number_of_ballots_recieved_item_index] )) totalBallotBoxCount += polling_station_wise_number_of_ordinary_ballots_in_ballot_box[ "numValue"].values[ polling_station_wise_number_of_ballots_recieved_item_index] content["data"].append(data_row) content["totalBallotBoxCount"] = to_comma_seperated_num( totalBallotBoxCount) html = render_template('CE-201.html', content=content) return html
def get_by_id(areaId): result = Area.get_by_id(areaId=areaId) return AreaSchema().dump(result).data
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
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.PRE_34_CO) 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 tallySheetVersion.set_complete( ) # TODO: valid before setting complete. Refer to PRE_34_CO tally_sheet_content = request_body.get("content") # summary summary = request_body.get("summary") ballotPapersNotCounted = summary['ballotPapersNotCounted'] remainingBallotPapers = summary['remainingBallotPapers'] if ballotPapersNotCounted is not None and remainingBallotPapers is not None: TallySheetVersionRow_PRE_34_summary.create( electionId=tallySheetVersion.submission.electionId, tallySheetVersionId=tallySheetVersion.tallySheetVersionId, ballotPapersNotCounted=ballotPapersNotCounted, remainingBallotPapers=remainingBallotPapers) if tally_sheet_content is not None: for row in tally_sheet_content: party_count_body = RequestBody(row) tallySheetVersion.add_row( electionId=tallySheetVersion.submission.electionId, candidateId=party_count_body.get("candidateId"), preferenceCount=party_count_body.get("preferenceCount"), preferenceNumber=party_count_body.get("preferenceNumber"), ) pollingStationId = party_count_body.get("areaId") candidateId = party_count_body.get("candidateId") preferenceCount = party_count_body.get("preferenceCount") preferenceNumber = party_count_body.get("preferenceNumber") secondPreferenceCount = 0 thirdPreferenceCount = 0 if preferenceNumber == 2: secondPreferenceCount = preferenceCount if preferenceNumber == 3: thirdPreferenceCount = preferenceCount if election is not None: existingStatus = StatusPRE34.get_status_record( electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, candidateId=candidateId) if existingStatus is None: StatusPRE34.create( voteType=voteType, status=status, electionId=electionId, electoralDistrictId=electoralDistrictId, pollingDivisionId=pollingDivisionId, countingCentreId=countingCentreId, secondPreferenceCount=secondPreferenceCount, thirdPreferenceCount=thirdPreferenceCount, candidateId=candidateId, ballotPapersNotCounted=ballotPapersNotCounted, remainingBallotPapers=remainingBallotPapers) else: existingStatus.voteType = voteType existingStatus.electionId = electionId existingStatus.electoralDistrictId = electoralDistrictId existingStatus.pollingDivisionId = pollingDivisionId existingStatus.countingCentreId = countingCentreId existingStatus.ballotPapersNotCounted = ballotPapersNotCounted existingStatus.remainingBallotPapers = remainingBallotPapers if secondPreferenceCount is not 0: existingStatus.secondPreferenceCount = secondPreferenceCount if thirdPreferenceCount is not 0: existingStatus.thirdPreferenceCount = thirdPreferenceCount existingStatus.candidateId = candidateId 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