Create serializer for past Pokemon types

This commit is contained in:
Julian Lawrance 2019-11-30 20:57:15 +00:00
parent 385a2d62cc
commit 87ce1a593a

View file

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