def get(self, id): tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckUserOwnsTournamentAndMaybeReturnStatus( self.response, users.get_current_user(), tourney): return movement = BuildMovementAndMaybeSetStatus(self.response, tourney.no_pairs, tourney.no_boards, tourney.legacy_version_id) if not movement: return name_list = GetPlayerListForTourney(tourney) scored_hands = self._TuplesToDict(tourney.ScoredHands()) unscored_hands = [] round_list = [] for round_no in xrange(1, movement.GetNumRounds() + 1): round_dict = {} round_dict["round"] = round_no round_dict["scored_hands"] = [] round_dict["unscored_hands"] = [] for team_no in xrange(1, tourney.no_pairs + 1): round = movement.GetMovement(team_no)[round_no - 1] hands = round.hands if not hands or not round.is_north: continue for hand in hands: hand_dict = { "hand": hand, "ns_pair": team_no, "ns_names": list(name_list[team_no - 1]), "ew_pair": round.opponent, "ew_names": list(name_list[round.opponent - 1]), "table": round.table } if hand in scored_hands.get(team_no, []): scored_unscored = "scored_hands" else: scored_unscored = "unscored_hands" round_dict[scored_unscored].append(hand_dict) round_dict["scored_hands"].sort(key=lambda x: x["table"]) round_dict["unscored_hands"].sort(key=lambda x: x["table"]) round_dict["scored_hands"].sort(key=lambda x: x["hand"]) round_dict["unscored_hands"].sort(key=lambda x: x["hand"]) round_list.append(round_dict) self.response.headers['Content-Type'] = 'application/json' self.response.set_status(200) self.response.out.write(json.dumps({"rounds": round_list}, indent=2))
def get(self, id): ''' Returns tournament and pair number information this pair_id. Args: id: tournament ID whose hands are being prepared. Tournament must already have been created. See api for request and response documentation. ''' tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckUserOwnsTournamentAndMaybeReturnStatus( self.response, users.get_current_user(), tourney): return movement = BuildMovementAndMaybeSetStatus(self.response, tourney.no_pairs, tourney.no_boards, tourney.legacy_version_id) if not movement: return unplayed_list = [] for pair_no in range(1, tourney.no_pairs + 1): unplayed_list.append({ "pair_no": pair_no, "hands": movement.GetUnplayedHands(pair_no) }) suggested_prep_list = [] for pair_no in range(1, tourney.no_pairs + 1): suggested_prep_list.append({ "pair_no": pair_no, "hands": movement.GetSuggestedHandPrep(pair_no) }) self.response.headers['Content-Type'] = 'application/json' self.response.set_status(200) self.response.out.write( json.dumps( { "unplayed_hands": unplayed_list, "preparation": suggested_prep_list }, indent=2))
def _CheckValidTournamentInfoAndMaybeSetStatus(self, name, no_pairs, no_boards, players=None): ''' Checks if the input is valid and sane. If not sets the response with the appropriate status and error message. Assumes no_pairs and no_boards are integers. ''' if name == "": SetErrorStatus(self.response, 400, "Invalid input", "Tournament name must be nonempty") return False elif no_pairs < 2: SetErrorStatus( self.response, 400, "Invalid input", "Number of pairs must be > 1, was {}".format(no_pairs)) return False elif no_boards < 1: SetErrorStatus( self.response, 400, "Invalid input", "Number of boards must be > 0, was {}".format(no_boards)) return False elif players: for player in players: if player['pair_no'] < 1 or player['pair_no'] > no_pairs: SetErrorStatus( self.response, 400, "Invalid Input", "Player pair must be between 1 and no_pairs, was {}.". format(player['pair_no'])) return False return BuildMovementAndMaybeSetStatus(self.response, no_pairs, no_boards) is not None