mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-14 15:57:07 +00:00
Create serializer for past Pokemon types
This commit is contained in:
parent
385a2d62cc
commit
87ce1a593a
1 changed files with 46 additions and 0 deletions
|
@ -429,6 +429,16 @@ class PokemonTypeSerializer(serializers.ModelSerializer):
|
|||
fields = ('slot', 'pokemon', 'type')
|
||||
|
||||
|
||||
class PokemonTypePastSerializer(serializers.ModelSerializer):
|
||||
|
||||
generation = GenerationSummarySerializer()
|
||||
type = TypeSummarySerializer()
|
||||
|
||||
class Meta:
|
||||
model = PokemonTypePast
|
||||
fields = ('pokemon', 'generation', 'slot', 'type')
|
||||
|
||||
|
||||
class PokedexVersionGroupSerializer(serializers.ModelSerializer):
|
||||
|
||||
pokedex = PokedexSummarySerializer()
|
||||
|
@ -2489,6 +2499,7 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
|
|||
species = PokemonSpeciesSummarySerializer(source="pokemon_species")
|
||||
stats = PokemonStatSerializer(many=True, read_only=True, source="pokemonstat")
|
||||
types = serializers.SerializerMethodField('get_pokemon_types')
|
||||
past_types = serializers.SerializerMethodField('get_past_pokemon_types')
|
||||
forms = PokemonFormSummarySerializer(many=True, read_only=True, source="pokemonform")
|
||||
held_items = serializers.SerializerMethodField('get_pokemon_held_items')
|
||||
location_area_encounters = serializers.SerializerMethodField('get_encounters')
|
||||
|
@ -2514,6 +2525,7 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
|
|||
'sprites',
|
||||
'stats',
|
||||
'types',
|
||||
'past_types',
|
||||
)
|
||||
|
||||
def get_pokemon_sprites(self, obj):
|
||||
|
@ -2634,6 +2646,40 @@ class PokemonDetailSerializer(serializers.ModelSerializer):
|
|||
|
||||
return poke_types
|
||||
|
||||
def get_past_pokemon_types(self, obj):
|
||||
|
||||
poke_past_type_objects = PokemonTypePast.objects.filter(pokemon=obj)
|
||||
poke_past_types = PokemonTypePastSerializer(poke_past_type_objects, many=True, context=self.context).data
|
||||
|
||||
# post-process to the form we want
|
||||
current_generation = ""
|
||||
past_obj = {}
|
||||
final_data = []
|
||||
for poke_past_type in poke_past_types:
|
||||
del poke_past_type['pokemon']
|
||||
|
||||
generation = poke_past_type['generation']['name']
|
||||
if generation != current_generation:
|
||||
current_generation = generation
|
||||
past_obj = {}
|
||||
|
||||
# create past types object for this generation
|
||||
past_obj['generation'] = poke_past_type['generation']
|
||||
del poke_past_type['generation']
|
||||
|
||||
# create types array
|
||||
past_obj['types'] = [poke_past_type]
|
||||
|
||||
# add to past types array
|
||||
final_data.append(past_obj)
|
||||
|
||||
else:
|
||||
# add to existing array for this generation
|
||||
del poke_past_type['generation']
|
||||
past_obj['types'].append(poke_past_type)
|
||||
|
||||
return final_data
|
||||
|
||||
def get_encounters(self, obj):
|
||||
|
||||
return reverse('pokemon_encounters', kwargs={'pokemon_id': obj.pk})
|
||||
|
|
Loading…
Reference in a new issue