comparison mysite/band/views.py @ 28:a404f93a68d3

Decrease by 2 orders of magnitude the number of queries needed to generate the gigs page for the Madeira.
author Brian Neal <bgneal@gmail.com>
date Thu, 31 Mar 2011 00:14:34 +0000
parents dac690ab98b2
children f61db5f82549
comparison
equal deleted inserted replaced
27:c185fcf5817f 28:a404f93a68d3
1 ####################################################################### 1 #######################################################################
2 # 2 #
3 # PyBand Copyright (C) 2008 by Brian Neal 3 # PyBand Copyright (C) 2008 - 2011 by Brian Neal
4 # 4 #
5 ####################################################################### 5 #######################################################################
6 import collections
7 import datetime
8 import random
9
6 from django import forms 10 from django import forms
7 from django.core.urlresolvers import reverse 11 from django.core.urlresolvers import reverse
8 from django.http import HttpResponse 12 from django.http import HttpResponse
9 from django.http import HttpResponseRedirect 13 from django.http import HttpResponseRedirect
10 from django.shortcuts import render_to_response 14 from django.shortcuts import render_to_response
11 from django.shortcuts import get_object_or_404 15 from django.shortcuts import get_object_or_404
12 from django.template import RequestContext 16 from django.template import RequestContext
13 from django.template.loader import render_to_string 17 from django.template.loader import render_to_string
14 from django.core.mail import send_mail 18 from django.core.mail import send_mail
15 from django.db.models import Q 19 from django.db import connection
20
16 from mysite.band.models import Article 21 from mysite.band.models import Article
17 from mysite.band.models import Album 22 from mysite.band.models import Album
23 from mysite.band.models import Band
18 from mysite.band.models import Fan 24 from mysite.band.models import Fan
19 from mysite.band.models import Gear 25 from mysite.band.models import Gear
20 from mysite.band.models import Gig 26 from mysite.band.models import Gig
21 from mysite.band.models import Member 27 from mysite.band.models import Member
22 from mysite.band.models import Merchandise 28 from mysite.band.models import Merchandise
25 from mysite.band.models import News 31 from mysite.band.models import News
26 from mysite.band.models import SiteConfig 32 from mysite.band.models import SiteConfig
27 from mysite.band.models import Video_Set 33 from mysite.band.models import Video_Set
28 from mysite.photologue.models import Gallery 34 from mysite.photologue.models import Gallery
29 from mysite.photologue.models import Photo 35 from mysite.photologue.models import Photo
30 import datetime
31 import random
32 36
33 ####################################################################### 37 #######################################################################
34 38
35 def index(request): 39 def index(request):
36 config = SiteConfig.objects.get(pk = 1) 40 config = SiteConfig.objects.get(pk = 1)
37 carpe = Photo.objects.get(title_slug = 'carpe-noctem') 41 carpe = Photo.objects.get(title_slug = 'carpe-noctem')
38 sandstorm = Photo.objects.get(title_slug = 'sandstorm-cover') 42 sandstorm = Photo.objects.get(title_slug = 'sandstorm-cover')
39 ruins = Photo.objects.get(title_slug = 'ruins-cover') 43 ruins = Photo.objects.get(title_slug = 'ruins-cover')
40 44
41 upcomingDates = Gig.objects.filter(date__gte = datetime.date.today).order_by('date')[:5] 45 upcomingDates = Gig.objects.filter(date__gte = datetime.date.today).order_by('date')[:5]
42
43 #tourPhotos = Photo.objects.filter(
44 # Q(slug = 'flyer-aug-1-2008-san-diego') |
45 # Q(slug = 'flyer-aug-2-2008-hermosa-beach')
46 # ).order_by('id')
47 46
48 return render_to_response('band/index.html', 47 return render_to_response('band/index.html',
49 { 48 {
50 'config' : config, 49 'config' : config,
51 'carpe' : carpe, 50 'carpe' : carpe,
66 context_instance = RequestContext(request)) 65 context_instance = RequestContext(request))
67 66
68 ####################################################################### 67 #######################################################################
69 68
70 def gigs(request): 69 def gigs(request):
71 upcoming = Gig.objects.select_related().filter(date__gte = datetime.date.today).order_by('date') 70 today = datetime.date.today()
72 previous = Gig.objects.select_related().filter(date__lt = datetime.date.today) 71 gigs = Gig.objects.select_related('venue', 'flyer', 'venue__city',
73 #upcoming = Gig.objects.filter(date__gte = datetime.date.today).order_by('date') 72 'venue__city__state', 'venue__city__country')
74 #previous = Gig.objects.filter(date__lt = datetime.date.today) 73 upcoming = []
74 previous = []
75
76 # To avoid many, many database hits in the template, we get all the
77 # bands out at once. We also get the many-to-many intermediate table
78 # that Django generated for us so we can associate bands to gigs.
79 # Since we don't know about this table we drop into raw SQL to get
80 # the contents.
81
82 bands = dict((band.id, band) for band in Band.objects.all())
83 cursor = connection.cursor()
84 cursor.execute('SELECT * FROM band_gig_bands')
85 gig_bands = collections.defaultdict(list)
86 for row in cursor.fetchall():
87 gig_bands[row[1]].append(bands[row[2]])
88
89 for gig in gigs:
90 gig.bands_ = gig_bands[gig.id]
91 if gig.date >= today:
92 upcoming.append(gig)
93 else:
94 previous.append(gig)
95
96 upcoming.reverse()
75 97
76 stats = {} 98 stats = {}
77 venues = set([]) 99 venues = set()
78 cities = set([]) 100 cities = set()
79 states = set([]) 101 states = set()
80 countries = set([]) 102 countries = set()
81 bands = set([])
82 for gig in previous: 103 for gig in previous:
83 if gig.venue.id not in venues: 104 venues.add(gig.venue.id)
84 venues.add(gig.venue.id) 105 cities.add(gig.venue.city.id)
85 if gig.venue.city.id not in cities: 106 if gig.venue.city.state:
86 cities.add(gig.venue.city.id)
87 if gig.venue.city.state and gig.venue.city.state.id not in states:
88 states.add(gig.venue.city.state.id) 107 states.add(gig.venue.city.state.id)
89 if gig.venue.city.country and gig.venue.city.country.id not in countries: 108 if gig.venue.city.country:
90 countries.add(gig.venue.city.country.id) 109 countries.add(gig.venue.city.country.id)
91 for band in gig.bands.all(): 110
92 if band.id not in bands: 111 stats['count'] = len(previous)
93 bands.add(band.id)
94
95 stats['count'] = previous.count()
96 stats['venues'] = len(venues) 112 stats['venues'] = len(venues)
97 stats['cities'] = len(cities) 113 stats['cities'] = len(cities)
98 stats['states'] = len(states) 114 stats['states'] = len(states)
99 stats['countries'] = len(countries) 115 stats['countries'] = len(countries)
100 stats['bands'] = len(bands) 116 stats['bands'] = len(bands)
101 117
102 flyerGigs = Gig.objects.exclude(flyer__isnull = True).order_by('-date') 118 flyerGigs = Gig.objects.exclude(flyer__isnull = True).select_related(
103 119 'venue', 'flyer').order_by('-date')
104 return render_to_response('band/gigs.html', 120
105 { 121 return render_to_response('band/gigs.html', {
106 'upcoming' : upcoming, 122 'upcoming' : upcoming,
107 'previous' : previous, 123 'previous' : previous,
108 'stats' : stats, 124 'stats' : stats,
109 'flyerGigs' : flyerGigs, 125 'flyerGigs' : flyerGigs,
110 }, 126 },
111 context_instance = RequestContext(request)) 127 context_instance = RequestContext(request))
112 128
113 ####################################################################### 129 #######################################################################
114 130
115 def news(request): 131 def news(request):
116 news = News.objects.order_by('-date') 132 news = News.objects.order_by('-date')