views.py 2.57 KB
Newer Older
1
from django.template import RequestContext
2
from website.models import Facility, OpenTime, BaseModel
3
from website.api import export_data
4
from django.shortcuts import render_to_response
5
6
from django.http import HttpResponse
from django.views.decorators.http import condition
7
8
9
10
11
12
13
14
15
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

16
17
import hashlib
import json
Daniel Bond's avatar
Daniel Bond committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

56

57
58
def facility_grid(request):
    """Display the facilities in a grid. Main page."""
59
60
61
62
    if 'sort' in request.GET:
        if request.GET['sort'] == 'location':
            # Display the grid by location (instead of listing alphabetically)
            pass  # Not implemented yet
63
    return render_to_response('facility_grid.html',
64
            context_instance=RequestContext(request))
65
66
67
68
69
70
71
72
73
74
75
76

def gen_etag(request):
    return hashlib.sha1(str(OpenTime.objects.all())).hexdigest()

def gen_last_modified(request):
    return BaseModel.objects.all().order_by('-last_modified')[0].last_modified


@condition(etag_func=gen_etag, last_modified_func=gen_last_modified)
def ajax_schedule_data(request):
    # Wrapping up in an object to avoid possible CSRF attack on top-level
    # arrays in JSON objects
Renfred Harper's avatar
Renfred Harper committed
77
    return HttpResponse(json.dumps({'data': export_data()}, indent=4),
78
79
            content_type="application/json")