Ejemplo n.º 1
0
    def post(self):
        template_params = handle_user()
        if not 'admin' in template_params:
            self.redirect('/')
            return
        tournament_url = self.request.get("url")
        # Check for input
        if tournament_url == "":
            self.redirect("/addTournaments")
            return
        dup_check = TournamentModel.query(
            TournamentModel.url == convert_url(tournament_url))
        if dup_check.get() is not None:
            render_template(self, 'error_dup_league.html', template_params)
            return
        api.set_credentials(CHALLONGE_USERNAME, CHALLONGE_API)
        #Handle tournament not found error
        tournament = tournaments.show(tournament_url)
        tournament_object = TournamentModel()
        tournament_object.name = tournament['name']
        tournament_object.url = convert_url(tournament_url)
        timestamp = parser.parse(tournament['created-at'][:-6])
        tournament_object.timestamp = timestamp
        tournament_object.put()

        # Extract the matches
        # Move participant seach to preindex'd list rather than 3 challonge requests
        match_list = matches.index(tournament['id'])
        participant_list = participants.index(tournament['id'])
        self.response.out.write(participant_list)
        for match in match_list:
            match_object = MatchModel(parent=tournament_object.key)
            # Find names via challonge
            #match_object.player1 = participants.show(tournament['id'], match['player1-id'])['name']
            #match_object.player2 = participants.show(tournament['id'], match['player2-id'])['name']
            # Find names via list
            for p in participant_list:
                if p['id'] == match['player1-id']:
                    match_object.player1 = p['name']
                if p['id'] == match['player2-id']:
                    match_object.player2 = p['name']
            if match['scores-csv'] != "":
                parts = match['scores-csv'].split('-')
                match_object.player1Score = int(parts[0])
                match_object.player2Score = int(parts[1])
            winner = participants.show(tournament['id'], match['winner-id'])
            match_object.winner = winner['name']
            match_object.label = match['identifier']
            timestamp = parser.parse(match['started-at'][:-6])
            match_object.timestamp = timestamp
            match_object.put()

        self.redirect("/listTournaments")