comparison mysite/band/views.py @ 1:0dcfcdf50c62

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