Beispiel #1
0
def create_tournament():
    data = request.get_json() or {}

    current_user = get_jwt_identity()
    user = User.query.filter_by(username=current_user).first()
    #print(user.id)
    #return jsonify({'data': user})
    if not user:
        return jsonify({'error': 'Invalid creditentials'}), 401

    if 'bracket' not in data:
        return jsonify({'error': 'Something went wrong.'}), 400
    if 'tournamentName' not in data:
        return jsonify({'error': 'Must include a name.'}), 400
    if 'round' not in data:
        return jsonify({'error': 'Must include a round.'}), 400

    tournament = Tournament(tournament_title=data['tournamentName'])
    title = 'Round ' + str(data['round'])
    tournament.user_id = user.id
    # #add players
    players = []
    for p in data['bracket']:
        players.append(p['playerOne'])
        players.append(p['playerTwo'])
        tournament.players = (TournamentPlayers(player_name=person)
                              for person in players)
        #tournament.matches = [Matches(round=1, player_one=p['playerOne'], player_two=p['playerTwo'])]

    for p in data['bracket']:
        match = [
            Matches(round=1,
                    title=title,
                    player_one=p['playerOne'],
                    player_two=p['playerTwo'])
        ]
        tournament.matches.extend(match)

        #matches.append(tournament)
        #tournament.players = (TournamentPlayers(player_name=person['playerOne'])for person in players)
    #tournament.matches = (Matches(round=data['round']))

    #round = Matches(round=data['round'])
    #round=data['round']
    #tournament.matches.round = round
    #playersList = players.values()
    db.session.add(tournament)
    db.session.commit()
    return jsonify({'data': tournament.to_dict()}), 200
    for row in r:
        p = State(created=timezone.now(),
                  modified=timezone.now(),
                  uid=uuid4(),
                  name=row['name'],
                  abbreviation=row['abbreviation'],
                  population=int(row['population_2017'].replace(',', '')),
                  slug=slugify(row['name']),)
        p.save()

with open('./populate/tournaments.csv') as c:
    r = DictReader(c)
    for row in r:
        p = Tournament(created=timezone.now(),
                       modified=timezone.now(),
                       uid=uuid4(),
                       name=row['name'],
                       slug=slugify(row['name']),
                       number_of_teams=int(row['number_of_teams']),)
        p.save()

with open('./populate/cities.csv') as c:
    r = DictReader(c)
    state = State.states.get(name__iexact='California')
    for row in r:
        p = City(created=timezone.now(),
                 modified=timezone.now(),
                 uid=uuid4(),
                 name=row['name'],
                 state=State.states.get(name__iexact=row['name_state']),
                 slug=slugify(row['name']+'_'+row['name_state']),)
        p.save()