def get(self, id, pair_no):
        ''' Returns the opaque pair ID for pair with pair nubmer pair_no in this
        Tournament.

        Args: 
          id: Tournament ID for this tournament.
          pair_no: The pair number whose ID has to be looked up.
    '''
        if not is_int(pair_no):
            SetErrorStatus(
                self.response, 404, "Invalid Pair Number",
                "Pair number must be an integer, was {}".format(pair_no))
            return

        tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id)
        if not tourney:
            return

        if not CheckUserOwnsTournamentAndMaybeReturnStatus(
                self.response, users.get_current_user(), tourney):
            return
        player_pairs = PlayerPair._query(
            ndb.GenericProperty('pair_no') == int(pair_no),
            ancestor=tourney.key).fetch(1, projection=[PlayerPair.id])
        if not player_pairs:
            SetErrorStatus(
                self.response, 404, "Invalid Id",
                "Pair pair number {} does not exist in this " +
                "tournament".format(pair_no))
            return
        self.response.headers['Content-Type'] = 'application/json'
        self.response.set_status(200)
        self.response.out.write(
            json.dumps({'pair_id': player_pairs[0].id}, indent=2))
    def get(self, id):
        ''' Returns tournament and pair number information this pair_id.

        Args: 
          id: Tournament ID for this tournament
        Returns: 
          a list of all unique ids in order. Length will necessarily equal
          to the number of pairs in this tournament.
    '''
        tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id)
        if not tourney:
            return

        if not CheckUserOwnsTournamentAndMaybeReturnStatus(
                self.response, users.get_current_user(), tourney):
            return

        player_pairs = PlayerPair._query(ancestor=tourney.key).fetch(
            projection=[PlayerPair.id, PlayerPair.pair_no])
        player_pairs.sort(key=lambda p: p.pair_no)
        if not player_pairs:
            SetErrorStatus(
                self.response, 500, "Corrupted Data",
                "Could not find any players for this tournament. " +
                "Consider resetting tournament info.")

        self.response.headers['Content-Type'] = 'application/json'
        self.response.set_status(200)
        self.response.out.write(
            json.dumps({"pair_ids": [p.id for p in player_pairs]}, indent=2))
Exemplo n.º 3
0
    def _CheckUserHasAccessMaybeSetStatus(self, tourney, hand_no):
        '''Tests if the current user has access to the results of this hand.

    Uses the pair id code, if any, set in the request header to see if the user
    is in one of the teams that is set to play this hand in this movement. 
    Directors always have access.

    Args:
      tourney: Tournament. Current tournament.
      hand_no: Integer. Number of the hand.

    Returns:
      A (Boolean, Integer, [(Integer, Integer)]) tuple. First member is True iff
      the user should have access to the hand provided they played it (or is a
      director). The second member is the pair number of the user (0 for director).
      Only set if first member is True. The last member is the list of 
      (nw_pair, ew_pair) tuples that correspond to the pairs that play hand_no in
      this movement.
    '''
        user = users.get_current_user()
        movement = tourney.GetMovement()
        all_matchups = movement.GetListOfPlayersForHand(hand_no)
        if user and tourney.owner_id == user.user_id():
            return (True, 0, all_matchups)
        if tourney.IsUnlocked():
            SetErrorStatus(
                self.response, 403, "Forbidden User",
                "The tournament is not set up to show hand " +
                "results to players.")
            return (False, None, None)

        error = "Forbidden User"
        pair_id = GetPairIdFromRequest(self.request)
        player_pairs = PlayerPair._query(
            ndb.GenericProperty('id') == pair_id).fetch(
                1, projection=[PlayerPair.pair_no])
        if not pair_id or not player_pairs:
            SetErrorStatus(
                self.response, 403, error,
                "User does not own tournament and is not authenticated " +
                "with a pair code to see the results of this hand.")
            return (False, None, None)
        pair_no = player_pairs[0].pair_no
        if not self._PlayerInMatchupList(pair_no, all_matchups):
            SetErrorStatus(self.response, 403, error,
                           "User does not play this hand.")
            return (False, None, None)
        return (True, pair_no, all_matchups)
    def get(self, pair_id):
        ''' Returns tournament and pair number information this pair_id.

        Args: 
          pair_id: Opaque secret ID used to look up information for the user.
    '''
        player_pairs = PlayerPair._query(
            ndb.GenericProperty('id') == pair_id).fetch(
                projection=[PlayerPair.pair_no])
        if not player_pairs:
            SetErrorStatus(self.response, 404, "Invalid Id",
                           "Pair number with this ID does not exist")
            return
        info_dict = {
            'tournament_infos': [{
                'pair_no': p.pair_no,
                'tournament_id': str(p.key.parent().id())
            } for p in player_pairs]
        }

        self.response.headers['Content-Type'] = 'application/json'
        self.response.set_status(200)
        self.response.out.write(json.dumps(info_dict, indent=2))