annotate mysite/band/views.py @ 33:7d8015de651a

Removed old reference to /media in the admin/base_site.html template for the favicon.
author Brian Neal <bgneal@gmail.com>
date Wed, 02 Nov 2011 01:30:46 +0000
parents f61db5f82549
children 25e00d1b99bf
rev   line source
bgneal@1 1 #######################################################################
bgneal@1 2 #
bgneal@28 3 # PyBand Copyright (C) 2008 - 2011 by Brian Neal
bgneal@1 4 #
bgneal@1 5 #######################################################################
bgneal@28 6 import collections
bgneal@28 7 import datetime
bgneal@28 8 import random
bgneal@28 9
bgneal@1 10 from django import forms
bgneal@1 11 from django.core.urlresolvers import reverse
bgneal@1 12 from django.http import HttpResponse
bgneal@1 13 from django.http import HttpResponseRedirect
bgneal@1 14 from django.shortcuts import render_to_response
bgneal@1 15 from django.shortcuts import get_object_or_404
bgneal@1 16 from django.template import RequestContext
bgneal@1 17 from django.template.loader import render_to_string
bgneal@1 18 from django.core.mail import send_mail
bgneal@28 19 from django.db import connection
bgneal@28 20
bgneal@1 21 from mysite.band.models import Article
bgneal@1 22 from mysite.band.models import Album
bgneal@28 23 from mysite.band.models import Band
bgneal@1 24 from mysite.band.models import Fan
bgneal@1 25 from mysite.band.models import Gear
bgneal@1 26 from mysite.band.models import Gig
bgneal@1 27 from mysite.band.models import Member
bgneal@1 28 from mysite.band.models import Merchandise
bgneal@1 29 from mysite.band.models import Mp3
bgneal@1 30 from mysite.band.models import Mp3_Set
bgneal@1 31 from mysite.band.models import News
bgneal@1 32 from mysite.band.models import SiteConfig
bgneal@1 33 from mysite.band.models import Video_Set
bgneal@1 34 from mysite.photologue.models import Gallery
bgneal@1 35 from mysite.photologue.models import Photo
bgneal@1 36
bgneal@1 37 #######################################################################
bgneal@1 38
bgneal@1 39 def index(request):
bgneal@1 40 config = SiteConfig.objects.get(pk = 1)
bgneal@1 41 carpe = Photo.objects.get(title_slug = 'carpe-noctem')
bgneal@1 42 sandstorm = Photo.objects.get(title_slug = 'sandstorm-cover')
bgneal@1 43 ruins = Photo.objects.get(title_slug = 'ruins-cover')
bgneal@1 44
bgneal@1 45 upcomingDates = Gig.objects.filter(date__gte = datetime.date.today).order_by('date')[:5]
bgneal@1 46
bgneal@1 47 return render_to_response('band/index.html',
bgneal@1 48 {
bgneal@1 49 'config' : config,
bgneal@1 50 'carpe' : carpe,
bgneal@1 51 'sandstorm' : sandstorm,
bgneal@1 52 'ruins' : ruins,
bgneal@1 53 'upcomingDates' : upcomingDates,
bgneal@1 54 # 'tourPhotos' : tourPhotos,
bgneal@1 55 },
bgneal@1 56 context_instance = RequestContext(request))
bgneal@1 57
bgneal@1 58 #######################################################################
bgneal@1 59
bgneal@1 60 def bio(request):
bgneal@1 61 members = Member.objects.exclude(is_active__exact = 0)
bgneal@1 62
bgneal@1 63 return render_to_response('band/bio.html',
bgneal@1 64 { 'members' : members, },
bgneal@1 65 context_instance = RequestContext(request))
bgneal@1 66
bgneal@1 67 #######################################################################
bgneal@1 68
bgneal@1 69 def gigs(request):
bgneal@28 70 today = datetime.date.today()
bgneal@28 71 gigs = Gig.objects.select_related('venue', 'flyer', 'venue__city',
bgneal@28 72 'venue__city__state', 'venue__city__country')
bgneal@28 73 upcoming = []
bgneal@28 74 previous = []
bgneal@28 75
bgneal@28 76 # To avoid many, many database hits in the template, we get all the
bgneal@28 77 # bands out at once. We also get the many-to-many intermediate table
bgneal@28 78 # that Django generated for us so we can associate bands to gigs.
bgneal@28 79 # Since we don't know about this table we drop into raw SQL to get
bgneal@28 80 # the contents.
bgneal@28 81
bgneal@28 82 bands = dict((band.id, band) for band in Band.objects.all())
bgneal@28 83 cursor = connection.cursor()
bgneal@28 84 cursor.execute('SELECT * FROM band_gig_bands')
bgneal@28 85 gig_bands = collections.defaultdict(list)
bgneal@28 86 for row in cursor.fetchall():
bgneal@28 87 gig_bands[row[1]].append(bands[row[2]])
bgneal@28 88
bgneal@28 89 for gig in gigs:
bgneal@28 90 gig.bands_ = gig_bands[gig.id]
bgneal@28 91 if gig.date >= today:
bgneal@28 92 upcoming.append(gig)
bgneal@28 93 else:
bgneal@28 94 previous.append(gig)
bgneal@28 95
bgneal@28 96 upcoming.reverse()
bgneal@1 97
bgneal@1 98 stats = {}
bgneal@28 99 venues = set()
bgneal@28 100 cities = set()
bgneal@28 101 states = set()
bgneal@28 102 countries = set()
bgneal@1 103 for gig in previous:
bgneal@28 104 venues.add(gig.venue.id)
bgneal@28 105 cities.add(gig.venue.city.id)
bgneal@28 106 if gig.venue.city.state:
bgneal@1 107 states.add(gig.venue.city.state.id)
bgneal@28 108 if gig.venue.city.country:
bgneal@28 109 countries.add(gig.venue.city.country.id)
bgneal@1 110
bgneal@28 111 stats['count'] = len(previous)
bgneal@1 112 stats['venues'] = len(venues)
bgneal@1 113 stats['cities'] = len(cities)
bgneal@1 114 stats['states'] = len(states)
bgneal@5 115 stats['countries'] = len(countries)
bgneal@1 116 stats['bands'] = len(bands)
bgneal@1 117
bgneal@28 118 flyerGigs = Gig.objects.exclude(flyer__isnull = True).select_related(
bgneal@28 119 'venue', 'flyer').order_by('-date')
bgneal@1 120
bgneal@28 121 return render_to_response('band/gigs.html', {
bgneal@28 122 'upcoming' : upcoming,
bgneal@28 123 'previous' : previous,
bgneal@28 124 'stats' : stats,
bgneal@28 125 'flyerGigs' : flyerGigs,
bgneal@28 126 },
bgneal@28 127 context_instance = RequestContext(request))
bgneal@1 128
bgneal@1 129 #######################################################################
bgneal@1 130
bgneal@1 131 def news(request):
bgneal@1 132 news = News.objects.order_by('-date')
bgneal@1 133
bgneal@1 134 return render_to_response('band/news.html',
bgneal@1 135 {
bgneal@1 136 'news' : news
bgneal@1 137 },
bgneal@1 138 context_instance = RequestContext(request))
bgneal@1 139
bgneal@1 140 #######################################################################
bgneal@1 141
bgneal@1 142 def press_index(request):
bgneal@1 143 articles = Article.objects.order_by('-date')
bgneal@1 144
bgneal@1 145 return render_to_response('band/press.html',
bgneal@1 146 {
bgneal@1 147 'articles' : articles
bgneal@1 148 },
bgneal@1 149 context_instance = RequestContext(request))
bgneal@1 150
bgneal@1 151 #######################################################################
bgneal@1 152
bgneal@1 153 def press_detail(request, id):
bgneal@1 154 article = get_object_or_404(Article, pk = id)
bgneal@1 155
bgneal@1 156 return render_to_response('band/press_detail.html',
bgneal@1 157 { 'article' : article },
bgneal@1 158 context_instance = RequestContext(request))
bgneal@1 159
bgneal@1 160 #######################################################################
bgneal@1 161
bgneal@1 162 def songs(request):
bgneal@1 163 mp3Sets = Mp3_Set.objects.order_by('-date', '-id')
bgneal@1 164
bgneal@1 165 return render_to_response('band/songs.html',
bgneal@1 166 { 'mp3Sets' : mp3Sets },
bgneal@1 167 context_instance = RequestContext(request))
bgneal@1 168
bgneal@1 169 #######################################################################
bgneal@1 170
bgneal@1 171 def photos_index(request):
bgneal@1 172 galleries = Gallery.objects.values('title', 'id').order_by('-id')
bgneal@1 173
bgneal@1 174 photos = Photo.objects.filter(is_public__exact = 1)
bgneal@1 175 randomPhotos = random.sample(photos, 4)
bgneal@1 176
bgneal@1 177 return render_to_response('band/photos.html',
bgneal@1 178 { 'galleries' : galleries, 'randomPhotos' : randomPhotos },
bgneal@1 179 context_instance = RequestContext(request))
bgneal@1 180
bgneal@1 181 #######################################################################
bgneal@1 182
bgneal@1 183 def photo_detail(request, id):
bgneal@1 184 gallery = get_object_or_404(Gallery, pk = id)
bgneal@29 185 photos = gallery.photos.order_by('id')
bgneal@1 186 return render_to_response('band/photo_detail.html',
bgneal@29 187 {'gallery' : gallery, 'photos': photos },
bgneal@29 188 context_instance = RequestContext(request))
bgneal@1 189
bgneal@1 190 #######################################################################
bgneal@1 191
bgneal@1 192 def videos_index(request):
bgneal@9 193 vidsets = Video_Set.objects.values('title', 'id').order_by('-date')
bgneal@1 194 return render_to_response('band/videos.html',
bgneal@1 195 { 'vidsets' : vidsets },
bgneal@1 196 context_instance = RequestContext(request))
bgneal@1 197
bgneal@1 198 #######################################################################
bgneal@1 199
bgneal@1 200 def video_detail(request, id):
bgneal@1 201 vidset = get_object_or_404(Video_Set, pk = id)
bgneal@1 202
bgneal@1 203 return render_to_response('band/video_detail.html',
bgneal@1 204 { 'vidset' : vidset },
bgneal@1 205 context_instance = RequestContext(request))
bgneal@1 206
bgneal@1 207 #######################################################################
bgneal@1 208
bgneal@1 209 def buy(request):
bgneal@1 210 albums = Album.objects.all().order_by('-id')
bgneal@1 211 merchandise = Merchandise.objects.all().order_by('-id')
bgneal@1 212 config = SiteConfig.objects.values('ordering_info').get(pk = 1)
bgneal@1 213 return render_to_response('band/buy.html',
bgneal@1 214 { 'albums' : albums, 'merchandise' : merchandise, 'config' : config },
bgneal@1 215 context_instance = RequestContext(request))
bgneal@1 216
bgneal@1 217 #######################################################################
bgneal@1 218
bgneal@1 219 def confirmEmail(config, to, subscribe, key):
bgneal@1 220 band = config.band_name
bgneal@1 221 fromEmail = config.contact_email
bgneal@1 222 url = config.url
bgneal@1 223 if url[-1] != '/':
bgneal@1 224 url += '/'
bgneal@1 225 url += 'mail/confirm/' + key
bgneal@1 226
bgneal@1 227 if subscribe:
bgneal@1 228 emailTemplate = 'band/email_subscribe.txt'
bgneal@1 229 else:
bgneal@1 230 emailTemplate = 'band/email_unsubscribe.txt'
bgneal@1 231
bgneal@1 232 msg = render_to_string(emailTemplate, { 'band' : band, 'url' : url, 'band_url' : config.url })
bgneal@1 233
bgneal@1 234 subject = '[' + band + '] Mailing List Confirmation'
bgneal@1 235
bgneal@1 236 send_mail(subject, msg, fromEmail, [to])
bgneal@1 237
bgneal@1 238 #######################################################################
bgneal@1 239
bgneal@1 240 def contact(request):
bgneal@1 241 config = SiteConfig.objects.get(pk = 1)
bgneal@1 242 band = Member.objects.exclude(is_active__exact = 0).order_by('order')
bgneal@1 243 return render_to_response('band/contact.html',
bgneal@1 244 { 'config' : config, 'band' : band },
bgneal@1 245 context_instance = RequestContext(request))
bgneal@1 246
bgneal@1 247 #######################################################################
bgneal@1 248
bgneal@1 249 class ContactForm(forms.Form):
bgneal@1 250 name = forms.CharField(max_length = 32, required = False,
bgneal@1 251 widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 252 email = forms.EmailField(widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 253 location = forms.CharField(max_length = 32, required = False,
bgneal@1 254 widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 255 option = forms.ChoiceField(choices = (('subscribe', 'Subscribe'), ('unsubscribe', 'Unsubscribe')),
bgneal@1 256 widget = forms.Select(attrs = {'class' : 'form-box'}))
bgneal@1 257
bgneal@1 258 def mail(request):
bgneal@1 259 config = SiteConfig.objects.get(pk = 1)
bgneal@1 260 form = ContactForm()
bgneal@1 261 if request.method == 'POST':
bgneal@1 262 form = ContactForm(request.POST)
bgneal@1 263 if form.is_valid():
bgneal@1 264 if form.cleaned_data['option'] == 'unsubscribe':
bgneal@1 265 try:
bgneal@1 266 fan = Fan.objects.get(email = form.cleaned_data['email'])
bgneal@1 267 except Fan.DoesNotExist:
bgneal@1 268 return HttpResponseRedirect(reverse(mail_not_found))
bgneal@1 269
bgneal@1 270 fan.setLeaving()
bgneal@1 271 fan.save()
bgneal@1 272 confirmEmail(config, fan.email, False, fan.key)
bgneal@1 273 return HttpResponseRedirect(reverse(mail_unsubscribe))
bgneal@1 274
bgneal@1 275 elif form.cleaned_data['option'] == 'subscribe':
bgneal@1 276 try:
bgneal@1 277 fan = Fan.objects.get(email = form.cleaned_data['email'])
bgneal@1 278 except Fan.DoesNotExist:
bgneal@1 279 fan = Fan(name = form.cleaned_data['name'],
bgneal@1 280 email = form.cleaned_data['email'],
bgneal@1 281 location = form.cleaned_data['location'])
bgneal@1 282
bgneal@1 283 fan.setPending()
bgneal@1 284 fan.save()
bgneal@1 285 confirmEmail(config, fan.email, True, fan.key)
bgneal@1 286 return HttpResponseRedirect(reverse(mail_thanks))
bgneal@1 287
bgneal@1 288 return render_to_response('band/mail.html',
bgneal@1 289 { 'form' : form },
bgneal@1 290 context_instance = RequestContext(request))
bgneal@1 291
bgneal@1 292 #######################################################################
bgneal@1 293
bgneal@1 294 def mail_not_found(request):
bgneal@1 295 return render_to_response('band/mail_not_found.html',
bgneal@1 296 {},
bgneal@1 297 context_instance = RequestContext(request))
bgneal@1 298
bgneal@1 299 #######################################################################
bgneal@1 300
bgneal@1 301 def mail_thanks(request):
bgneal@1 302 return render_to_response('band/mail_thanks.html',
bgneal@1 303 {},
bgneal@1 304 context_instance = RequestContext(request))
bgneal@1 305
bgneal@1 306 #######################################################################
bgneal@1 307
bgneal@1 308 def mail_unsubscribe(request):
bgneal@1 309 return render_to_response('band/mail_unsubscribe.html',
bgneal@1 310 {},
bgneal@1 311 context_instance = RequestContext(request))
bgneal@1 312
bgneal@1 313 #######################################################################
bgneal@1 314
bgneal@1 315 def mail_confirm(request, key):
bgneal@1 316 fan = get_object_or_404(Fan, key = key)
bgneal@1 317
bgneal@1 318 email = fan.email
bgneal@1 319 action = 'subscribed'
bgneal@1 320
bgneal@1 321 if fan.isPending():
bgneal@1 322 fan.setActive()
bgneal@1 323 fan.save()
bgneal@1 324 elif fan.isLeaving():
bgneal@1 325 fan.delete()
bgneal@1 326 action = 'unsubscribed'
bgneal@1 327
bgneal@1 328 return render_to_response('band/mail_confirm.html',
bgneal@1 329 { 'email' : email, 'action' : action },
bgneal@1 330 context_instance = RequestContext(request))
bgneal@1 331
bgneal@1 332 #######################################################################
bgneal@1 333
bgneal@1 334 def flyers(request):
bgneal@1 335
bgneal@1 336 gigs = Gig.objects.exclude(flyer__isnull = True).order_by('-date')
bgneal@1 337
bgneal@1 338 return render_to_response('band/flyers.html',
bgneal@1 339 { 'gigs' : gigs },
bgneal@1 340 context_instance = RequestContext(request))