Commit fc5ec423 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

added api versioning and api root

parent 8565caae
...@@ -2,11 +2,14 @@ ...@@ -2,11 +2,14 @@
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
# core django imports # core django imports
from django.conf.urls import patterns, include, url from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
# imports from your apps # imports from your apps
from .views import BuildingList, BuildingRetrieve, FloorRetrieve, RoomRetrieve,\ from .views import BuildingList, BuildingRetrieve, FloorRetrieve, RoomRetrieve,\
MajorList, MajorRetrieve MajorList, MajorRetrieve, APIRoot
# custom routing ftw # custom routing ftw
# API v1
# separate out major and building patterns # separate out major and building patterns
building_urls = patterns('', building_urls = patterns('',
url(r'^$', BuildingList.as_view(), name='api_list_buildings'), url(r'^$', BuildingList.as_view(), name='api_list_buildings'),
...@@ -20,10 +23,14 @@ building_urls = patterns('', ...@@ -20,10 +23,14 @@ building_urls = patterns('',
major_urls = patterns('', major_urls = patterns('',
url(r'^$', MajorList.as_view(), name='api_list_majors'), url(r'^$', MajorList.as_view(), name='api_list_majors'),
url(r'^(?P<slug>[\w-]+)/$', MajorRetrieve.as_view(), name='api_detail_building'), url(r'^(?P<slug>[\w-]+)/$', MajorRetrieve.as_view(), name='api_detail_major'),
) )
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^housing/', include(building_urls)), url(r'^v1/housing/', include(building_urls)),
url(r'^majors/', include(major_urls)), url(r'^v1/majors/', include(major_urls)),
url(r'^v1/$', APIRoot.as_view(), name='api_root'),
url(r'^$', RedirectView.as_view(pattern_name='api_root')),
) )
# Subsequent API versions below
...@@ -5,6 +5,9 @@ from django.shortcuts import get_object_or_404 ...@@ -5,6 +5,9 @@ from django.shortcuts import get_object_or_404
# third party imports # third party imports
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from rest_framework.generics import ListAPIView, RetrieveAPIView from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.views import APIView
from rest_framework.reverse import reverse
from rest_framework.response import Response
# imports from your apps # imports from your apps
from housing.models import Building, Floor, Room from housing.models import Building, Floor, Room
from accounts.models import Major from accounts.models import Major
...@@ -84,3 +87,12 @@ class MajorRetrieve(RetrieveAPIView): ...@@ -84,3 +87,12 @@ class MajorRetrieve(RetrieveAPIView):
queryset = Major.objects.all() queryset = Major.objects.all()
serializer_class = MajorSerializer serializer_class = MajorSerializer
lookup_field = 'slug' lookup_field = 'slug'
# root urls
class APIRoot(APIView):
def get(self, request):
return Response({
'housing': reverse('api_list_buildings', request=request),
'majors': reverse('api_list_majors', request=request),
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment