annotate gigs/views.py @ 205:b4566292bbfe tip

Update t-shirt inventory
author Brian Neal <bgneal@gmail.com>
date Sun, 06 Nov 2022 17:42:25 -0600
parents e2868ad47a1e
children
rev   line source
bgneal@44 1 """
bgneal@44 2 Views for the gigs application.
bgneal@44 3
bgneal@44 4 """
bgneal@44 5 import collections
bgneal@44 6 import datetime
bgneal@44 7
bgneal@44 8 from django.db import connection
bgneal@44 9 from django.shortcuts import render
bgneal@44 10
bgneal@44 11 from gigs.models import Band, Gig
bgneal@44 12
bgneal@44 13
bgneal@44 14 def gigs(request):
bgneal@44 15 """
bgneal@44 16 This view function renders the main gigs view, showing upcoming and past
bgneal@44 17 gigs as well as some statistics.
bgneal@44 18
bgneal@44 19 """
bgneal@44 20 today = datetime.date.today()
bgneal@44 21 gigs = Gig.objects.select_related('venue', 'flyer', 'venue__city',
bgneal@44 22 'venue__city__state', 'venue__city__country')
bgneal@44 23 upcoming = []
bgneal@44 24 previous = []
bgneal@44 25
bgneal@44 26 # To avoid many, many database hits in the template, we get all the
bgneal@44 27 # bands out at once. We also get the many-to-many intermediate table
bgneal@44 28 # that Django generated for us so we can associate bands to gigs.
bgneal@44 29 # Since we don't know about this table we drop into raw SQL to get
bgneal@44 30 # the contents.
bgneal@44 31
bgneal@44 32 bands = dict((band.id, band) for band in Band.objects.all())
bgneal@44 33 cursor = connection.cursor()
bgneal@57 34 cursor.execute('SELECT * FROM gigs_gig_bands')
bgneal@44 35 gig_bands = collections.defaultdict(list)
bgneal@44 36 for row in cursor.fetchall():
bgneal@44 37 gig_bands[row[1]].append(bands[row[2]])
bgneal@44 38
bgneal@44 39 for gig in gigs:
bgneal@44 40 gig.bands_ = gig_bands[gig.id]
bgneal@44 41 if gig.date >= today:
bgneal@44 42 upcoming.append(gig)
bgneal@44 43 else:
bgneal@44 44 previous.append(gig)
bgneal@44 45
bgneal@44 46 upcoming.reverse()
bgneal@44 47
bgneal@44 48 stats = {}
bgneal@44 49 venues = set()
bgneal@44 50 cities = set()
bgneal@44 51 states = set()
bgneal@44 52 countries = set()
bgneal@44 53 for gig in previous:
bgneal@44 54 venues.add(gig.venue.id)
bgneal@44 55 cities.add(gig.venue.city.id)
bgneal@44 56 if gig.venue.city.state:
bgneal@44 57 states.add(gig.venue.city.state.id)
bgneal@44 58 if gig.venue.city.country:
bgneal@44 59 countries.add(gig.venue.city.country.id)
bgneal@44 60
bgneal@44 61 stats['count'] = len(previous)
bgneal@44 62 stats['venues'] = len(venues)
bgneal@44 63 stats['cities'] = len(cities)
bgneal@44 64 stats['states'] = len(states)
bgneal@44 65 stats['countries'] = len(countries)
bgneal@44 66 stats['bands'] = len(bands)
bgneal@44 67
bgneal@44 68 flyer_gigs = Gig.objects.exclude(flyer__isnull = True).select_related(
bgneal@44 69 'venue', 'flyer').order_by('-date')
bgneal@44 70
bgneal@44 71 return render(request, 'gigs/gigs.html', {
bgneal@44 72 'upcoming' : upcoming,
bgneal@44 73 'previous' : previous,
bgneal@44 74 'stats' : stats,
bgneal@44 75 'flyer_gigs' : flyer_gigs,
bgneal@44 76 })