views.py 2.59 KB
Newer Older
1
from django.template import RequestContext
Daniel W Bond's avatar
Daniel W Bond committed
2
from website.models import Facility, OpenTime
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
from django.views.generic import ListView, DetailView
Daniel W Bond's avatar
Daniel W Bond committed
8
from model_utils.models import TimeStampedModel
9
10
11
12
13

from website.models import Facility, OpenTime, Category, Schedule
from website.api import export_data
from website.serializers import  CategorySerializer, FacilitySerializer, ScheduleSerializer, OpenTimeSerializer

14
from rest_framework import viewsets, status
15
from rest_framework.response import Response
16
from rest_framework.decorators import api_view
17

18
19
import hashlib
import json
Daniel Bond's avatar
Daniel Bond committed
20

21
22
23
24
25
# Rest Framework Class Views
class CategoryViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer

26
class FacilityViewSet(viewsets.ReadOnlyModelViewSet):
27
28
29
    queryset = Facility.objects.all()
    serializer_class = FacilitySerializer

30
31
    def get_queryset(self):
        queryset = Facility.objects.all()
32
        open_now = self.request.query_params.get('open', None)
33
34
35
36
37
38
39
40
41
42
43
        if open_now is not None:
            results = []
            for fac in queryset:
                print fac
                if fac.isOpen():
                    print True
                    results.append(fac)
            return results
        else:
            return queryset

44
45
46
47
48
49
50
51
class ScheduleViewSet(viewsets.ModelViewSet):
    queryset = Schedule.objects.all()
    serializer_class = ScheduleSerializer

class OpenTimeViewSet(viewsets.ModelViewSet):
    queryset = OpenTime.objects.all()
    serializer_class = OpenTimeSerializer

52
53
54
55
56
57
58
59
#def facility_grid(request):
#    """Display the facilities in a grid. Main page."""
#    if 'sort' in request.GET:
#        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',
#            context_instance=RequestContext(request))
60
61
62
63
64

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

def gen_last_modified(request):
Daniel W Bond's avatar
Daniel W Bond committed
65
    return TimeStampedModel.objects.all().order_by('-last_modified')[0].last_modified
66
67


68
69
70
71
72
73
#@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
#    return HttpResponse(json.dumps({'data': export_data()}, indent=4),
#            content_type="application/json")