views.py 895 Bytes
Newer Older
1 2
from website.models import Restaurant
from django.shortcuts import render_to_response
3
import re
Daniel Bond's avatar
Daniel Bond committed
4

5

6 7 8 9 10 11
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
12
    restaurants = Restaurant.objects.all()
13 14 15 16
    # 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))
17 18
    # Restaurants in lists of 4 to easily create rows in template
    restRows = [restaurants[x:x + 4] for x in xrange(0, len(restaurants), 4)]
19
    return render_to_response('restaurant_grid.html', {'restRows': restRows,
20
            'restaurants': restaurants})