Mercurial > public > madeira
view mysite/band/views.py @ 40:25e00d1b99bf
Get rid of the project name 'mysite' from the source.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 14 Feb 2012 19:09:57 -0600 |
parents | f61db5f82549 |
children |
line wrap: on
line source
####################################################################### # # PyBand Copyright (C) 2008 - 2011 by Brian Neal # ####################################################################### import collections import datetime import random from django import forms from django.core.urlresolvers import reverse from django.http import HttpResponse from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.shortcuts import get_object_or_404 from django.template import RequestContext from django.template.loader import render_to_string from django.core.mail import send_mail from django.db import connection from band.models import Article from band.models import Album from band.models import Band from band.models import Fan from band.models import Gear from band.models import Gig from band.models import Member from band.models import Merchandise from band.models import Mp3 from band.models import Mp3_Set from band.models import News from band.models import SiteConfig from band.models import Video_Set from photologue.models import Gallery from photologue.models import Photo ####################################################################### def index(request): config = SiteConfig.objects.get(pk = 1) carpe = Photo.objects.get(title_slug = 'carpe-noctem') sandstorm = Photo.objects.get(title_slug = 'sandstorm-cover') ruins = Photo.objects.get(title_slug = 'ruins-cover') upcomingDates = Gig.objects.filter(date__gte = datetime.date.today).order_by('date')[:5] return render_to_response('band/index.html', { 'config' : config, 'carpe' : carpe, 'sandstorm' : sandstorm, 'ruins' : ruins, 'upcomingDates' : upcomingDates, # 'tourPhotos' : tourPhotos, }, context_instance = RequestContext(request)) ####################################################################### def bio(request): members = Member.objects.exclude(is_active__exact = 0) return render_to_response('band/bio.html', { 'members' : members, }, context_instance = RequestContext(request)) ####################################################################### def gigs(request): today = datetime.date.today() gigs = Gig.objects.select_related('venue', 'flyer', 'venue__city', 'venue__city__state', 'venue__city__country') upcoming = [] previous = [] # To avoid many, many database hits in the template, we get all the # bands out at once. We also get the many-to-many intermediate table # that Django generated for us so we can associate bands to gigs. # Since we don't know about this table we drop into raw SQL to get # the contents. bands = dict((band.id, band) for band in Band.objects.all()) cursor = connection.cursor() cursor.execute('SELECT * FROM band_gig_bands') gig_bands = collections.defaultdict(list) for row in cursor.fetchall(): gig_bands[row[1]].append(bands[row[2]]) for gig in gigs: gig.bands_ = gig_bands[gig.id] if gig.date >= today: upcoming.append(gig) else: previous.append(gig) upcoming.reverse() stats = {} venues = set() cities = set() states = set() countries = set() for gig in previous: venues.add(gig.venue.id) cities.add(gig.venue.city.id) if gig.venue.city.state: states.add(gig.venue.city.state.id) if gig.venue.city.country: countries.add(gig.venue.city.country.id) stats['count'] = len(previous) stats['venues'] = len(venues) stats['cities'] = len(cities) stats['states'] = len(states) stats['countries'] = len(countries) stats['bands'] = len(bands) flyerGigs = Gig.objects.exclude(flyer__isnull = True).select_related( 'venue', 'flyer').order_by('-date') return render_to_response('band/gigs.html', { 'upcoming' : upcoming, 'previous' : previous, 'stats' : stats, 'flyerGigs' : flyerGigs, }, context_instance = RequestContext(request)) ####################################################################### def news(request): news = News.objects.order_by('-date') return render_to_response('band/news.html', { 'news' : news }, context_instance = RequestContext(request)) ####################################################################### def press_index(request): articles = Article.objects.order_by('-date') return render_to_response('band/press.html', { 'articles' : articles }, context_instance = RequestContext(request)) ####################################################################### def press_detail(request, id): article = get_object_or_404(Article, pk = id) return render_to_response('band/press_detail.html', { 'article' : article }, context_instance = RequestContext(request)) ####################################################################### def songs(request): mp3Sets = Mp3_Set.objects.order_by('-date', '-id') return render_to_response('band/songs.html', { 'mp3Sets' : mp3Sets }, context_instance = RequestContext(request)) ####################################################################### def photos_index(request): galleries = Gallery.objects.values('title', 'id').order_by('-id') photos = Photo.objects.filter(is_public__exact = 1) randomPhotos = random.sample(photos, 4) return render_to_response('band/photos.html', { 'galleries' : galleries, 'randomPhotos' : randomPhotos }, context_instance = RequestContext(request)) ####################################################################### def photo_detail(request, id): gallery = get_object_or_404(Gallery, pk = id) photos = gallery.photos.order_by('id') return render_to_response('band/photo_detail.html', {'gallery' : gallery, 'photos': photos }, context_instance = RequestContext(request)) ####################################################################### def videos_index(request): vidsets = Video_Set.objects.values('title', 'id').order_by('-date') return render_to_response('band/videos.html', { 'vidsets' : vidsets }, context_instance = RequestContext(request)) ####################################################################### def video_detail(request, id): vidset = get_object_or_404(Video_Set, pk = id) return render_to_response('band/video_detail.html', { 'vidset' : vidset }, context_instance = RequestContext(request)) ####################################################################### def buy(request): albums = Album.objects.all().order_by('-id') merchandise = Merchandise.objects.all().order_by('-id') config = SiteConfig.objects.values('ordering_info').get(pk = 1) return render_to_response('band/buy.html', { 'albums' : albums, 'merchandise' : merchandise, 'config' : config }, context_instance = RequestContext(request)) ####################################################################### def confirmEmail(config, to, subscribe, key): band = config.band_name fromEmail = config.contact_email url = config.url if url[-1] != '/': url += '/' url += 'mail/confirm/' + key if subscribe: emailTemplate = 'band/email_subscribe.txt' else: emailTemplate = 'band/email_unsubscribe.txt' msg = render_to_string(emailTemplate, { 'band' : band, 'url' : url, 'band_url' : config.url }) subject = '[' + band + '] Mailing List Confirmation' send_mail(subject, msg, fromEmail, [to]) ####################################################################### def contact(request): config = SiteConfig.objects.get(pk = 1) band = Member.objects.exclude(is_active__exact = 0).order_by('order') return render_to_response('band/contact.html', { 'config' : config, 'band' : band }, context_instance = RequestContext(request)) ####################################################################### class ContactForm(forms.Form): name = forms.CharField(max_length = 32, required = False, widget = forms.TextInput(attrs = {'class' : 'form-box'})) email = forms.EmailField(widget = forms.TextInput(attrs = {'class' : 'form-box'})) location = forms.CharField(max_length = 32, required = False, widget = forms.TextInput(attrs = {'class' : 'form-box'})) option = forms.ChoiceField(choices = (('subscribe', 'Subscribe'), ('unsubscribe', 'Unsubscribe')), widget = forms.Select(attrs = {'class' : 'form-box'})) def mail(request): config = SiteConfig.objects.get(pk = 1) form = ContactForm() if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): if form.cleaned_data['option'] == 'unsubscribe': try: fan = Fan.objects.get(email = form.cleaned_data['email']) except Fan.DoesNotExist: return HttpResponseRedirect(reverse(mail_not_found)) fan.setLeaving() fan.save() confirmEmail(config, fan.email, False, fan.key) return HttpResponseRedirect(reverse(mail_unsubscribe)) elif form.cleaned_data['option'] == 'subscribe': try: fan = Fan.objects.get(email = form.cleaned_data['email']) except Fan.DoesNotExist: fan = Fan(name = form.cleaned_data['name'], email = form.cleaned_data['email'], location = form.cleaned_data['location']) fan.setPending() fan.save() confirmEmail(config, fan.email, True, fan.key) return HttpResponseRedirect(reverse(mail_thanks)) return render_to_response('band/mail.html', { 'form' : form }, context_instance = RequestContext(request)) ####################################################################### def mail_not_found(request): return render_to_response('band/mail_not_found.html', {}, context_instance = RequestContext(request)) ####################################################################### def mail_thanks(request): return render_to_response('band/mail_thanks.html', {}, context_instance = RequestContext(request)) ####################################################################### def mail_unsubscribe(request): return render_to_response('band/mail_unsubscribe.html', {}, context_instance = RequestContext(request)) ####################################################################### def mail_confirm(request, key): fan = get_object_or_404(Fan, key = key) email = fan.email action = 'subscribed' if fan.isPending(): fan.setActive() fan.save() elif fan.isLeaving(): fan.delete() action = 'unsubscribed' return render_to_response('band/mail_confirm.html', { 'email' : email, 'action' : action }, context_instance = RequestContext(request)) ####################################################################### def flyers(request): gigs = Gig.objects.exclude(flyer__isnull = True).order_by('-date') return render_to_response('band/flyers.html', { 'gigs' : gigs }, context_instance = RequestContext(request))