def get_object(election, row, row_key, data_key=None): if data_key is None: cell = row[row_key].strip() data_key = cell else: cell = row[data_key].strip() data_key = cell data_store_key = row_key if data_store_key == "TallySheet": data_key = "%s-%s" % (row["TallySheet"], row["Counting Centre"]) elif data_store_key == "Polling District": data_key = "%s-%s" % (row["Polling Division"], row["Polling District"]) obj = get_object_from_data_store(data_key, data_store_key) if obj is None: if data_store_key == "Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId) elif data_store_key == "Tendered Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId, ballotType=BallotTypeEnum.Tendered) elif data_store_key == "Ballot Box": obj = BallotBox.create(ballotBoxId=cell, electionId=election.electionId) elif data_store_key == "Party": obj = Party.create(partyName=cell, partySymbol=row["Party Symbol"], partyAbbreviation=row["Party Abbreviation"]) elif data_store_key == "Candidate": obj = Candidate.create(candidateName=cell) elif data_store_key == "Country": obj = Country.create(cell, electionId=election.electionId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_ALL_ISLAND_RESULTS, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum. PRE_ALL_ISLAND_RESULTS_BY_ELECTORAL_DISTRICTS, electionId=election.electionId, officeId=obj.areaId) jwt_payload["areaAssignment/nationalReportViewer"].append({ "areaId": obj.areaId, "areaName": obj.areaName }) elif data_store_key == "Electoral District": obj = ElectoralDistrict.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_ED, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD, electionId=postal_election.electionId, officeId=obj.areaId) jwt_payload["areaAssignment/electoralDistrictReportViewer"].append( { "areaId": obj.areaId, "areaName": obj.areaName }) jwt_payload[ "areaAssignment/electoralDistrictReportGenerator"].append({ "areaId": obj.areaId, "areaName": obj.areaName }) elif data_store_key == "Polling Division": obj = PollingDivision.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD, electionId=ordinary_election.electionId, officeId=obj.areaId) jwt_payload["areaAssignment/pollingDivisionReportViewer"].append({ "areaId": obj.areaId, "areaName": obj.areaName }) jwt_payload[ "areaAssignment/pollingDivisionReportGenerator"].append({ "areaId": obj.areaId, "areaName": obj.areaName }) elif data_store_key == "Polling District": obj = PollingDistrict.create(cell, electionId=election.electionId) elif data_store_key == "Election Commission": obj = ElectionCommission.create(cell, electionId=election.electionId) elif data_store_key == "District Centre": obj = DistrictCentre.create(cell, electionId=election.electionId) elif data_store_key == "Counting Centre": obj = CountingCentre.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_41, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_21, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.CE_201, electionId=election.electionId, officeId=obj.areaId) jwt_payload["areaAssignment/dataEditor"].append({ "areaId": obj.areaId, "areaName": obj.areaName }) elif data_store_key == "Polling Station": obj = PollingStation.create( cell, electionId=election.electionId, registeredVotersCount=row["Registered Voters"]) else: print("------------- Not supported yet : *%s*" % data_store_key) set_object_to_data_store(data_key, data_store_key, obj) return obj
def get_object(election, row, row_key, data_key=None): if data_key is None: cell = row[row_key].strip() data_key = cell else: cell = row[data_key].strip() data_key = cell data_store_key = row_key if data_store_key == "TallySheet": data_key = "%s-%s" % (row["TallySheet"], row["Counting Centre"]) elif data_store_key == "Polling District": data_key = "%s-%s-%s" % (row["Electoral District"], row["Polling Division"], row["Polling District"]) elif data_store_key == "Counting Centre": data_key = "%s-%s" % (row["Electoral District"], row["Counting Centre"]) elif data_store_key == "Polling Station": data_key = "%s-%s-%s-%s" % ( row["Electoral District"], row["Polling Division"], row["Polling District"], row["Polling Station"]) obj = get_object_from_data_store(data_key, data_store_key) # To identify the duplicated polling stations. if data_store_key == "Polling Station" and obj is not None: raise Exception("Duplicated polling station %s" % data_key) # To identify the duplicated counting centres. # if data_store_key == "Counting Centre" and obj is not None: # print("[Error] Duplicated counting centre %s" % data_key) if obj is None: if data_store_key == "Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId) elif data_store_key == "Tendered Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId, ballotType=BallotTypeEnum.Tendered) elif data_store_key == "Ballot Box": obj = BallotBox.create(ballotBoxId=cell, electionId=election.electionId) elif data_store_key == "Party": obj = Party.create(partyName=cell, partySymbol=row["Party Symbol"], partyAbbreviation=row["Party Abbreviation"]) elif data_store_key == "Candidate": obj = Candidate.create(candidateName=cell) elif data_store_key == "Country": obj = Country.create(cell, electionId=election.electionId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_ALL_ISLAND_RESULTS, electionId=election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum. PRE_ALL_ISLAND_RESULTS_BY_ELECTORAL_DISTRICTS, electionId=election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_34_AI, electionId=election.electionId, areaId=obj.areaId) elif data_store_key == "Electoral District": obj = ElectoralDistrict.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_ED, electionId=election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_34_ED, electionId=election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD, electionId=postal_election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_34_PD, electionId=postal_election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_34_I_RO, electionId=postal_election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_34_II_RO, electionId=election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_34, electionId=election.electionId, areaId=obj.areaId) elif data_store_key == "Polling Division": obj = PollingDivision.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD, electionId=ordinary_election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_34_PD, electionId=ordinary_election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_34_I_RO, electionId=ordinary_election.electionId, areaId=obj.areaId) elif data_store_key == "Polling District": obj = PollingDistrict.create(cell, electionId=election.electionId) elif data_store_key == "Election Commission": obj = ElectionCommission.create(cell, electionId=election.electionId) elif data_store_key == "District Centre": obj = DistrictCentre.create(cell, electionId=election.electionId) elif data_store_key == "Counting Centre": if election.voteType is VoteTypeEnum.NonPostal: obj = CountingCentre.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_41, electionId=election.electionId, areaId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.CE_201, electionId=election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_34_CO, electionId=election.electionId, areaId=obj.areaId) elif election.voteType is VoteTypeEnum.Postal: obj = CountingCentre.create( cell, electionId=election.electionId, registeredVotersCount=row["Registered Voters"]) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_41, electionId=election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.CE_201_PV, electionId=election.electionId, areaId=obj.areaId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_34_CO, electionId=election.electionId, areaId=obj.areaId) elif data_store_key == "Polling Station": obj = PollingStation.create( cell, electionId=election.electionId, registeredVotersCount=row["Registered Voters"]) else: print("------------- Not supported yet : *%s*" % data_store_key) set_object_to_data_store(data_key, data_store_key, obj) return obj
def get_object(row, row_key, data_key=None): if data_key is None: cell = row[row_key].strip() data_key = cell else: cell = row[data_key].strip() data_key = cell data_store_key = row_key if data_store_key == "TallySheet": data_key = "%s-%s" % (row["TallySheet"], row["Counting Centre"]) elif data_store_key == "Polling District": data_key = "%s-%s" % (row["Polling Division"], row["Polling District"]) obj = get_object_from_data_store(data_key, data_store_key) if obj is None: if data_store_key == "Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId) elif data_store_key == "Tendered Ballot": obj = Ballot.create(ballotId=cell, electionId=election.electionId, ballotType=BallotTypeEnum.Tendered) elif data_store_key == "Ballot Box": obj = BallotBox.create(ballotBoxId=cell, electionId=election.electionId) elif data_store_key == "Party": obj = Party.create(partyName=cell, partySymbol=row["Party Symbol"]) elif data_store_key == "Candidate": obj = Candidate.create(candidateName=cell) elif data_store_key == "Country": obj = Country.create(cell, electionId=election.electionId) TallySheet.create( tallySheetCode=TallySheetCodeEnum.PRE_ALL_ISLAND_RESULTS, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum. PRE_ALL_ISLAND_RESULTS_BY_ELECTORAL_DISTRICTS, electionId=election.electionId, officeId=obj.areaId) elif data_store_key == "Electoral District": obj = ElectoralDistrict.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_ED, electionId=election.electionId, officeId=obj.areaId) elif data_store_key == "Polling Division": obj = PollingDivision.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_30_PD_PV, electionId=election.electionId, officeId=obj.areaId) elif data_store_key == "Polling District": obj = PollingDistrict.create(cell, electionId=election.electionId) elif data_store_key == "Election Commission": obj = ElectionCommission.create(cell, electionId=election.electionId) elif data_store_key == "District Centre": obj = DistrictCentre.create(cell, electionId=election.electionId) elif data_store_key == "Counting Centre": obj = CountingCentre.create(cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_41, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_21, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.CE_201, electionId=election.electionId, officeId=obj.areaId) elif data_store_key == "Postal Vote Counting Centre": obj = PostalVoteCountingCentre.create( cell, electionId=election.electionId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_41, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.PRE_21, electionId=election.electionId, officeId=obj.areaId) TallySheet.create(tallySheetCode=TallySheetCodeEnum.CE_201_PV, electionId=election.electionId, officeId=obj.areaId) elif data_store_key == "Polling Station": obj = PollingStation.create( cell, electionId=election.electionId, registeredVotersCount=row["Registered Voters"]) elif data_store_key == "TallySheet": countingCentre = get_object(row, "Counting Centre") tallySheetCode = get_tally_sheet_code(cell) obj = TallySheet.create(tallySheetCode=tallySheetCode, electionId=election.electionId, officeId=countingCentre.areaId) if len(countingCentre.districtCentres) > 0: districtCentres = countingCentre.districtCentres[0] if tallySheetCode is TallySheetCodeEnum.PRE_41: sampleTallySheetDataRows = get_rows_from_csv( 'tallysheets/%s/%s/PRE-41.csv' % (districtCentres.areaName, countingCentre.areaName)) if len(sampleTallySheetDataRows) > 0: tallySheetVersion = TallySheetVersionPRE41.create( tallySheetId=obj.tallySheetId) for sampleTallySheetDataRow in sampleTallySheetDataRows: candidate = get_object(sampleTallySheetDataRow, "Candidate") tallySheetVersion.add_row( candidateId=candidate.candidateId, count=sampleTallySheetDataRow["Count"], countInWords=sampleTallySheetDataRow[ "Count in words"]) elif tallySheetCode is TallySheetCodeEnum.CE_201: sampleTallySheetDataRows = get_rows_from_csv( 'tallysheets/%s/%s/CE-201.csv' % (districtCentres.areaName, countingCentre.areaName)) if len(sampleTallySheetDataRows) > 0: tallySheetVersion = TallySheetVersionCE201.create( tallySheetId=obj.tallySheetId) for sampleTallySheetDataRow in sampleTallySheetDataRows: pollingStation = get_object( sampleTallySheetDataRow, "Polling Station") tallySheetVersionRow = tallySheetVersion.add_row( areaId=pollingStation.areaId, ballotBoxesIssued=sampleTallySheetDataRow[ "Issued Ballots"], ballotBoxesReceived=sampleTallySheetDataRow[ "Received Ballots"], ballotsSpoilt=sampleTallySheetDataRow[ "Spoilt Ballots"], ballotsUnused=sampleTallySheetDataRow[ "Unused Ballots"], ordinaryBallotCountFromBoxCount= sampleTallySheetDataRow[ "Box Count - Ordinary Ballots"], tenderedBallotCountFromBoxCount= sampleTallySheetDataRow[ "Box Count - Tendered Ballots"], ordinaryBallotCountFromBallotPaperAccount= sampleTallySheetDataRow[ "Ballot Paper Account - Ordinary Ballots"], tenderedBallotCountFromBallotPaperAccount= sampleTallySheetDataRow[ "Ballot Paper Account - Tendered Ballots"], ) receivedBoxIds = sampleTallySheetDataRow[ "Received Boxes"].split(",") print("####### receivedBoxIds ### ", receivedBoxIds) for receivedBoxId in receivedBoxIds: receivedBox = get_object( {"Ballot Box": receivedBoxId}, "Ballot Box") tallySheetVersionRow.add_received_ballot_box( receivedBox.stationaryItemId) else: print("------------- Not supported yet : *%s*" % data_store_key) set_object_to_data_store(data_key, data_store_key, obj) return obj