Commit 6564a989 authored by Renfred Harper's avatar Renfred Harper

Add rest framework API for models

parent 17be1cd9
from rest_framework import serializers
from website.models import Category, Facility, Schedule, OpenTime
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
class OpenTimeSerializer(serializers.ModelSerializer):
class Meta:
model = OpenTime
class ScheduleSerializer(serializers.ModelSerializer):
open_times = OpenTimeSerializer(many=True)
class Meta:
model = Schedule
class FacilitySerializer(serializers.ModelSerializer):
main_schedule = ScheduleSerializer()
special_schedules = ScheduleSerializer(many=True)
class Meta:
model = Facility
from django.conf.urls import patterns, url
from django.conf.urls import patterns, include, url
from website.views import *
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'categories', CategoryViewSet)
router.register(r'facilities', FacilityViewSet)
router.register(r'schedules', ScheduleViewSet)
router.register(r'opentimes', OpenTimeViewSet)
urlpatterns = patterns('website.views',
url(r'^(?:ajax|api)/schedule/', 'ajax_schedule_data', name='schedule_data'),
url(r'^api/', include(router.urls)),
url(r'^$', 'facility_grid', name='facility_grid'),
)
......@@ -4,9 +4,55 @@ from website.api import export_data
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.views.decorators.http import condition
from django.views.generic import ListView, DetailView
from website.models import Facility, OpenTime, Category, Schedule
from website.api import export_data
from website.serializers import CategorySerializer, FacilitySerializer, ScheduleSerializer, OpenTimeSerializer
from rest_framework import viewsets
from rest_framework.response import Response
import hashlib
import json
# Rest Framework Class Views
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
class FacilityViewSet(viewsets.ModelViewSet):
queryset = Facility.objects.all()
serializer_class = FacilitySerializer
class ScheduleViewSet(viewsets.ModelViewSet):
queryset = Schedule.objects.all()
serializer_class = ScheduleSerializer
class OpenTimeViewSet(viewsets.ModelViewSet):
queryset = OpenTime.objects.all()
serializer_class = OpenTimeSerializer
class FacilityListView(ListView):
queryset = Facility.objects.all()
serializer_class = FacilitySerializer
class FacilityCategoryListView(ListView):
model = Facility
def get_queryset(self):
return Facility.objects.filter(category=self.kwargs['category'])
class FacilityStatusListView(ListView):
model = Facility
def get_queryset(self):
return Facility.objects.all()
class FacilityDetailView(DetailView):
model = Facility
class OpenTimeDetailView(DetailView):
model = OpenTime
def facility_grid(request):
"""Display the facilities in a grid. Main page."""
......@@ -14,7 +60,7 @@ def facility_grid(request):
if request.GET['sort'] == 'location':
# Display the grid by location (instead of listing alphabetically)
pass # Not implemented yet
return render_to_response('facility_grid.html',
return render_to_response('facility_grid.html',
context_instance=RequestContext(request))
def gen_etag(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