Commit 8565caae authored by Daniel W Bond's avatar Daniel W Bond
Browse files

minimal viable product for api, functional, though there's stuff I want to toy...

minimal viable product for api, functional, though there's stuff I want to toy with now that I know what's going on
parent 63e34f77
...@@ -10,16 +10,17 @@ from .views import BuildingList, BuildingRetrieve, FloorRetrieve, RoomRetrieve,\ ...@@ -10,16 +10,17 @@ from .views import BuildingList, BuildingRetrieve, FloorRetrieve, RoomRetrieve,\
# 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'),
url(r'^(?P<name>[\w-]+)/$', BuildingRetrieve.as_view(), name='api_detail_building'), url(r'^(?P<building_name>[\w-]+)/$', BuildingRetrieve.as_view(), name='api_detail_building'),
url(r'^building/(?P<pk>\d+)/$', FloorRetrieve.as_view(), name='api_detail_floor'), # the naming here and for floors is a little obnoxious
url(r'^(?P<building__building_name>[\w-]+)/(?P<floor_num>\d+)/$', FloorRetrieve.as_view(), name='api_detail_floor'),
# list all the floors still? # list all the floors still?
url(r'^building/floor/(?P<pk>\d+)/$', RoomRetrieve.as_view(), name='api_deatil_room'), url(r'^(?P<floor__building__building_name>[\w-]+)/(?P<floor__floor_num>\d+)/(?P<room_num>\d+)/$', RoomRetrieve.as_view(), name='api_detail_room'),
# list all the rooms still? # list all the rooms still?
) )
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<pk>\d+)/$', MajorRetrieve.as_view(), name='api_detail_building'), url(r'^(?P<slug>[\w-]+)/$', MajorRetrieve.as_view(), name='api_detail_building'),
) )
urlpatterns = patterns('', urlpatterns = patterns('',
......
# standard library imports # standard library imports
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
# core django imports
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
...@@ -17,6 +19,18 @@ class HousingPagination(PageNumberPagination): ...@@ -17,6 +19,18 @@ class HousingPagination(PageNumberPagination):
max_page_size = 1000 max_page_size = 1000
class MultipleFieldLookupMixin(object):
# http://www.django-rest-framework.org/api-guide/generic-views/#get_objectself
def get_object(self):
queryset = self.get_queryset()
filter = {}
for field in self.multiple_lookup_fields:
filter[field] = self.kwargs[field]
obj = get_object_or_404(queryset, **filter)
return obj
# the actual api views # the actual api views
# housing apis # housing apis
...@@ -29,7 +43,7 @@ class BuildingRetrieve(RetrieveAPIView): ...@@ -29,7 +43,7 @@ class BuildingRetrieve(RetrieveAPIView):
model = Building model = Building
queryset = Building.objects.all() queryset = Building.objects.all()
serializer_class = BuildingSerializer serializer_class = BuildingSerializer
lookup_field = 'name' lookup_field = 'building_name'
#class FloorList(ListAPIView): #class FloorList(ListAPIView):
...@@ -38,16 +52,19 @@ class BuildingRetrieve(RetrieveAPIView): ...@@ -38,16 +52,19 @@ class BuildingRetrieve(RetrieveAPIView):
# pagination_class = HousingPagination # pagination_class = HousingPagination
class FloorRetrieve(RetrieveAPIView): class FloorRetrieve(MultipleFieldLookupMixin, RetrieveAPIView):
model = Floor model = Floor
queryset = Floor.objects.all() queryset = Floor.objects.all()
serializer_class = FloorSerializer serializer_class = FloorSerializer
multiple_lookup_fields = ('building__building_name', 'floor_num')
class RoomRetrieve(RetrieveAPIView): class RoomRetrieve(MultipleFieldLookupMixin, RetrieveAPIView):
model = Room model = Room
queryset = Room.objects.all() queryset = Room.objects.all()
serializer_class = RoomSerializer serializer_class = RoomSerializer
multiple_lookup_fields = ('room_num', 'floor__floor_num',
'floor__building__building_name')
#class RoomList(ListAPIView): # kek #class RoomList(ListAPIView): # kek
...@@ -66,3 +83,4 @@ class MajorRetrieve(RetrieveAPIView): ...@@ -66,3 +83,4 @@ class MajorRetrieve(RetrieveAPIView):
model = Major model = Major
queryset = Major.objects.all() queryset = Major.objects.all()
serializer_class = MajorSerializer serializer_class = MajorSerializer
lookup_field = 'slug'
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