views.py 1.64 KB
Newer Older
1
from django.template import RequestContext
2
3
from website.models import Restaurant, OpenTime, BaseModel
from website.json 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
import re
8
9
import hashlib
import json
Daniel Bond's avatar
Daniel Bond committed
10

11

12
13
14
15
16
17
def restaurant_grid(request):
    """Display the restaurants 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
Daniel Bond's avatar
Daniel Bond committed
18
    restaurants = Restaurant.objects.all()
19
20
21
22
    # Sort the restaurants by alphabetical order ignoring "the" and "a"
    restaurants = sorted(restaurants,
            key=lambda r: re.sub('^(the|a) ', '', r.name, count=1,
            flags=re.IGNORECASE))
23
24
    # Restaurants in lists of 4 to easily create rows in template
    restRows = [restaurants[x:x + 4] for x in xrange(0, len(restaurants), 4)]
25
    return render_to_response('restaurant_grid.html', {'restRows': restRows,
26
27
            'restaurants': restaurants},
            context_instance=RequestContext(request))
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

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
    return HttpResponse(json.dumps({'data': export_data()}),
            content_type="application/json")