mirror of
https://github.com/PokeAPI/pokeapi
synced 2024-11-25 04:40:21 +00:00
Getting started with the APIs. A few serving lists and details by name or pk
This commit is contained in:
parent
744193dcab
commit
8eff28a872
5 changed files with 206 additions and 71 deletions
|
@ -145,11 +145,11 @@ CORS_ALLOW_METHODS = (
|
|||
REST_FRAMEWORK = {
|
||||
|
||||
'DEFAULT_RENDERER_CLASSES': (
|
||||
'rest_framework.renderers.JSONRenderer'
|
||||
'rest_framework.renderers.JSONRenderer',
|
||||
),
|
||||
|
||||
'DEFAULT_PARSER_CLASSES': (
|
||||
'rest_framework.parsers.JSONParser'
|
||||
'rest_framework.parsers.JSONParser',
|
||||
),
|
||||
|
||||
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
|
||||
|
|
|
@ -9,28 +9,21 @@ from django.views.generic import TemplateView
|
|||
from django.contrib import admin
|
||||
admin.autodiscover()
|
||||
|
||||
|
||||
##################################
|
||||
#
|
||||
# V1 API setup using Tastypie
|
||||
#
|
||||
##################################
|
||||
|
||||
from tastypie.api import Api
|
||||
|
||||
from pokemon.api import (
|
||||
PokemonResource, TypeResource, AbilityResource, GameResource,
|
||||
SpriteResource, DescriptionResource, EggResource, MoveResource,
|
||||
PokedexResource
|
||||
)
|
||||
|
||||
from rest_framework import routers
|
||||
|
||||
from pokemon_v2 import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
|
||||
router.register(r"sprites", views.SpriteResource)
|
||||
|
||||
from tastypie.api import Api
|
||||
|
||||
api_resources = Api()
|
||||
api_resources.register(PokemonResource())
|
||||
api_resources.register(AbilityResource())
|
||||
|
@ -50,14 +43,16 @@ api_resources.register(PokedexResource())
|
|||
#####################################
|
||||
|
||||
from rest_framework import routers
|
||||
from pokemon_v2.views import (
|
||||
AbilityResource, SpriteResource
|
||||
)
|
||||
from pokemon_v2.views import *
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
|
||||
router.register(r"ability", AbilityResource)
|
||||
router.register(r"sprite", SpriteResource)
|
||||
router.register(r"generation", GenerationResource)
|
||||
router.register(r"move", MoveResource)
|
||||
router.register(r"nature", NatureResource)
|
||||
router.register(r"pokemon", PokemonResource)
|
||||
router.register(r"type", TypeResource)
|
||||
|
||||
|
||||
###########################
|
||||
|
|
|
@ -58,7 +58,7 @@ for index, info in enumerate(data):
|
|||
iso639 = info[1],
|
||||
iso3166 = info[2],
|
||||
name = info[3],
|
||||
official = bool(info[4]),
|
||||
official = bool(int(info[4])),
|
||||
order = info[5],
|
||||
)
|
||||
|
||||
|
@ -252,7 +252,7 @@ for index, info in enumerate(data):
|
|||
id = int(info[0]),
|
||||
move_damage_class = MoveDamageClass.objects.get(pk = int(info[1])) if info[1] != '' else None,
|
||||
name = info[2],
|
||||
is_battle_only = bool(info[3]),
|
||||
is_battle_only = bool(int(info[3])),
|
||||
game_index = int(info[4]) if info[4] else 0,
|
||||
)
|
||||
stat.save()
|
||||
|
@ -315,7 +315,7 @@ for index, info in enumerate(data):
|
|||
id = int(info[0]),
|
||||
name = info[1],
|
||||
generation = Generation.objects.get(pk = int(info[2])),
|
||||
is_main_series = bool(info[3])
|
||||
is_main_series = bool(int(info[3]))
|
||||
)
|
||||
ability.save()
|
||||
|
||||
|
@ -1451,7 +1451,7 @@ for index, info in enumerate(data):
|
|||
id = int(info[0]),
|
||||
region = Region.objects.get(pk = int(info[1])) if info[1] != '' else None,
|
||||
name = info[2],
|
||||
is_main_series = bool(info[3])
|
||||
is_main_series = bool(int(info[3]))
|
||||
)
|
||||
model.save()
|
||||
|
||||
|
@ -1575,11 +1575,11 @@ for index, info in enumerate(data):
|
|||
gender_rate = int(info[8]),
|
||||
capture_rate = int(info[9]),
|
||||
base_happiness = int(info[10]),
|
||||
is_baby = bool(info[11]),
|
||||
is_baby = bool(int(info[11])),
|
||||
hatch_counter = int(info[12]),
|
||||
has_gender_differences = bool(info[13]),
|
||||
has_gender_differences = bool(int(info[13])),
|
||||
growth_rate = GrowthRate.objects.get(pk = int(info[14])),
|
||||
forms_switchable = bool(info[15]),
|
||||
forms_switchable = bool(int(info[15])),
|
||||
order = int(info[16])
|
||||
)
|
||||
model.save()
|
||||
|
@ -1655,7 +1655,7 @@ for index, info in enumerate(data):
|
|||
weight = int(info[4]),
|
||||
base_experience = int(info[5]),
|
||||
order = int(info[6]),
|
||||
is_default = bool(info[7])
|
||||
is_default = bool(int(info[7]))
|
||||
)
|
||||
model.save()
|
||||
|
||||
|
@ -1669,7 +1669,7 @@ for index, info in enumerate(data):
|
|||
model = PokemonAbility (
|
||||
pokemon = Pokemon.objects.get(pk = int(info[0])),
|
||||
ability = Ability.objects.get(pk = int(info[1])),
|
||||
is_hidden = bool(info[2]),
|
||||
is_hidden = bool(int(info[2])),
|
||||
slot = int(info[3])
|
||||
)
|
||||
model.save()
|
||||
|
@ -1727,8 +1727,8 @@ for index, info in enumerate(data):
|
|||
party_species = PokemonSpecies.objects.get(pk = int(info[15])) if info[15] != '' else None,
|
||||
party_type = Type.objects.get(pk = int(info[16])) if info[16] != '' else None,
|
||||
trade_species = PokemonSpecies.objects.get(pk = int(info[17])) if info[17] != '' else None,
|
||||
needs_overworld_rain = bool(info[18]),
|
||||
turn_upside_down = bool(info[19])
|
||||
needs_overworld_rain = bool(int(info[18])),
|
||||
turn_upside_down = bool(int(info[19]))
|
||||
)
|
||||
model.save()
|
||||
|
||||
|
@ -1745,9 +1745,9 @@ for index, info in enumerate(data):
|
|||
form_identifier = info[2],
|
||||
pokemon = Pokemon.objects.get(pk = int(info[3])),
|
||||
introduced_in_version_group = VersionGroup.objects.get(pk = int(info[4])),
|
||||
is_default = bool(info[5]),
|
||||
is_battle_only = bool(info[6]),
|
||||
is_mega = bool(info[7]),
|
||||
is_default = bool(int(info[5])),
|
||||
is_battle_only = bool(int(info[6])),
|
||||
is_mega = bool(int(info[7])),
|
||||
form_order = int(info[8]),
|
||||
order = int(info[9])
|
||||
)
|
||||
|
@ -2109,7 +2109,7 @@ for index, info in enumerate(data):
|
|||
id = int(info[0]),
|
||||
encounter_condition = EncounterCondition.objects.get(pk = int(info[1])),
|
||||
name = info[2],
|
||||
is_default = bool(info[3])
|
||||
is_default = bool(int(info[3]))
|
||||
)
|
||||
model.save()
|
||||
|
||||
|
|
|
@ -6,58 +6,148 @@ from rest_framework import serializers
|
|||
PokeAPI v2 serializers
|
||||
"""
|
||||
|
||||
from pokemon.models import (
|
||||
Sprite
|
||||
)
|
||||
|
||||
from .models import (
|
||||
Ability,
|
||||
Move,
|
||||
Nature,
|
||||
Type
|
||||
)
|
||||
from .models import *
|
||||
|
||||
|
||||
class AbilitySerializer(serializers.HyperlinkedModelSerializer):
|
||||
class AbilitySerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Ability resource
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Ability
|
||||
|
||||
|
||||
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 GenerationSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Ability resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Generation
|
||||
|
||||
|
||||
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 LanguageSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Language resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Language
|
||||
|
||||
|
||||
class LocationSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Location resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Location
|
||||
|
||||
|
||||
class MoveSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class MoveSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Move resource
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Move
|
||||
|
||||
|
||||
class NatureSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class NatureSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Nature resource
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Nature
|
||||
|
||||
|
||||
class SpriteSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class PokedexSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Type resource
|
||||
Serializer for the Pokedex resource
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Sprite
|
||||
model = Pokedex
|
||||
|
||||
|
||||
class TypeSerializer(serializers.HyperlinkedModelSerializer):
|
||||
class PokemonSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Pokemon resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Pokemon
|
||||
|
||||
|
||||
class RegionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Region resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Region
|
||||
|
||||
|
||||
class TypeSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Type resource
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
model = Type
|
||||
|
||||
|
||||
class VersionSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Serializer for the Version resource
|
||||
"""
|
||||
class Meta:
|
||||
model = Version
|
|
@ -1,32 +1,82 @@
|
|||
|
||||
from __future__ import unicode_literals
|
||||
from rest_framework import viewsets
|
||||
from django.shortcuts import get_object_or_404
|
||||
from .models import *
|
||||
from .serializers import *
|
||||
import re
|
||||
|
||||
from pokemon.models import (
|
||||
|
||||
Sprite
|
||||
)
|
||||
|
||||
from .models import (
|
||||
Ability
|
||||
)
|
||||
|
||||
from .serializers import (
|
||||
AbilitySerializer, SpriteSerializer
|
||||
)
|
||||
|
||||
class SpriteResource(viewsets.ReadOnlyModelViewSet):
|
||||
class NameOrIdLookupMixin(viewsets.ReadOnlyModelViewSet):
|
||||
"""
|
||||
Views for the Sprite V2 Resource
|
||||
This will allow a resource to be looked up by name or id (pk in this case).
|
||||
"""
|
||||
idPattern = re.compile("^[0-9]+$")
|
||||
namePattern = re.compile("^[0-9A-Za-z\-]+$")
|
||||
|
||||
queryset = Sprite.objects.all()
|
||||
serializer_class = SpriteSerializer
|
||||
def get_object(self):
|
||||
|
||||
class AbilityResource(viewsets.ReadOnlyModelViewSet):
|
||||
queryset = self.get_queryset()
|
||||
queryset = self.filter_queryset(queryset)
|
||||
lookup = self.kwargs['pk']
|
||||
|
||||
if (self.idPattern.match(lookup)):
|
||||
resp = get_object_or_404(queryset, pk=lookup)
|
||||
|
||||
elif (self.namePattern.match(lookup)):
|
||||
resp = get_object_or_404(queryset, name=lookup)
|
||||
|
||||
else:
|
||||
resp = get_object_or_404(queryset, pk="")
|
||||
|
||||
return resp
|
||||
|
||||
|
||||
class AbilityResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Ability V2 Resource
|
||||
"""
|
||||
|
||||
queryset = Ability.objects.all()
|
||||
serializer_class = AbilitySerializer
|
||||
|
||||
|
||||
class GenerationResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Generation V2 Resource
|
||||
"""
|
||||
queryset = Generation.objects.all()
|
||||
serializer_class = GenerationSerializer
|
||||
|
||||
|
||||
class MoveResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Move V2 Resource
|
||||
"""
|
||||
queryset = Move.objects.all()
|
||||
serializer_class = MoveSerializer
|
||||
|
||||
|
||||
class NatureResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Nature V2 Resource
|
||||
"""
|
||||
queryset = Nature.objects.all()
|
||||
serializer_class = NatureSerializer
|
||||
|
||||
|
||||
class PokemonResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Pokemon V2 Resource
|
||||
"""
|
||||
queryset = Pokemon.objects.all()
|
||||
serializer_class = PokemonSerializer
|
||||
|
||||
|
||||
class TypeResource(NameOrIdLookupMixin):
|
||||
"""
|
||||
Views for the Type V2 Resource
|
||||
"""
|
||||
queryset = Type.objects.all()
|
||||
serializer_class = TypeSerializer
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue