def load_pokemon_abilities_from_api(name): request = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name}') pokemon_data = request.json() pokemon = get_pokemon_by_name(name) PokemonAbilities.delete().where( PokemonAbilities.pokemon == pokemon).execute() abilities = [] for api_ability in pokemon_data['abilities']: ability_name = api_ability['ability']['name'] ability = Ability.get_or_none(name=ability_name) if ability is None: ability = Ability.create(name=ability_name, url=api_ability['ability']['url']) pokemon_ability = PokemonAbilities.create( pokemon=pokemon, ability=ability, is_hidden=api_ability['is_hidden'], slot=api_ability['slot']) abilities.append(pokemon_ability) return abilities
def load_ability_from_api(name): request = requests.get(f'https://pokeapi.co/api/v2/ability/{name}') data = request.json() generation = Generation.get_or_none(name=data['generation']['name']) if generation is None: generation = Generation.create(name=data['generation']['name']) ability = Ability.get_or_none(name=name) if ability is None: db_data = { 'name': data['name'], 'is_main_series': data['is_main_series'], 'generation': generation } ability = Ability.create(**db_data) AbilityEffects.delete().where(AbilityEffects.ability == ability).execute() for effect in data['effect_entries']: verbose_effect = VerboseEffect.get_or_none( short_effect=effect['short_effect']) if verbose_effect is None: language = Language.get_or_none(name=effect['language']['name']) if language is None: language = Language.create(name=effect['language']['name']) verbose_effect = VerboseEffect.create( effect=effect['effect'], short_effect=effect['short_effect'], language=language) ability_effect = AbilityEffects.create(ability=ability, effect=verbose_effect) return ability
def add_ability(name, generation_name): generation = Generation.get_or_none(Generation.name == generation_name) if generation is None: generation = Generation.create(name=generation_name) new_ability = Ability.create(name=name, generation=generation) return new_ability
def get_abilities(search=None, unused=False, query_gerneration=None): abilities = [] if search is None: search = "" abilities = [] for ability in Ability.select().order_by(Ability.id): if search in ability.name: abilities.append(ability) if unused: abilities = [ ability for ability in abilities if len(ability.pokemons) == 0 ] if query_gerneration is not None: filtered_abilities = [] for ability in abilities: # abilities = [] generation_de_ce_ability = Generation.select().where( Generation.id == ability.generation) # print((generation_de_ce_ability[0].name)) if query_gerneration in generation_de_ce_ability[0].name: filtered_abilities.append(ability) abilities = filtered_abilities return abilities
def search_abilities(query=None, limits=None, offset=None): """:return abilities containing query :parameter limits limit the number of abilities :parameter offset the offset""" if limits == None: limits = 10 if offset == None: offset = 0 abilities = (Ability.select(Ability.id, Ability.name, Ability.is_main_series, Ability.generation).join(Generation)).order_by( Ability.name).limit(limits).offset(offset) if query != None: abilities = abilities.where(query in Ability.name) if len(abilities) > (limits + offset): abilities = abilities.limits(limits).offset(offset) data = [ dict({ "id": ability.id, "name": ability.name, "is_main_series": ability.is_main_series, "generation": ability.generation.name }) for ability in abilities ] return data
def get_abilities(generation=None, limit=None, offset=None): abilities = Ability.select().offset(offset).limit(limit) if generation is not None: generation_id = Generation.get_or_none(name=generation) if generation_id is not None: abilities = Generation.select().where( Generation.name == generation) return abilities
def get_number_of_abilities_by_generation(generation=None): ability_of_generation = Ability.select().where( Ability.generation == generation) number_of_abilities_of_generation = ability_of_generation.count() return number_of_abilities_of_generation
def search_abilities(query, limit=None): query = query.lower() abilities = Ability.select().where(Ability.name.contains(query)) return abilities
def get_abilities(): abilities = Ability.select() return abilities
def number_of_abilities(generation_id): counter = len( Ability.select().where(Ability.generation_id == generation_id)) return counter