Add rudimentary API functionality.

This commit is contained in:
Josh Washburne 2018-03-28 16:12:55 -04:00
parent 0fb7f148bb
commit abe64f3a7d
14 changed files with 93 additions and 1 deletions

View file

@ -1,6 +1,7 @@
dj-database-url>=0.4.2
Django>=2.0
django-authtools>=1.6.0
djangorestframework>=3.7.7
psycopg2>=2.7.3.2
python-decouple>=3.1
pytz>=2017.3

View file

View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'

View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View file

@ -0,0 +1,36 @@
from rest_framework import serializers
from radio.models import Album, Artist, Game, Song
class AlbumSerializer(serializers.ModelSerializer):
class Meta:
model = Album
fields = ('id', 'title')
class ArtistSerializer(serializers.ModelSerializer):
class Meta:
model = Artist
fields = ('id', 'alias', 'first_name', 'last_name')
class ArtistFullnameSerializer(serializers.ModelSerializer):
class Meta:
model = Artist
fields = ('id', 'full_name')
class GameSerializer(serializers.ModelSerializer):
class Meta:
model = Game
fields = ('id', 'title')
class SongSerializer(serializers.ModelSerializer):
artists = ArtistFullnameSerializer(many=True)
class Meta:
model = Song
fields = ('id', 'album', 'artists', 'published_date', 'game',
'num_played', 'last_played', 'length', 'song_type', 'title')

View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View file

@ -0,0 +1,13 @@
from rest_framework.routers import DefaultRouter
from api.views.radio import (AlbumViewSet, ArtistViewSet,
GameViewSet, SongViewSet)
router = DefaultRouter()
router.register(r'albums', AlbumViewSet, base_name='album')
router.register(r'artists', ArtistViewSet, base_name='artist')
router.register(r'games', GameViewSet, base_name='game')
router.register(r'songs', SongViewSet, base_name='song')
urlpatterns = router.urls

View file

View file

@ -0,0 +1,25 @@
from rest_framework import viewsets
from radio.models import Album, Artist, Game, Song
from ..serializers.radio import (AlbumSerializer, ArtistSerializer,
GameSerializer, SongSerializer)
class AlbumViewSet(viewsets.ModelViewSet):
queryset = Album.objects.all()
serializer_class = AlbumSerializer
class ArtistViewSet(viewsets.ModelViewSet):
queryset = Artist.objects.all()
serializer_class = ArtistSerializer
class GameViewSet(viewsets.ModelViewSet):
queryset = Game.objects.all()
serializer_class = GameSerializer
class SongViewSet(viewsets.ModelViewSet):
queryset = Song.objects.all()
serializer_class = SongSerializer

View file

@ -38,10 +38,12 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'authtools',
'rest_framework',
'core.apps.CoreConfig',
'radio.apps.RadioConfig',
'profiles.apps.ProfilesConfig'
'profiles.apps.ProfilesConfig',
'api.apps.ApiConfig',
]
LANGUAGE_CODE = 'en-us'

View file

@ -4,4 +4,5 @@ from django.urls import include, path
urlpatterns = [
path('accounts/', include('authtools.urls')),
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
]