def __write_round_to_database( self, db_session: DbSession, election_id: str, votes: List[Tuple[str, int]], round: int, affected_candidate: str, candidate_round_action: RoundAction, ): db_session.add(Round(election_id=election_id, round_number=round)) db_session.flush() db_session.add_all( [ CandidateRoundResult( election_id=election_id, round_number=round, business_id=candidate_id, number_of_rank_one_votes=number_of_rank_one_votes_for_candidate, round_action=None if candidate_id != affected_candidate else candidate_round_action, ) for candidate_id, number_of_rank_one_votes_for_candidate in votes ] ) db_session.flush()
def consume(self, election_id: str): db_session = DbSession() Election.delete_election_results_for_election(db_session, election_id) db_session.flush() db_session.commit() rankings_by_voter: Dict[ str, List[str] ] = Election.get_rankings_by_user_for_election(db_session, election_id) self.__run_election_round( db_session=db_session, election_id=election_id, rankings=list(rankings_by_voter.values()), ) db_session.commit() ElectionUpdateStream.for_election(election_id).publish_message( ServerSentEvent( type=ElectionUpdateEventType.RESULTS_UPDATED, payload=None ) )