view mysite/band/views.py @ 4:432f7467543a

Added a BSD style license file.
author Brian Neal <bgneal@gmail.com>
date Sun, 19 Apr 2009 18:36:56 +0000
parents 0dcfcdf50c62
children e602b5302b94
line wrap: on
line source
#######################################################################
#
# PyBand Copyright (C) 2008 by Brian Neal
#
#######################################################################
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.models import Q
from mysite.band.models import Article
from mysite.band.models import Album
from mysite.band.models import Fan
from mysite.band.models import Gear
from mysite.band.models import Gig
from mysite.band.models import Member
from mysite.band.models import Merchandise
from mysite.band.models import Mp3
from mysite.band.models import Mp3_Set
from mysite.band.models import News
from mysite.band.models import SiteConfig
from mysite.band.models import Video_Set
from mysite.photologue.models import Gallery
from mysite.photologue.models import Photo
import datetime
import random

#######################################################################

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]

   #tourPhotos = Photo.objects.filter(
   #   Q(slug = 'flyer-aug-1-2008-san-diego') |
   #   Q(slug = 'flyer-aug-2-2008-hermosa-beach') 
   #   ).order_by('id')

   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):
   upcoming = Gig.objects.select_related().filter(date__gte = datetime.date.today).order_by('date')
   previous = Gig.objects.select_related().filter(date__lt = datetime.date.today)
   #upcoming = Gig.objects.filter(date__gte = datetime.date.today).order_by('date')
   #previous = Gig.objects.filter(date__lt = datetime.date.today)

   stats = {}
   venues = set([])
   cities = set([])
   states = set([])
   bands = set([])
   for gig in previous:
      if gig.venue.id not in venues:
         venues.add(gig.venue.id)
      if gig.venue.city.id not in cities:
         cities.add(gig.venue.city.id)
      if gig.venue.city.state and gig.venue.city.state.id not in states:
         states.add(gig.venue.city.state.id)
      for band in gig.bands.all():
         if band.id not in bands:
            bands.add(band.id)

   stats['count'] = previous.count()
   stats['venues'] = len(venues)
   stats['cities'] = len(cities)
   stats['states'] = len(states)
   stats['bands'] = len(bands)

   flyerGigs = Gig.objects.exclude(flyer__isnull = True).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)
   return render_to_response('band/photo_detail.html',
         { 'gallery' : gallery },
         context_instance = RequestContext(request))

#######################################################################

def videos_index(request):
   vidsets = Video_Set.objects.values('title', 'id').order_by('-id')
   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))