Esempio n. 1
0
def add_to_team(username, teamname):
    team = json_util.loads(query_team(username=username, teamname=teamname))[0]
    num_poke = len(team['pokemon'])
    if num_poke < 6:
        data = request.get_json() or {}
        print(data)
        if 'pokemon' in data and 'level' in data:
            if 0 < data['level'] <= 100:
                poke, exists = create_pokemon(data['pokemon'], data['level'])
                if exists:
                    team['pokemon'].append(poke)
                    upload_team(team)
                    return jsonify({
                        'success': True,
                        '_meta': {},
                        "team": team
                    })
                else:
                    return bad_request(
                        'Pokemon was not found in database. Please enter a valid pokemon'
                    )
            else:
                return bad_request("Pokemon level must be between 1 and 100!")
        else:
            return bad_request("Pokemon and level must be set inside body")
    else:
        return bad_request("This team is already full")
Esempio n. 2
0
def teampage(username, teamname):
    results = query_team(username, teamname)
    if len(results) == 1:
        return render_template("pokemon/teampage.html", results=results)
    else:
        flash("Unable to find " + teamname + " by " + username + ".")
        return redirect(url_for("home.index"))
Esempio n. 3
0
def addnewpokemon(username, teamname):
    results = query_team(username, teamname)[0]
    form = PokemonNew()
    # Check if we are already at cap
    if int(results['count']) > 6:
        flash("You can't add anymore pokemon to this team!")
        redirect(url_for("poke.teampage", username=username,
                         teamname=teamname))
    if current_user.username != username:
        flash("You aren't able to modify this team!")
        redirect(url_for("poke.teampage", username=username,
                         teamname=teamname))
    if form.validate_on_submit():
        if form.pokemon.data != "":
            poke, exists = create_pokemon(form.pokemon.data, form.level.data,
                                          form.item.data)
            if exists:
                results['pokemon'].append(poke)
                results['count'] = len(results['pokemon'])
            else:
                flash(form.pokemon.data +
                      " is not a valid pokemon! This data was not recorded.")
                redirect(
                    url_for('poke.createteam',
                            username=username,
                            teamname=teamname))
            upload_team(results)
            return redirect(
                url_for('poke.teampage', username=username, teamname=teamname))
    return render_template('pokemon/addpokemon.html', form=form)
Esempio n. 4
0
def query():
    form = PokemonTeamSearch()
    if form.validate_on_submit():
        username_query = form.username_query.data
        teamname_query = form.teamname_query.data
        if username_query != "" and teamname_query != "":
            results = query_team(username=username_query,
                                 teamname=teamname_query)
        elif username_query != "" and teamname_query == "":
            results = query_team(username=username_query)
        elif username_query == "" and teamname_query != "":
            results = query_team(teamname=teamname_query)
        else:
            results = query_team()
        return render_template("pokemon/queryteams.html",
                               form=form,
                               results=results)
    return render_template("pokemon/queryteams.html", form=form, results="")
Esempio n. 5
0
def deleteteam(teamname, username):
    if current_user.username == username:
        results = query_team(username, teamname)
        if len(results) != 0:
            ret = delete_team(username, teamname)
            if ret:
                flash(teamname + " deleted successfully!")
            else:
                flash(teamname + " was unable to be deleted")
                return redirect(
                    url_for('poke.teampage',
                            username=username,
                            teamname=teamname))
    return redirect(url_for('home.user', username=username))
Esempio n. 6
0
def deletepokemon(teamname, username, slot):
    if current_user.username == username:
        if 0 <= slot < 6:
            results = query_team(username, teamname)
            if len(results) != 0:
                try:
                    results = results[0]
                    ret = results['pokemon'].pop(slot)
                    flash("Removed " + str(ret) + " from " + teamname)
                    results['count'] = len(results['pokemon'])
                    upload_team(results)
                except:
                    flash("Unable to find pokemon at slot " + str(slot))
            else:
                flash("Unable to find team; It might have been moved!")
        else:
            flash(
                "You need to enter in a valid position for a pokemon on the team!"
            )
    else:
        flash("You do not have permissions to edit this team")
    return redirect(
        url_for('poke.teampage', username=username, teamname=teamname))
Esempio n. 7
0
def get_team(username, teamname):
    teams = json_util.loads(query_team(username=username, teamname=teamname))
    num_teams = len(teams)
    return jsonify({'_meta': {'total_teams': num_teams}, "teams": teams})
Esempio n. 8
0
def get_teams():
    teams = json_util.loads(query_team())
    num_teams = len(teams)
    return jsonify({'_meta': {'total_teams': num_teams}, "teams": teams})
Esempio n. 9
0
def editpokemon(teamname, username, slot):
    # Make sure we have a valid number
    if username != current_user.username:
        flash("You do not have permissions to edit this team")
        return redirect(
            url_for('poke.teampage', username=username, teamname=teamname))
    if 0 <= slot < 6:
        # Receive the json for this team
        results = query_team(username, teamname)[0]
        pokemonName = results['pokemon'][slot]['name']

        form = PokemonEdit()

        # Send Pokemon Moves List to the form
        moveList = GetPokemonMove(pokemonName)
        newList = CreatePokemonMoveSelectList(moveList)

        form.move1.choices = newList
        form.move2.choices = newList
        form.move3.choices = newList
        form.move4.choices = newList

        # Once user clicks submit
        if form.validate_on_submit():
            # Try to update
            try:
                moves = [
                    form.move1.data, form.move2.data, form.move3.data,
                    form.move4.data
                ]
                poke, exists = create_pokemon(results['pokemon'][slot]['name'],
                                              form.level.data, form.item.data,
                                              moves)
                if exists:
                    # Pokemon is valid, let's update
                    results['pokemon'][slot] = poke
                    flash("Edited Pokemon " +
                          results['pokemon'][slot]['name'] + " in " + teamname)
                    results['count'] = len(results['pokemon'])
                    upload_team(results)

                    # Show user the new team combination
                    return redirect(
                        url_for('poke.teampage',
                                username=username,
                                teamname=teamname))
                else:
                    flash("Unable to edit pokemon")
                    return redirect(
                        url_for('poke.editpokemon',
                                username=username,
                                teamname=teamname,
                                slot=slot))
            except Exception as E:
                flash("Unable to find pokemon at slot " + str(slot))
        # Tell user current information about pokemon
        elif request.method == 'GET':
            # Total number of pokemon on this team
            print(results)
            count = results['count']
            print("DETAILS", slot, count)
            if slot >= count and count <= 5:
                return redirect(
                    url_for('poke.addnewpokemon',
                            username=username,
                            teamname=teamname))
            elif slot >= count >= 6:
                flash('Unable to update that pokemon')
                return redirect(
                    url_for('poke.teampage',
                            username=username,
                            teamname=teamname))
            form.level.data = results['pokemon'][slot]['level']
            form.item.data = results['pokemon'][slot]['item']
        return render_template('pokemon/editpokemon.html',
                               form=form,
                               pokemon_name=results['pokemon'][slot]['name'])

    else:
        flash(
            "You need to enter in a valid position for a pokemon on the team!")
        return redirect(
            url_for('poke.teampage', username=username, teamname=teamname))
Esempio n. 10
0
def editteam(teamname, username):
    if current_user.username != username:
        flash("You do not have permissions to edit this team")
        return redirect(
            url_for('poke.teampage', username=username, teamname=teamname))
    form = EditTeam()
    results = query_team(username, teamname)
    if len(results) == 0 or current_user.username != username:
        flash("Unable to edit team " + teamname)
        redirect(url_for('home.user', username=username))
    else:
        results = results[0]
    if form.validate_on_submit():
        poketeam = {
            "UserName": username,
            "TeamName": teamname,
            "TimeStamp": str(datetime.now())
        }
        pokemon = []
        # Scan the data in the forms and place inside a dictionary
        if form.pokemon1.data != "":
            poke, exists = create_pokemon(form.pokemon1.data,
                                          form.poke1level.data,
                                          form.pokemonHeldItem1.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon1.data +
                      " is not a valid pokemon! This data was not recorded.")

        if form.pokemon2.data != "":
            poke, exists = create_pokemon(form.pokemon2.data,
                                          form.poke2level.data,
                                          form.pokemonHeldItem2.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon2.data +
                      " is not a valid pokemon! This data was not recorded.")

        if form.pokemon3.data != "":
            poke, exists = create_pokemon(form.pokemon3.data,
                                          form.poke3level.data,
                                          form.pokemonHeldItem3.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon3.data +
                      " is not a valid pokemon! This data was not recorded.")

        if form.pokemon4.data != "":
            poke, exists = create_pokemon(form.pokemon4.data,
                                          form.poke4level.data,
                                          form.pokemonHeldItem4.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon4.data +
                      " is not a valid pokemon! This data was not recorded.")

        if form.pokemon5.data != "":
            poke, exists = create_pokemon(form.pokemon5.data,
                                          form.poke5level.data,
                                          form.pokemonHeldItem5.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon5.data +
                      " is not a valid pokemon! This data was not recorded.")

        if form.pokemon6.data != "":
            poke, exists = create_pokemon(form.pokemon6.data,
                                          form.poke6level.data,
                                          form.pokemonHeldItem6.data)
            if exists:
                pokemon.append(poke)
            else:
                flash(form.pokemon6.data +
                      " is not a valid pokemon! This data was not recorded.")
        poketeam['count'] = len(pokemon)
        poketeam['pokemon'] = pokemon
        upload_team(poketeam)
        return redirect(url_for("home.index"))
    elif request.method == 'GET':
        count = results['count']
        form.poke1level.data = results['pokemon'][0][
            'level'] if count > 0 else 1
        form.poke2level.data = results['pokemon'][1][
            'level'] if count > 1 else 1
        form.poke3level.data = results['pokemon'][2][
            'level'] if count > 2 else 1
        form.poke4level.data = results['pokemon'][3][
            'level'] if count > 3 else 1
        form.poke5level.data = results['pokemon'][4][
            'level'] if count > 4 else 1
        form.poke6level.data = results['pokemon'][5][
            'level'] if count > 5 else 1

        form.pokemonHeldItem1.data = results['pokemon'][0][
            'item'] if count > 0 else ""
        form.pokemonHeldItem2.data = results['pokemon'][1][
            'item'] if count > 1 else ""
        form.pokemonHeldItem3.data = results['pokemon'][2][
            'item'] if count > 2 else ""
        form.pokemonHeldItem4.data = results['pokemon'][3][
            'item'] if count > 3 else ""
        form.pokemonHeldItem5.data = results['pokemon'][4][
            'item'] if count > 4 else ""
        form.pokemonHeldItem6.data = results['pokemon'][5][
            'item'] if count > 5 else ""

        form.pokemon1.data = results['pokemon'][0]['name'] if count > 0 else ""
        form.pokemon2.data = results['pokemon'][1]['name'] if count > 1 else ""
        form.pokemon3.data = results['pokemon'][2]['name'] if count > 2 else ""
        form.pokemon4.data = results['pokemon'][3]['name'] if count > 3 else ""
        form.pokemon5.data = results['pokemon'][4]['name'] if count > 4 else ""
        form.pokemon6.data = results['pokemon'][5]['name'] if count > 5 else ""

    return render_template('pokemon/editteam.html', form=form)
Esempio n. 11
0
def user(username):
    user = User.query.filter_by(username=username).first_or_404()
    pokeList = query_team(username=username)
    return render_template('home/user.html', user=user, pokeList=pokeList)
Esempio n. 12
0
def index():
    pokeList = query_team()
    return render_template("home/index.html", pokeList=pokeList)