little cleanup

This commit is contained in:
Adickes 2015-09-10 19:02:06 -04:00
parent ff63705a4d
commit a0c0d62699
4 changed files with 45 additions and 8 deletions

View file

@ -49,6 +49,7 @@ router = routers.DefaultRouter()
router.register(r"ability", AbilityResource)
router.register(r"generation", GenerationResource)
router.register(r"language", LanguageResource)
router.register(r"move", MoveResource)
router.register(r"nature", NatureResource)
router.register(r"pokemon", PokemonResource)

View file

@ -152,7 +152,7 @@ class HasItemPocket(models.Model):
class HasLanguage(models.Model):
language = models.ForeignKey('Language', blank = True, null = True, related_name="%(class)slanguage")
language = models.ForeignKey('Language', blank = True, null = True, related_name="%(class)s_language")
class Meta:
abstract = True

View file

@ -9,6 +9,12 @@ PokeAPI v2 serializers
from .models import *
class LanguageListSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Language
class LanguageSerializer(serializers.ModelSerializer):
class Meta:
@ -28,7 +34,7 @@ class AbilityDescriptionSerializer(serializers.ModelSerializer):
class AbilityFlavorTextSerializer(serializers.ModelSerializer):
text = serializers.CharField(source="flavor_text")
# language = LanguageSerializer(source="abilityflavortextlanguage")
language = LanguageListSerializer()
class Meta:
model = AbilityFlavorText
@ -42,13 +48,13 @@ class AbilityNameSerializer(serializers.ModelSerializer):
fields = ('name', 'language')
class AbilityListSerializer(serializers.ModelSerializer):
class AbilityListSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Ability
class AbilitySerializer(serializers.ModelSerializer):
class AbilitySerializer(serializers.HyperlinkedModelSerializer):
descriptions = AbilityDescriptionSerializer(many=True, read_only=True, source="abilitydescription")
flavor_text_entries = AbilityFlavorTextSerializer(many=True, read_only=True, source="abilityflavortext")

View file

@ -7,12 +7,16 @@ from .serializers import *
import re
class PokeapiCommonViewset(viewsets.ReadOnlyModelViewSet):
###########################
# BEHAVOIR ABSTRACTIONS #
###########################
class ListOrDetailSerialRelation():
"""
This will allow a resource to be looked up by name or id (pk in this case).
Mixin to allow association with separate serializers
for list or detail view.
"""
idPattern = re.compile("^[0-9]+$")
namePattern = re.compile("^[0-9A-Za-z\-]+$")
list_serializer_class = None
def get_serializer_class(self):
@ -20,6 +24,16 @@ class PokeapiCommonViewset(viewsets.ReadOnlyModelViewSet):
return self.list_serializer_class
return self.serializer_class
class NameOrIdRetrieval():
"""
Mixin to allow retrieval of resources by
pk (in this case ID) or by name
"""
idPattern = re.compile("^[0-9]+$")
namePattern = re.compile("^[0-9A-Za-z\-]+$")
def get_object(self):
queryset = self.get_queryset()
queryset = self.filter_queryset(queryset)
@ -37,6 +51,14 @@ class PokeapiCommonViewset(viewsets.ReadOnlyModelViewSet):
return resp
class PokeapiCommonViewset(ListOrDetailSerialRelation, NameOrIdRetrieval, viewsets.ReadOnlyModelViewSet):
pass
##########
# APIS #
##########
class AbilityResource(PokeapiCommonViewset):
"""
Views for the Ability V2 Resource
@ -54,6 +76,14 @@ class GenerationResource(PokeapiCommonViewset):
serializer_class = GenerationSerializer
class LanguageResource(PokeapiCommonViewset):
"""
Views for the Generation V2 Resource
"""
queryset = Language.objects.all()
serializer_class = LanguageSerializer
class MoveResource(PokeapiCommonViewset):
"""
Views for the Move V2 Resource