Skip to content

Commit e2e43d8

Browse files
authored
Merge pull request #457 from phrasmotica/past-pokemon-types
2 parents 4f4d124 + b5f014a commit e2e43d8

File tree

7 files changed

+258
-59
lines changed

7 files changed

+258
-59
lines changed

data/v2/build.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,16 @@ def csv_record_to_objects(info):
20212021

20222022
build_generic((PokemonType,), "pokemon_types.csv", csv_record_to_objects)
20232023

2024+
def csv_record_to_objects(info):
2025+
yield PokemonTypePast(
2026+
pokemon_id=int(info[0]),
2027+
generation_id=int(info[1]),
2028+
type_id=int(info[2]),
2029+
slot=int(info[3]),
2030+
)
2031+
2032+
build_generic((PokemonTypePast,), "pokemon_types_past.csv", csv_record_to_objects)
2033+
20242034

20252035
###############
20262036
# ENCOUNTER #

data/v2/csv/pokemon_types_past.csv

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pokemon_id,generation_id,type_id,slot
2+
35,5,1,1
3+
36,5,1,1
4+
39,5,1,1
5+
40,5,1,1
6+
81,1,13,1
7+
82,1,13,1
8+
122,5,14,1
9+
173,5,1,1
10+
174,5,1,1
11+
175,5,1,1
12+
176,5,1,1
13+
176,5,3,2
14+
183,5,11,1
15+
184,5,11,1
16+
209,5,1,1
17+
210,5,1,1
18+
280,5,14,1
19+
281,5,14,1
20+
282,5,14,1
21+
298,5,1,1
22+
303,5,9,1
23+
439,5,14,1
24+
468,5,1,1
25+
468,5,3,2
26+
546,5,12,1
27+
547,5,12,1
28+
10008,4,13,1
29+
10008,4,8,2
30+
10009,4,13,1
31+
10009,4,8,2
32+
10010,4,13,1
33+
10010,4,8,2
34+
10011,4,13,1
35+
10011,4,8,2
36+
10012,4,13,1
37+
10012,4,8,2

pokemon_v2/README.md

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,106 +2266,96 @@ Pokémon are the creatures that inhabit the world of the pokemon games. They can
22662266

22672267
```json
22682268
{
2269-
"id": 12,
2270-
"name": "butterfree",
2271-
"base_experience": 178,
2272-
"height": 11,
2273-
"is_default": true,
2274-
"order": 16,
2275-
"weight": 320,
2276-
"abilities": [{
2269+
"id": 35,
2270+
"name": "clefairy",
2271+
"base_experience": 113,
2272+
"height": 6,
2273+
"is_default": true,
2274+
"order": 56,
2275+
"weight": 75,
2276+
"abilities": [{
22772277
"is_hidden": true,
22782278
"slot": 3,
22792279
"ability": {
2280-
"name": "tinted-lens",
2281-
"url": "http://pokeapi.co/api/v2/ability/110/"
2280+
"name": "friend-guard",
2281+
"url": "https://pokeapi.co/api/v2/ability/132/"
22822282
}
22832283
}],
22842284
"forms": [{
2285-
"name": "butterfree",
2286-
"url": "http://pokeapi.co/api/v2/pokemon-form/12/"
2285+
"name": "clefairy",
2286+
"url": "https://pokeapi.co/api/v2/pokemon-form/35/"
22872287
}],
22882288
"game_indices": [{
2289-
"game_index": 12,
2289+
"game_index": 35,
22902290
"version": {
22912291
"name": "white-2",
2292-
"url": "http://pokeapi.co/api/v2/version/22/"
2292+
"url": "https://pokeapi.co/api/v2/version/22/"
22932293
}
22942294
}],
22952295
"held_items": [{
22962296
"item": {
2297-
"name": "silver-powder",
2298-
"url": "http://pokeapi.co/api/v2/item/199/"
2297+
"name": "moon-stone",
2298+
"url": "https://pokeapi.co/api/v2/item/81/"
22992299
},
23002300
"version_details": [{
23012301
"rarity": 5,
23022302
"version": {
2303-
"name": "y",
2304-
"url": "http://pokeapi.co/api/v2/version/24/"
2305-
}
2306-
}]
2307-
}],
2308-
"location_area_encounters": [{
2309-
"location_area": {
2310-
"name": "kanto-route-2-south-towards-viridian-city",
2311-
"url": "http://pokeapi.co/api/v2/location-area/296/"
2312-
},
2313-
"version_details": [{
2314-
"max_chance": 10,
2315-
"encounter_details": [{
2316-
"min_level": 7,
2317-
"max_level": 7,
2318-
"condition_values": [{
2319-
"name": "time-morning",
2320-
"url": "http://pokeapi.co/api/v2/encounter-condition-value/3/"
2321-
}],
2322-
"chance": 5,
2323-
"method": {
2324-
"name": "walk",
2325-
"url": "http://pokeapi.co/api/v2/encounter-method/1/"
2326-
}
2327-
}],
2328-
"version": {
2329-
"name": "heartgold",
2330-
"url": "http://pokeapi.co/api/v2/version/15/"
2303+
"name": "ruby",
2304+
"url": "https://pokeapi.co/api/v2/version/7/"
23312305
}
23322306
}]
23332307
}],
2308+
"location_area_encounters": "/api/v2/pokemon/35/encounters",
23342309
"moves": [{
23352310
"move": {
2336-
"name": "flash",
2337-
"url": "http://pokeapi.co/api/v2/move/148/"
2311+
"name": "pound",
2312+
"url": "https://pokeapi.co/api/v2/move/1/"
23382313
},
23392314
"version_group_details": [{
2340-
"level_learned_at": 0,
2315+
"level_learned_at": 1,
23412316
"version_group": {
2342-
"name": "x-y",
2343-
"url": "http://pokeapi.co/api/v2/version-group/15/"
2317+
"name": "red-blue",
2318+
"url": "https://pokeapi.co/api/v2/version-group/1/"
23442319
},
23452320
"move_learn_method": {
2346-
"name": "machine",
2347-
"url": "http://pokeapi.co/api/v2/move-learn-method/4/"
2321+
"name": "level-up",
2322+
"url": "https://pokeapi.co/api/v2/move-learn-method/1/"
23482323
}
23492324
}]
23502325
}],
23512326
"species": {
2352-
"name": "butterfree",
2353-
"url": "http://pokeapi.co/api/v2/pokemon-species/12/"
2327+
"name": "clefairy",
2328+
"url": "https://pokeapi.co/api/v2/pokemon-species/35/"
23542329
},
23552330
"stats": [{
2356-
"base_stat": 70,
2331+
"base_stat": 35,
23572332
"effort": 0,
23582333
"stat": {
23592334
"name": "speed",
2360-
"url": "http://pokeapi.co/api/v2/stat/6/"
2335+
"url": "https://pokeapi.co/api/v2/stat/6/"
23612336
}
23622337
}],
23632338
"types": [{
2364-
"slot": 2,
2339+
"slot": 1,
23652340
"type": {
2366-
"name": "flying",
2367-
"url": "http://pokeapi.co/api/v2/type/3/"
2341+
"name": "fairy",
2342+
"url": "https://pokeapi.co/api/v2/type/18/"
23682343
}
2344+
}],
2345+
"past_types": [{
2346+
"generation": {
2347+
"name": "generation-v",
2348+
"url": "https://pokeapi.co/api/v2/generation/5/"
2349+
},
2350+
"types": [
2351+
{
2352+
"slot": 1,
2353+
"type": {
2354+
"name": "normal",
2355+
"url": "https://pokeapi.co/api/v2/type/1/"
2356+
}
2357+
}
2358+
]
23692359
}]
23702360
}
23712361
```
@@ -2392,6 +2382,7 @@ Pokémon are the creatures that inhabit the world of the pokemon games. They can
23922382
| species | The species this pokémon belongs to | [NamedAPIResource](#namedapiresource) ([PokemonSpecies](#pokemon-species)) |
23932383
| stats | A list of base stat values for this pokémon | list [NamedAPIResource](#namedapiresource) ([Stat](#stats)) |
23942384
| types | A list of details showing types this pokémon has | list [PokemonType](#pokemontype) |
2385+
| past_types | A list of details showing types this pokémon had in previous generations | list [PokemonTypePast](#pokemontypepast) |
23952386

23962387
#### PokemonAbility
23972388

@@ -2408,6 +2399,13 @@ Pokémon are the creatures that inhabit the world of the pokemon games. They can
24082399
| slot | The order the pokémon types are listed in | integer |
24092400
| type | The type the referenced pokémon has | string |
24102401

2402+
#### PokemonTypePast
2403+
2404+
| Name | Description | Data Type |
2405+
| ---- | ----------- | --------- |
2406+
| generation | The last generation in which the referenced pokémon had the listed types | [NamedAPIResource](#namedapiresource) ([Generation](#generations)) |
2407+
| types | The types the referenced pokémon had up to and including the listed generation | list [PokemonType](#pokemontype) |
2408+
24112409
#### LocationAreaEncounter
24122410

24132411
| Name | Description | Data Type |
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Generated by Django 2.1.11 on 2021-02-06 22:03
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("pokemon_v2", "0008_auto_20201123_2045"),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name="PokemonTypePast",
16+
fields=[
17+
(
18+
"id",
19+
models.AutoField(
20+
auto_created=True,
21+
primary_key=True,
22+
serialize=False,
23+
verbose_name="ID",
24+
),
25+
),
26+
("slot", models.IntegerField()),
27+
(
28+
"generation",
29+
models.ForeignKey(
30+
blank=True,
31+
null=True,
32+
on_delete=django.db.models.deletion.CASCADE,
33+
related_name="pokemontypepast",
34+
to="pokemon_v2.Generation",
35+
),
36+
),
37+
(
38+
"pokemon",
39+
models.ForeignKey(
40+
blank=True,
41+
null=True,
42+
on_delete=django.db.models.deletion.CASCADE,
43+
related_name="pokemontypepast",
44+
to="pokemon_v2.Pokemon",
45+
),
46+
),
47+
(
48+
"type",
49+
models.ForeignKey(
50+
blank=True,
51+
null=True,
52+
on_delete=django.db.models.deletion.CASCADE,
53+
related_name="pokemontypepast",
54+
to="pokemon_v2.Type",
55+
),
56+
),
57+
],
58+
options={
59+
"abstract": False,
60+
},
61+
),
62+
]

pokemon_v2/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,12 @@ class PokemonType(HasPokemon, HasType):
18721872
slot = models.IntegerField()
18731873

18741874

1875+
# model for a Pokemon's types that were used until a given generation
1876+
class PokemonTypePast(HasPokemon, HasType, HasGeneration):
1877+
1878+
slot = models.IntegerField()
1879+
1880+
18751881
class PokemonSprites(HasPokemon):
18761882

18771883
sprites = models.CharField(max_length=20000)

pokemon_v2/serializers.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ class Meta:
386386
fields = ("slot", "pokemon", "type")
387387

388388

389+
class PokemonTypePastSerializer(serializers.ModelSerializer):
390+
391+
generation = GenerationSummarySerializer()
392+
type = TypeSummarySerializer()
393+
394+
class Meta:
395+
model = PokemonTypePast
396+
fields = ("pokemon", "generation", "slot", "type")
397+
398+
389399
class PokedexVersionGroupSerializer(serializers.ModelSerializer):
390400

391401
pokedex = PokedexSummarySerializer()
@@ -2721,6 +2731,7 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
27212731
species = PokemonSpeciesSummarySerializer(source="pokemon_species")
27222732
stats = PokemonStatSerializer(many=True, read_only=True, source="pokemonstat")
27232733
types = serializers.SerializerMethodField("get_pokemon_types")
2734+
past_types = serializers.SerializerMethodField("get_past_pokemon_types")
27242735
forms = PokemonFormSummarySerializer(
27252736
many=True, read_only=True, source="pokemonform"
27262737
)
@@ -2748,6 +2759,7 @@ class Meta:
27482759
"sprites",
27492760
"stats",
27502761
"types",
2762+
"past_types",
27512763
)
27522764

27532765
def get_pokemon_sprites(self, obj):
@@ -2887,6 +2899,42 @@ def get_pokemon_types(self, obj):
28872899

28882900
return poke_types
28892901

2902+
def get_past_pokemon_types(self, obj):
2903+
2904+
poke_past_type_objects = PokemonTypePast.objects.filter(pokemon=obj)
2905+
poke_past_types = PokemonTypePastSerializer(
2906+
poke_past_type_objects, many=True, context=self.context
2907+
).data
2908+
2909+
# post-process to the form we want
2910+
current_generation = ""
2911+
past_obj = {}
2912+
final_data = []
2913+
for poke_past_type in poke_past_types:
2914+
del poke_past_type["pokemon"]
2915+
2916+
generation = poke_past_type["generation"]["name"]
2917+
if generation != current_generation:
2918+
current_generation = generation
2919+
past_obj = {}
2920+
2921+
# create past types object for this generation
2922+
past_obj["generation"] = poke_past_type["generation"]
2923+
del poke_past_type["generation"]
2924+
2925+
# create types array
2926+
past_obj["types"] = [poke_past_type]
2927+
2928+
# add to past types array
2929+
final_data.append(past_obj)
2930+
2931+
else:
2932+
# add to existing array for this generation
2933+
del poke_past_type["generation"]
2934+
past_obj["types"].append(poke_past_type)
2935+
2936+
return final_data
2937+
28902938
def get_encounters(self, obj):
28912939

28922940
return reverse("pokemon_encounters", kwargs={"pokemon_id": obj.pk})

0 commit comments

Comments
 (0)