Add previous ability data structure

This commit is contained in:
Ryan Voice 2023-02-16 15:18:04 -08:00
parent 14c2f06bfb
commit fb893e4177
11 changed files with 196 additions and 0 deletions

View file

@ -1913,6 +1913,17 @@ def _build_pokemons():
build_generic((PokemonAbility,), "pokemon_abilities.csv", csv_record_to_objects) 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): def csv_record_to_objects(info):
yield PokemonDexNumber( yield PokemonDexNumber(
pokemon_species_id=int(info[0]), pokemon_species_id=int(info[0]),

View 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
1 pokemon_id generation_id ability_id is_hidden slot
2 94 6 26 0 1
3 145 5 31 1 3
4 243 6 10 1 3
5 244 6 18 1 3
6 245 6 11 1 3
7 543 5 95 1 3
8 544 5 95 1 3
9 545 5 95 1 3
10 607 5 23 1 3
11 608 5 23 1 3
12 609 5 23 1 3

View file

@ -41,6 +41,13 @@ array_relationships:
table: table:
name: pokemon_v2_pokemonability name: pokemon_v2_pokemonability
schema: public schema: public
- name: pokemon_v2_pokemonabilitiespast
using:
foreign_key_constraint_on:
column: ability_id
table:
name: pokemon_v2_pokemonabilitypast
schema: public
select_permissions: select_permissions:
- permission: - permission:
allow_aggregations: true allow_aggregations: true

View file

@ -13,6 +13,13 @@ array_relationships:
table: table:
name: pokemon_v2_ability name: pokemon_v2_ability
schema: public 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 - name: pokemon_v2_generationnames
using: using:
foreign_key_constraint_on: foreign_key_constraint_on:

View file

@ -20,6 +20,13 @@ array_relationships:
table: table:
name: pokemon_v2_pokemonability name: pokemon_v2_pokemonability
schema: public 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 - name: pokemon_v2_pokemonforms
using: using:
foreign_key_constraint_on: foreign_key_constraint_on:

View file

@ -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

View file

@ -107,6 +107,7 @@
- "!include public_pokemon_v2_pokedexversiongroup.yaml" - "!include public_pokemon_v2_pokedexversiongroup.yaml"
- "!include public_pokemon_v2_pokemon.yaml" - "!include public_pokemon_v2_pokemon.yaml"
- "!include public_pokemon_v2_pokemonability.yaml" - "!include public_pokemon_v2_pokemonability.yaml"
- "!include public_pokemon_v2_pokemonabilitypast.yaml"
- "!include public_pokemon_v2_pokemoncolor.yaml" - "!include public_pokemon_v2_pokemoncolor.yaml"
- "!include public_pokemon_v2_pokemoncolorname.yaml" - "!include public_pokemon_v2_pokemoncolorname.yaml"
- "!include public_pokemon_v2_pokemondexnumber.yaml" - "!include public_pokemon_v2_pokemondexnumber.yaml"

View 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,
},
),
]

View file

@ -1689,6 +1689,14 @@ class PokemonAbility(HasPokemon, HasAbility):
slot = models.IntegerField() 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): class PokemonColor(HasName):
pass pass

View file

@ -366,6 +366,16 @@ class PokemonAbilitySerializer(serializers.ModelSerializer):
fields = ("is_hidden", "slot", "ability", "pokemon") 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): class PokemonDexEntrySerializer(serializers.ModelSerializer):
entry_number = serializers.IntegerField(source="pokedex_number") entry_number = serializers.IntegerField(source="pokedex_number")
@ -2900,6 +2910,7 @@ class PokemonSpritesSerializer(serializers.ModelSerializer):
class PokemonDetailSerializer(serializers.ModelSerializer): class PokemonDetailSerializer(serializers.ModelSerializer):
abilities = serializers.SerializerMethodField("get_pokemon_abilities") abilities = serializers.SerializerMethodField("get_pokemon_abilities")
past_abilities = serializers.SerializerMethodField("get_past_pokemon_abilities")
game_indices = PokemonGameIndexSerializer( game_indices = PokemonGameIndexSerializer(
many=True, read_only=True, source="pokemongameindex" many=True, read_only=True, source="pokemongameindex"
) )
@ -2926,6 +2937,7 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
"order", "order",
"weight", "weight",
"abilities", "abilities",
"past_abilities",
"forms", "forms",
"game_indices", "game_indices",
"held_items", "held_items",
@ -3063,6 +3075,42 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
return abilities 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): def get_pokemon_types(self, obj):
poke_type_objects = PokemonType.objects.filter(pokemon=obj) poke_type_objects = PokemonType.objects.filter(pokemon=obj)

View file

@ -1698,6 +1698,18 @@ class APIData:
return pokemon_ability 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 @classmethod
def setup_pokemon_stat_data(cls, pokemon, base_stat=10, effort=10): def setup_pokemon_stat_data(cls, pokemon, base_stat=10, effort=10):