mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-13 23:37:06 +00:00
388 lines
9.9 KiB
Python
388 lines
9.9 KiB
Python
|
|
from __future__ import unicode_literals
|
|
from rest_framework import serializers
|
|
from collections import OrderedDict
|
|
|
|
"""
|
|
PokeAPI v2 serializers in order of dependency
|
|
"""
|
|
|
|
from .models import *
|
|
|
|
|
|
##########################
|
|
# LANGUAGE SERIALIZERS #
|
|
##########################
|
|
|
|
class LanguageNameSerializer(serializers.ModelSerializer):
|
|
|
|
local_language_url = serializers.HyperlinkedRelatedField(read_only='True', source="local_language_id", view_name='language-detail')
|
|
|
|
class Meta:
|
|
model = LanguageName
|
|
fields = ('name', 'local_language_url')
|
|
|
|
|
|
class LanguageSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Language
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class LanguageDetailSerializer(serializers.ModelSerializer):
|
|
|
|
names = LanguageNameSerializer(many=True, read_only=True, source='languagename_language')
|
|
|
|
class Meta:
|
|
model = Language
|
|
fields = ('name', 'official', 'iso639', 'iso3166', 'id', 'names')
|
|
|
|
|
|
########################
|
|
# REGION SERIALIZERS #
|
|
########################
|
|
|
|
class RegionNameSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = RegionName
|
|
fields = ('name', 'language')
|
|
|
|
|
|
class RegionSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Region
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class RegionDetailSerializer(serializers.ModelSerializer):
|
|
|
|
names = RegionNameSerializer(many=True, read_only=True, source="regionname")
|
|
|
|
class Meta:
|
|
model = Region
|
|
fields = ('id', 'name', 'names')
|
|
|
|
|
|
############################
|
|
# GENERATION SERIALIZERS #
|
|
############################
|
|
|
|
class GenerationNameSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = GenerationName
|
|
fields = ('name', 'language')
|
|
|
|
|
|
class GenerationSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Generation
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class GenerationDetailSerializer(serializers.ModelSerializer):
|
|
|
|
region = RegionSummarySerializer()
|
|
names = GenerationNameSerializer(many=True, read_only=True, source="generationname")
|
|
|
|
class Meta:
|
|
model = Generation
|
|
fields = ('id', 'name', 'region', 'names')
|
|
|
|
|
|
#########################
|
|
# VERSION SERIALIZERS #
|
|
#########################
|
|
|
|
class VersionNameSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = VersionName
|
|
fields = ('name', 'language')
|
|
|
|
|
|
class VersionSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Version
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class VersionDetailSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Should have a link to Version Group info but the Circular
|
|
dependency and compilation order fight eachother and I'm
|
|
not sure how to add anything other than a hyperlink
|
|
"""
|
|
|
|
names = VersionNameSerializer(many=True, read_only=True, source="versionname")
|
|
version_group_url = serializers.HyperlinkedRelatedField(read_only='True', source="version_group", view_name='versiongroup-detail')
|
|
|
|
class Meta:
|
|
model = Version
|
|
fields = ('id', 'name', 'names', 'version_group_url')
|
|
|
|
|
|
class VersionGroupSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = VersionGroup
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class VersionGroupDetailSerializer(serializers.ModelSerializer):
|
|
|
|
generation = GenerationSummarySerializer()
|
|
versions = VersionSummarySerializer(many=True, read_only=True, source="version")
|
|
|
|
class Meta:
|
|
model = VersionGroup
|
|
fields = ('id', 'name', 'order', 'generation', 'versions')
|
|
|
|
|
|
#########################
|
|
# ABILITY SERIALIZERS #
|
|
#########################
|
|
|
|
class AbilityDescriptionSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = AbilityDescription
|
|
fields = ('effect', 'short_effect', 'language')
|
|
|
|
|
|
class AbilityFlavorTextSerializer(serializers.ModelSerializer):
|
|
|
|
text = serializers.CharField(source="flavor_text")
|
|
language = LanguageSummarySerializer()
|
|
version_group = VersionGroupSummarySerializer()
|
|
|
|
class Meta:
|
|
model = AbilityFlavorText
|
|
fields = ('text', 'version_group', 'language')
|
|
|
|
|
|
class AbilityNameSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = AbilityName
|
|
fields = ('name', 'language')
|
|
|
|
|
|
class AbilitySummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Ability
|
|
|
|
|
|
class AbilityDetailSerializer(serializers.ModelSerializer):
|
|
|
|
descriptions = AbilityDescriptionSerializer(many=True, read_only=True, source="abilitydescription")
|
|
flavor_text_entries = AbilityFlavorTextSerializer(many=True, read_only=True, source="abilityflavortext")
|
|
names = AbilityNameSerializer(many=True, read_only=True, source="abilityname")
|
|
generation = GenerationSummarySerializer()
|
|
|
|
class Meta:
|
|
model = Ability
|
|
fields = (
|
|
'id',
|
|
'name',
|
|
'is_main_series',
|
|
'generation',
|
|
'names',
|
|
'descriptions',
|
|
'flavor_text_entries'
|
|
)
|
|
|
|
|
|
######################
|
|
# TYPE SERIALIZERS #
|
|
######################
|
|
|
|
class TypeEfficacySerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = TypeEfficacy
|
|
|
|
|
|
class TypeNameSerializer(serializers.ModelSerializer):
|
|
|
|
language = LanguageSummarySerializer()
|
|
|
|
class Meta:
|
|
model = TypeName
|
|
fields = ('name', 'language')
|
|
|
|
|
|
class TypeSummarySerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
model = Type
|
|
fields = ('name', 'url')
|
|
|
|
|
|
class TypeDetailSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Type resource
|
|
"""
|
|
generation = GenerationSummarySerializer()
|
|
names = AbilityNameSerializer(many=True, read_only=True, source="typename")
|
|
damage_relations = serializers.SerializerMethodField('get_type_relationships')
|
|
|
|
class Meta:
|
|
model = Type
|
|
fields = ('id', 'name', 'move_damage_class', 'generation', 'names', 'damage_relations')
|
|
|
|
def get_type_relationships(self, obj):
|
|
|
|
relations = OrderedDict()
|
|
relations['no_damage_to'] = []
|
|
relations['half_damage_to'] = []
|
|
relations['double_damage_to'] = []
|
|
|
|
relations['no_damage_from'] = []
|
|
relations['half_damage_from'] = []
|
|
relations['double_damage_from'] = []
|
|
|
|
# Damage To
|
|
results = TypeEfficacy.objects.filter(damage_type_id=obj)
|
|
serializer = TypeEfficacySerializer(results, many=True, context=self.context)
|
|
|
|
for relation in serializer.data:
|
|
type = Type.objects.get(id=relation['target_type_id'])
|
|
if relation['damage_factor'] == 200:
|
|
relations['double_damage_to'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
elif relation['damage_factor'] == 50:
|
|
relations['half_damage_to'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
elif relation['damage_factor'] == 0:
|
|
relations['no_damage_to'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
|
|
# Damage From
|
|
results = TypeEfficacy.objects.filter(target_type_id=obj)
|
|
serializer = TypeEfficacySerializer(results, many=True, context=self.context)
|
|
|
|
for relation in serializer.data:
|
|
type = Type.objects.get(id=relation['damage_type_id'])
|
|
if relation['damage_factor'] == 200:
|
|
relations['double_damage_from'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
elif relation['damage_factor'] == 50:
|
|
relations['half_damage_from'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
elif relation['damage_factor'] == 0:
|
|
relations['no_damage_from'].append(TypeSummarySerializer(type, context=self.context).data)
|
|
|
|
return relations
|
|
|
|
|
|
|
|
#########################
|
|
# BERRY SERIALIZERS #
|
|
#########################
|
|
|
|
class BerrySerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Berry resource
|
|
"""
|
|
class Meta:
|
|
model = Berry
|
|
|
|
|
|
class CharacteristicSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Characteristic resource
|
|
"""
|
|
class Meta:
|
|
model = Characteristic
|
|
|
|
|
|
class EggGroupSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the EggGroup resource
|
|
"""
|
|
class Meta:
|
|
model = EggGroup
|
|
|
|
|
|
class EncounterSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Encounter resource
|
|
"""
|
|
class Meta:
|
|
model = Encounter
|
|
|
|
|
|
class GenderSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Gender resource
|
|
"""
|
|
class Meta:
|
|
model = Gender
|
|
|
|
|
|
class GrowthRateSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the GrowthRate resource
|
|
"""
|
|
class Meta:
|
|
model = GrowthRate
|
|
|
|
|
|
class ItemSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Item resource
|
|
"""
|
|
class Meta:
|
|
model = Item
|
|
|
|
|
|
class LocationSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Location resource
|
|
"""
|
|
class Meta:
|
|
model = Location
|
|
|
|
|
|
class MoveSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Move resource
|
|
"""
|
|
class Meta:
|
|
model = Move
|
|
|
|
|
|
class NatureSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Nature resource
|
|
"""
|
|
class Meta:
|
|
model = Nature
|
|
|
|
|
|
class PokedexSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Pokedex resource
|
|
"""
|
|
class Meta:
|
|
model = Pokedex
|
|
|
|
|
|
class PokemonSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for the Pokemon resource
|
|
"""
|
|
class Meta:
|
|
model = Pokemon
|