mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-21 19:03:08 +00:00
Add previous ability data structure
This commit is contained in:
parent
14c2f06bfb
commit
fb893e4177
11 changed files with 196 additions and 0 deletions
|
@ -1913,6 +1913,17 @@ def _build_pokemons():
|
|||
|
||||
build_generic((PokemonAbility,), "pokemon_abilities.csv", csv_record_to_objects)
|
||||
|
||||
def csv_record_to_objects(info):
|
||||
yield PokemonAbilityPast(
|
||||
pokemon_id=int(info[0]),
|
||||
generation_id=int(info[1]),
|
||||
ability_id=int(info[2]),
|
||||
is_hidden=bool(int(info[3])),
|
||||
slot=int(info[4]),
|
||||
)
|
||||
|
||||
build_generic((PokemonAbilityPast,), "pokemon_abilities_past.csv", csv_record_to_objects)
|
||||
|
||||
def csv_record_to_objects(info):
|
||||
yield PokemonDexNumber(
|
||||
pokemon_species_id=int(info[0]),
|
||||
|
|
12
data/v2/csv/pokemon_abilities_past.csv
Normal file
12
data/v2/csv/pokemon_abilities_past.csv
Normal file
|
@ -0,0 +1,12 @@
|
|||
pokemon_id,generation_id,ability_id,is_hidden,slot
|
||||
94,6,26,0,1
|
||||
145,5,31,1,3
|
||||
243,6,10,1,3
|
||||
244,6,18,1,3
|
||||
245,6,11,1,3
|
||||
543,5,95,1,3
|
||||
544,5,95,1,3
|
||||
545,5,95,1,3
|
||||
607,5,23,1,3
|
||||
608,5,23,1,3
|
||||
609,5,23,1,3
|
|
|
@ -41,6 +41,13 @@ array_relationships:
|
|||
table:
|
||||
name: pokemon_v2_pokemonability
|
||||
schema: public
|
||||
- name: pokemon_v2_pokemonabilitiespast
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
column: ability_id
|
||||
table:
|
||||
name: pokemon_v2_pokemonabilitypast
|
||||
schema: public
|
||||
select_permissions:
|
||||
- permission:
|
||||
allow_aggregations: true
|
||||
|
|
|
@ -13,6 +13,13 @@ array_relationships:
|
|||
table:
|
||||
name: pokemon_v2_ability
|
||||
schema: public
|
||||
- name: pokemon_v2_abilitiespast
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
column: generation_id
|
||||
table:
|
||||
name: pokemon_v2_abilitypast
|
||||
schema: public
|
||||
- name: pokemon_v2_generationnames
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
|
|
|
@ -20,6 +20,13 @@ array_relationships:
|
|||
table:
|
||||
name: pokemon_v2_pokemonability
|
||||
schema: public
|
||||
- name: pokemon_v2_pokemonabilitiespast
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
column: pokemon_id
|
||||
table:
|
||||
name: pokemon_v2_pokemonabilitypast
|
||||
schema: public
|
||||
- name: pokemon_v2_pokemonforms
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
table:
|
||||
name: pokemon_v2_pokemonabilitypast
|
||||
schema: public
|
||||
object_relationships:
|
||||
- name: pokemon_v2_generation
|
||||
using:
|
||||
foreign_key_constraint_on: generation_id
|
||||
- name: pokemon_v2_pokemon
|
||||
using:
|
||||
foreign_key_constraint_on: pokemon_id
|
||||
- name: pokemon_v2_ability
|
||||
using:
|
||||
foreign_key_constraint_on: ability_id
|
||||
select_permissions:
|
||||
- permission:
|
||||
allow_aggregations: true
|
||||
columns: "*"
|
||||
filter: {}
|
||||
limit: 100000
|
||||
role: anon
|
|
@ -107,6 +107,7 @@
|
|||
- "!include public_pokemon_v2_pokedexversiongroup.yaml"
|
||||
- "!include public_pokemon_v2_pokemon.yaml"
|
||||
- "!include public_pokemon_v2_pokemonability.yaml"
|
||||
- "!include public_pokemon_v2_pokemonabilitypast.yaml"
|
||||
- "!include public_pokemon_v2_pokemoncolor.yaml"
|
||||
- "!include public_pokemon_v2_pokemoncolorname.yaml"
|
||||
- "!include public_pokemon_v2_pokemondexnumber.yaml"
|
||||
|
|
63
pokemon_v2/migrations/0013_pokemonabilitypast.py
Normal file
63
pokemon_v2/migrations/0013_pokemonabilitypast.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("pokemon_v2", "0012_auto_20220626_1402"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="PokemonAbilityPast",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("is_hidden", models.BooleanField(default=False)),
|
||||
("slot", models.IntegerField()),
|
||||
(
|
||||
"generation",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="pokemonabilitypast",
|
||||
to="pokemon_v2.Generation",
|
||||
),
|
||||
),
|
||||
(
|
||||
"ability",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="pokemonabilitypast",
|
||||
to="pokemon_v2.Ability",
|
||||
),
|
||||
),
|
||||
(
|
||||
"pokemon",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="pokemonabilitypast",
|
||||
to="pokemon_v2.Pokemon",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
]
|
|
@ -1687,6 +1687,14 @@ class PokemonAbility(HasPokemon, HasAbility):
|
|||
is_hidden = models.BooleanField(default=False)
|
||||
|
||||
slot = models.IntegerField()
|
||||
|
||||
|
||||
# model for a Pokemon's abilities that were used until a given generation
|
||||
class PokemonAbilityPast(HasPokemon, HasAbility, HasGeneration):
|
||||
|
||||
is_hidden = models.BooleanField(default=False)
|
||||
|
||||
slot = models.IntegerField()
|
||||
|
||||
|
||||
class PokemonColor(HasName):
|
||||
|
|
|
@ -366,6 +366,16 @@ class PokemonAbilitySerializer(serializers.ModelSerializer):
|
|||
fields = ("is_hidden", "slot", "ability", "pokemon")
|
||||
|
||||
|
||||
class PokemonAbilityPastSerializer(serializers.ModelSerializer):
|
||||
|
||||
generation = GenerationSummarySerializer()
|
||||
ability = AbilitySummarySerializer()
|
||||
|
||||
class Meta:
|
||||
model = PokemonAbilityPast
|
||||
fields = ("is_hidden", "pokemon", "generation", "slot", "ability")
|
||||
|
||||
|
||||
class PokemonDexEntrySerializer(serializers.ModelSerializer):
|
||||
|
||||
entry_number = serializers.IntegerField(source="pokedex_number")
|
||||
|
@ -2900,6 +2910,7 @@ class PokemonSpritesSerializer(serializers.ModelSerializer):
|
|||
class PokemonDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
abilities = serializers.SerializerMethodField("get_pokemon_abilities")
|
||||
past_abilities = serializers.SerializerMethodField("get_past_pokemon_abilities")
|
||||
game_indices = PokemonGameIndexSerializer(
|
||||
many=True, read_only=True, source="pokemongameindex"
|
||||
)
|
||||
|
@ -2926,6 +2937,7 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
|
|||
"order",
|
||||
"weight",
|
||||
"abilities",
|
||||
"past_abilities",
|
||||
"forms",
|
||||
"game_indices",
|
||||
"held_items",
|
||||
|
@ -3062,6 +3074,42 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
|
|||
abilities.append(ability)
|
||||
|
||||
return abilities
|
||||
|
||||
def get_past_pokemon_abilities(self, obj):
|
||||
|
||||
pokemon_past_ability_objects = PokemonAbilityPast.objects.filter(pokemon=obj)
|
||||
pokemon_past_abilities = PokemonAbilityPastSerializer(
|
||||
pokemon_past_ability_objects, many=True, context=self.context
|
||||
).data
|
||||
|
||||
# post-process to the form we want
|
||||
current_generation = ""
|
||||
past_obj = {}
|
||||
final_data = []
|
||||
for pokemon_past_ability in pokemon_past_abilities:
|
||||
del pokemon_past_ability["pokemon"]
|
||||
|
||||
generation = pokemon_past_ability["generation"]["name"]
|
||||
if generation != current_generation:
|
||||
current_generation = generation
|
||||
past_obj = {}
|
||||
|
||||
# create past abilities object for this generation
|
||||
past_obj["generation"] = pokemon_past_ability["generation"]
|
||||
del pokemon_past_ability["generation"]
|
||||
|
||||
# create abilities array
|
||||
past_obj["abilities"] = [pokemon_past_ability]
|
||||
|
||||
# add to past abilities array
|
||||
final_data.append(past_obj)
|
||||
|
||||
else:
|
||||
# add to existing array for this generation
|
||||
del pokemon_past_ability["generation"]
|
||||
past_obj["abilities"].append(pokemon_past_ability)
|
||||
|
||||
return final_data
|
||||
|
||||
def get_pokemon_types(self, obj):
|
||||
|
||||
|
|
|
@ -1698,6 +1698,18 @@ class APIData:
|
|||
|
||||
return pokemon_ability
|
||||
|
||||
@classmethod
|
||||
def setup_pokemon_past_ability_data(cls, pokemon, generation, ability=None, is_hidden=False, slot=1):
|
||||
|
||||
type = type or cls.setup_ability_data(name="ablty for pkmn")
|
||||
|
||||
pokemon_ability_past = PokemonAbilityPast(
|
||||
pokemon=pokemon, generation=generation, ability=ability, is_hidden=is_hidden, slot=slot
|
||||
)
|
||||
pokemon_ability_past.save()
|
||||
|
||||
return pokemon_ability_past
|
||||
|
||||
@classmethod
|
||||
def setup_pokemon_stat_data(cls, pokemon, base_stat=10, effort=10):
|
||||
|
||||
|
|
Loading…
Reference in a new issue