def search_pokemons(query, type=None, ability=None, limit=None): query = query.lower() pokemons = Pokemon.select().where(Pokemon.name.contains(query)) if ability is not None: filtered_pokemons = [] for pokemon in pokemons: abilities = [] pokemons_abilities_of_this_pokemon = PokemonAbilities.select( ).where(PokemonAbilities.pokemon == pokemon) # possibility to limit here? for pokemon_ability in pokemons_abilities_of_this_pokemon: ability_name = pokemon_ability.ability.name abilities.append(ability_name) if ability in abilities: filtered_pokemons.append(pokemon) return filtered_pokemons if type is not None: filtered_pokemons = [] for pokemon in pokemons: # types = [t.type.name for t in pokemon.types] types = [] pokemontypes_of_this_pokemon = PokemonTypes.select().where( PokemonTypes.pokemon == pokemon) for pokemontype in pokemontypes_of_this_pokemon: type_name = pokemontype.type.name types.append(type_name) if type in types: filtered_pokemons.append(pokemon) return filtered_pokemons[0:10] return pokemons
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 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]