def get_pokemons_from_type(type_id): pokemons = [] pokemon_types = PokemonTypes.select( PokemonTypes, Pokemon).join(Pokemon).where(PokemonTypes.type == type_id) for pokemon_type in pokemon_types: pokemons.append(pokemon_type.pokemon) return pokemons
def load_pokemon_types_from_api(name): request = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name}') pokemon_data = request.json() pokemon = get_pokemon_by_name(name) PokemonTypes.delete().where(PokemonTypes.pokemon == pokemon).execute() types = [] for api_type in pokemon_data['types']: type_name = api_type['type']['name'] type = Type.get_or_none(name=type_name) pokemon_type = PokemonTypes.create(pokemon=pokemon, type=type, slot=api_type['slot']) types.append(pokemon_type) return types
def get_types_of_pokemons(pokemons): types = PokemonTypes.select( PokemonTypes, Type).join(Type).where(PokemonTypes.type << pokemons) pokemons_by_type = {} for pokemon in types: if pokemon.type.id not in pokemons_by_type.keys(): pokemons_by_type[pokemons.type.id] = [] pokemons_by_type[pokemon.type.id].append(pokemon.pokemon) return pokemons_by_type
def search_pokemons(query, type): query = query.lower() pokemons = Pokemon.select().where(Pokemon.name.contains(query)).limit(10) if type is not None: filtered_pokemons = [] for pokemon in pokemons: # types = [t.type.name for t in pokemon.types] types = [] pokemontypes_de_ce_pokemon = PokemonTypes.select().where( PokemonTypes.pokemon == pokemon) for pokemontype in pokemontypes_de_ce_pokemon: type_name = pokemontype.type.name types.append(type_name) if type in types: filtered_pokemons.append(pokemon) return filtered_pokemons return pokemons
def search_pokemons(query=None, type=None, limit=100): pokemons = Pokemon.select() if query is not None: query = query.lower() pokemons = Pokemon.select().where(Pokemon.name.contains(query)) if type is not None and (len(type) > 0): filtered_pokemons = [] for pokemon in pokemons: # types = [t.type.name for t in pokemon.types] types = [] pokemontypes_de_ce_pokemon = PokemonTypes.select().where( PokemonTypes.pokemon == pokemon) for pokemontype in pokemontypes_de_ce_pokemon: type_name = pokemontype.type.name types.append(type_name) if type in types: filtered_pokemons.append(pokemon) return filtered_pokemons[:limit] if len( filtered_pokemons) > limit else filtered_pokemons return pokemons[:limit] if len(pokemons) > limit else pokemons
def search_pokemons(query, ability_query=None, type_query=None): query = query.lower() if query is 'all': pokemons = Pokemon.select() else: pokemons = Pokemon.select().where(Pokemon.name.contains(query)) if type_query is not None: filtered_pokemons = [] for pokemon in pokemons: # types = [t.type.name for t in pokemon.types] types = [] pokemontypes_de_ce_pokemon = PokemonTypes.select().where(PokemonTypes.pokemon == pokemon) for pokemontype in pokemontypes_de_ce_pokemon: type_name = pokemontype.type.name types.append(type_name) if type_query in types: filtered_pokemons.append(pokemon) pokemons = filtered_pokemons if ability_query is not None: filtered_pokemons = [] for pokemon in pokemons: # types = [t.type.name for t in pokemon.types] abilities = [] pokemonabilities_de_ce_pokemon = PokemonAbilities.select().where(PokemonAbilities.pokemon == pokemon) for pokemonability in pokemonabilities_de_ce_pokemon: ability_name = pokemonability.ability.name abilities.append(ability_name) if ability_query in abilities: filtered_pokemons.append(pokemon) pokemons = filtered_pokemons return pokemons[:10]