annotate mysite/band/views.py @ 88:7245c769e31e django1.3

Close this branch. I'm not sure if I merged it correctly to the default branch, because the graphlog doesn't look right. But the changes were made to default somehow. So closing this off to prevent future confusion.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Apr 2013 18:08:19 -0500
parents dac690ab98b2
children a404f93a68d3
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@5 80 countries = set([])
bgneal@1 81 bands = set([])
bgneal@1 82 for gig in previous:
bgneal@1 83 if gig.venue.id not in venues:
bgneal@1 84 venues.add(gig.venue.id)
bgneal@1 85 if gig.venue.city.id not in cities:
bgneal@1 86 cities.add(gig.venue.city.id)
bgneal@1 87 if gig.venue.city.state and gig.venue.city.state.id not in states:
bgneal@1 88 states.add(gig.venue.city.state.id)
bgneal@5 89 if gig.venue.city.country and gig.venue.city.country.id not in countries:
bgneal@5 90 countries.add(gig.venue.city.country.id)
bgneal@1 91 for band in gig.bands.all():
bgneal@1 92 if band.id not in bands:
bgneal@1 93 bands.add(band.id)
bgneal@1 94
bgneal@1 95 stats['count'] = previous.count()
bgneal@1 96 stats['venues'] = len(venues)
bgneal@1 97 stats['cities'] = len(cities)
bgneal@1 98 stats['states'] = len(states)
bgneal@5 99 stats['countries'] = len(countries)
bgneal@1 100 stats['bands'] = len(bands)
bgneal@1 101
bgneal@1 102 flyerGigs = Gig.objects.exclude(flyer__isnull = True).order_by('-date')
bgneal@1 103
bgneal@1 104 return render_to_response('band/gigs.html',
bgneal@1 105 {
bgneal@1 106 'upcoming' : upcoming,
bgneal@1 107 'previous' : previous,
bgneal@1 108 'stats' : stats,
bgneal@1 109 'flyerGigs' : flyerGigs,
bgneal@1 110 },
bgneal@1 111 context_instance = RequestContext(request))
bgneal@1 112
bgneal@1 113 #######################################################################
bgneal@1 114
bgneal@1 115 def news(request):
bgneal@1 116 news = News.objects.order_by('-date')
bgneal@1 117
bgneal@1 118 return render_to_response('band/news.html',
bgneal@1 119 {
bgneal@1 120 'news' : news
bgneal@1 121 },
bgneal@1 122 context_instance = RequestContext(request))
bgneal@1 123
bgneal@1 124 #######################################################################
bgneal@1 125
bgneal@1 126 def press_index(request):
bgneal@1 127 articles = Article.objects.order_by('-date')
bgneal@1 128
bgneal@1 129 return render_to_response('band/press.html',
bgneal@1 130 {
bgneal@1 131 'articles' : articles
bgneal@1 132 },
bgneal@1 133 context_instance = RequestContext(request))
bgneal@1 134
bgneal@1 135 #######################################################################
bgneal@1 136
bgneal@1 137 def press_detail(request, id):
bgneal@1 138 article = get_object_or_404(Article, pk = id)
bgneal@1 139
bgneal@1 140 return render_to_response('band/press_detail.html',
bgneal@1 141 { 'article' : article },
bgneal@1 142 context_instance = RequestContext(request))
bgneal@1 143
bgneal@1 144 #######################################################################
bgneal@1 145
bgneal@1 146 def songs(request):
bgneal@1 147 mp3Sets = Mp3_Set.objects.order_by('-date', '-id')
bgneal@1 148
bgneal@1 149 return render_to_response('band/songs.html',
bgneal@1 150 { 'mp3Sets' : mp3Sets },
bgneal@1 151 context_instance = RequestContext(request))
bgneal@1 152
bgneal@1 153 #######################################################################
bgneal@1 154
bgneal@1 155 def photos_index(request):
bgneal@1 156 galleries = Gallery.objects.values('title', 'id').order_by('-id')
bgneal@1 157
bgneal@1 158 photos = Photo.objects.filter(is_public__exact = 1)
bgneal@1 159 randomPhotos = random.sample(photos, 4)
bgneal@1 160
bgneal@1 161 return render_to_response('band/photos.html',
bgneal@1 162 { 'galleries' : galleries, 'randomPhotos' : randomPhotos },
bgneal@1 163 context_instance = RequestContext(request))
bgneal@1 164
bgneal@1 165 #######################################################################
bgneal@1 166
bgneal@1 167 def photo_detail(request, id):
bgneal@1 168 gallery = get_object_or_404(Gallery, pk = id)
bgneal@1 169 return render_to_response('band/photo_detail.html',
bgneal@1 170 { 'gallery' : gallery },
bgneal@1 171 context_instance = RequestContext(request))
bgneal@1 172
bgneal@1 173 #######################################################################
bgneal@1 174
bgneal@1 175 def videos_index(request):
bgneal@9 176 vidsets = Video_Set.objects.values('title', 'id').order_by('-date')
bgneal@1 177 return render_to_response('band/videos.html',
bgneal@1 178 { 'vidsets' : vidsets },
bgneal@1 179 context_instance = RequestContext(request))
bgneal@1 180
bgneal@1 181 #######################################################################
bgneal@1 182
bgneal@1 183 def video_detail(request, id):
bgneal@1 184 vidset = get_object_or_404(Video_Set, pk = id)
bgneal@1 185
bgneal@1 186 return render_to_response('band/video_detail.html',
bgneal@1 187 { 'vidset' : vidset },
bgneal@1 188 context_instance = RequestContext(request))
bgneal@1 189
bgneal@1 190 #######################################################################
bgneal@1 191
bgneal@1 192 def buy(request):
bgneal@1 193 albums = Album.objects.all().order_by('-id')
bgneal@1 194 merchandise = Merchandise.objects.all().order_by('-id')
bgneal@1 195 config = SiteConfig.objects.values('ordering_info').get(pk = 1)
bgneal@1 196 return render_to_response('band/buy.html',
bgneal@1 197 { 'albums' : albums, 'merchandise' : merchandise, 'config' : config },
bgneal@1 198 context_instance = RequestContext(request))
bgneal@1 199
bgneal@1 200 #######################################################################
bgneal@1 201
bgneal@1 202 def confirmEmail(config, to, subscribe, key):
bgneal@1 203 band = config.band_name
bgneal@1 204 fromEmail = config.contact_email
bgneal@1 205 url = config.url
bgneal@1 206 if url[-1] != '/':
bgneal@1 207 url += '/'
bgneal@1 208 url += 'mail/confirm/' + key
bgneal@1 209
bgneal@1 210 if subscribe:
bgneal@1 211 emailTemplate = 'band/email_subscribe.txt'
bgneal@1 212 else:
bgneal@1 213 emailTemplate = 'band/email_unsubscribe.txt'
bgneal@1 214
bgneal@1 215 msg = render_to_string(emailTemplate, { 'band' : band, 'url' : url, 'band_url' : config.url })
bgneal@1 216
bgneal@1 217 subject = '[' + band + '] Mailing List Confirmation'
bgneal@1 218
bgneal@1 219 send_mail(subject, msg, fromEmail, [to])
bgneal@1 220
bgneal@1 221 #######################################################################
bgneal@1 222
bgneal@1 223 def contact(request):
bgneal@1 224 config = SiteConfig.objects.get(pk = 1)
bgneal@1 225 band = Member.objects.exclude(is_active__exact = 0).order_by('order')
bgneal@1 226 return render_to_response('band/contact.html',
bgneal@1 227 { 'config' : config, 'band' : band },
bgneal@1 228 context_instance = RequestContext(request))
bgneal@1 229
bgneal@1 230 #######################################################################
bgneal@1 231
bgneal@1 232 class ContactForm(forms.Form):
bgneal@1 233 name = forms.CharField(max_length = 32, required = False,
bgneal@1 234 widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 235 email = forms.EmailField(widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 236 location = forms.CharField(max_length = 32, required = False,
bgneal@1 237 widget = forms.TextInput(attrs = {'class' : 'form-box'}))
bgneal@1 238 option = forms.ChoiceField(choices = (('subscribe', 'Subscribe'), ('unsubscribe', 'Unsubscribe')),
bgneal@1 239 widget = forms.Select(attrs = {'class' : 'form-box'}))
bgneal@1 240
bgneal@1 241 def mail(request):
bgneal@1 242 config = SiteConfig.objects.get(pk = 1)
bgneal@1 243 form = ContactForm()
bgneal@1 244 if request.method == 'POST':
bgneal@1 245 form = ContactForm(request.POST)
bgneal@1 246 if form.is_valid():
bgneal@1 247 if form.cleaned_data['option'] == 'unsubscribe':
bgneal@1 248 try:
bgneal@1 249 fan = Fan.objects.get(email = form.cleaned_data['email'])
bgneal@1 250 except Fan.DoesNotExist:
bgneal@1 251 return HttpResponseRedirect(reverse(mail_not_found))
bgneal@1 252
bgneal@1 253 fan.setLeaving()
bgneal@1 254 fan.save()
bgneal@1 255 confirmEmail(config, fan.email, False, fan.key)
bgneal@1 256 return HttpResponseRedirect(reverse(mail_unsubscribe))
bgneal@1 257
bgneal@1 258 elif form.cleaned_data['option'] == 'subscribe':
bgneal@1 259 try:
bgneal@1 260 fan = Fan.objects.get(email = form.cleaned_data['email'])
bgneal@1 261 except Fan.DoesNotExist:
bgneal@1 262 fan = Fan(name = form.cleaned_data['name'],
bgneal@1 263 email = form.cleaned_data['email'],
bgneal@1 264 location = form.cleaned_data['location'])
bgneal@1 265
bgneal@1 266 fan.setPending()
bgneal@1 267 fan.save()
bgneal@1 268 confirmEmail(config, fan.email, True, fan.key)
bgneal@1 269 return HttpResponseRedirect(reverse(mail_thanks))
bgneal@1 270
bgneal@1 271 return render_to_response('band/mail.html',
bgneal@1 272 { 'form' : form },
bgneal@1 273 context_instance = RequestContext(request))
bgneal@1 274
bgneal@1 275 #######################################################################
bgneal@1 276
bgneal@1 277 def mail_not_found(request):
bgneal@1 278 return render_to_response('band/mail_not_found.html',
bgneal@1 279 {},
bgneal@1 280 context_instance = RequestContext(request))
bgneal@1 281
bgneal@1 282 #######################################################################
bgneal@1 283
bgneal@1 284 def mail_thanks(request):
bgneal@1 285 return render_to_response('band/mail_thanks.html',
bgneal@1 286 {},
bgneal@1 287 context_instance = RequestContext(request))
bgneal@1 288
bgneal@1 289 #######################################################################
bgneal@1 290
bgneal@1 291 def mail_unsubscribe(request):
bgneal@1 292 return render_to_response('band/mail_unsubscribe.html',
bgneal@1 293 {},
bgneal@1 294 context_instance = RequestContext(request))
bgneal@1 295
bgneal@1 296 #######################################################################
bgneal@1 297
bgneal@1 298 def mail_confirm(request, key):
bgneal@1 299 fan = get_object_or_404(Fan, key = key)
bgneal@1 300
bgneal@1 301 email = fan.email
bgneal@1 302 action = 'subscribed'
bgneal@1 303
bgneal@1 304 if fan.isPending():
bgneal@1 305 fan.setActive()
bgneal@1 306 fan.save()
bgneal@1 307 elif fan.isLeaving():
bgneal@1 308 fan.delete()
bgneal@1 309 action = 'unsubscribed'
bgneal@1 310
bgneal@1 311 return render_to_response('band/mail_confirm.html',
bgneal@1 312 { 'email' : email, 'action' : action },
bgneal@1 313 context_instance = RequestContext(request))
bgneal@1 314
bgneal@1 315 #######################################################################
bgneal@1 316
bgneal@1 317 def flyers(request):
bgneal@1 318
bgneal@1 319 gigs = Gig.objects.exclude(flyer__isnull = True).order_by('-date')
bgneal@1 320
bgneal@1 321 return render_to_response('band/flyers.html',
bgneal@1 322 { 'gigs' : gigs },
bgneal@1 323 context_instance = RequestContext(request))