Exemplo n.º 1
0
    def new_session(
        self,
        search_request: BusinessSearchRequest,
        dinner_party_active_id: Optional[str],
        user_maybe: Optional[SerializableBasicUser],
    ) -> SearchSession:
        session_id = str(uuid4())

        dinner_party_id = None
        if dinner_party_active_id is not None:
            dinner_party = self.__rcv_manager.get_active_election_by_active_id(
                dinner_party_active_id)
            if dinner_party is None:
                raise HttpException(
                    message=
                    f"Election with active id {dinner_party_active_id} not found",
                    error_code=ErrorCode.NO_ELECTION_FOUND)
            dinner_party_id = dinner_party.id

        new_session = SearchSession(
            id=session_id,
            search_request=search_request,
            dinner_party_id=dinner_party_id,
            created_by_id=None if user_maybe is None else user_maybe.id)
        db_session = DbSession()
        db_session.add(new_session)
        db_session.commit()
        return new_session
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
    def create_anonymous_user(self, nickname: str) -> BasicUser:
        user_id = str(uuid4())
        user: BasicUser = BasicUser(id=user_id, nickname=nickname)
        db_session = DbSession()
        db_session.add(user)
        db_session.commit()

        return user
Exemplo n.º 4
0
 def create_election(self, user: SerializableBasicUser) -> Election:
     db_session = DbSession()
     election_id = str(uuid4())
     while True:
         try:
             election = Election(
                 id=election_id,
                 active_id=self.__generate_election_active_id(),
                 election_creator_id=user.id,
             )
             db_session.add(election)
             db_session.commit()
             return election
         except BaseException as error:
             raise error
Exemplo n.º 5
0
    def add_candidate(self, active_id: str, business_id: str,
                      user_id: str) -> bool:
        db_session = DbSession()

        partial_election = Election.get_active_election_by_active_id(
            db_session=db_session,
            active_id=active_id,
            query_modifier=lambda x: x.options(
                load_only(Election.id, Election.election_status)),
        )
        if partial_election is None:
            raise HttpException(
                message=f"Election with active id: {active_id} not found",
                status_code=404,
            )
        if partial_election.election_status != ElectionStatus.IN_CREATION:
            raise InvalidElectionStateException(
                message=
                f"Cannot add a candidate to an election with status: {partial_election.election_status.value}"
            )

        # Verify business exists? Cache business
        # Calculate distance

        candidate = Candidate(
            election_id=partial_election.id,
            business_id=business_id,
            distance=0,
            nominator_id=user_id,
        )
        db_session.add(candidate)
        try:
            db_session.commit()
        except IntegrityError as error:
            if is_unique_key_error(error):
                return False
            raise error
        # TODO: Accelerate fetching process here
        business: DisplayableBusiness = self.__business_manager.get_displayable_business(
            business_id=business_id)
        # TODO: Maybe just pass the name stored within the cookie instead of refetching from the database
        nickname = self.__user_manager.get_nickname_by_user_id(user_id)
        ElectionUpdateStream.for_election(partial_election.id).publish_message(
            CandidateAddedEvent(business_id=business_id,
                                name=business.name,
                                nominator_nickname=nickname
                                if nickname is not None else "Unknown"))
        return True
Exemplo n.º 6
0
    def get_next_recommendation_for_session(
            self, session_id: str) -> DisplayableRecommendation:
        db_session = DbSession()
        search_session: SearchSession = SearchSession.get_session_by_id(
            db_session, session_id)
        if search_session.current_recommendation is not None:
            raise ValueError(
                "Cannot get a new recommendation. The search session has a current recommendation"
            )

        if search_session.is_complete:
            return None

        recommendation: Recommendation = self.__recommendation_manager.generate_new_recommendation_for_session(
            search_session)
        db_session.add(recommendation)
        db_session.commit()
        return self.__recommendation_manager.get_displayable_recommendation_from_recommendation(
            recommendation)