annotate mysite/band/views.py @ 2:ffef0899826e

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