From 414392a9cf866f4f7892b03917cbed4a1df9d9b5 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 28 May 2015 00:33:19 +0200 Subject: [PATCH] Start implementing new Duck API --- api/serializers.py | 10 ++++++++++ api/urls.py | 12 +++++++++++- api/viewsets.py | 11 +++++++++++ duckbook/settings.py | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 api/serializers.py create mode 100644 api/viewsets.py diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 0000000..01b5dec --- /dev/null +++ b/api/serializers.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +from rest_framework import serializers + +from booking.models import Duck + +class DuckSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Duck + fields = ('url', 'name', 'color',) + view_name = 'api:duck-detail' diff --git a/api/urls.py b/api/urls.py index b626ee7..e11db75 100644 --- a/api/urls.py +++ b/api/urls.py @@ -1,9 +1,19 @@ -from django.conf.urls import patterns, url +from django.conf.urls import patterns, url, include + +from rest_framework import routers from . import views +from . import viewsets + +rest_router = routers.DefaultRouter() +rest_router.register(r'ducks', viewsets.DuckViewSet) urlpatterns = patterns( '', + url( + r'^', + include(rest_router.urls) + ), url( r'^duck/book/$', views.duck_book, diff --git a/api/viewsets.py b/api/viewsets.py new file mode 100644 index 0000000..094eee5 --- /dev/null +++ b/api/viewsets.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +from django.contrib.auth.models import User + +from rest_framework import viewsets, permissions + +from .serializers import DuckSerializer +from booking.models import Duck + +class DuckViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Duck.objects.all() + serializer_class = DuckSerializer diff --git a/duckbook/settings.py b/duckbook/settings.py index 8766ea5..abde5fe 100644 --- a/duckbook/settings.py +++ b/duckbook/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = ( 'django.contrib.messages', 'django.contrib.staticfiles', 'django_js_reverse', + 'rest_framework', 'accounts', 'booking', 'api',