-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
296 lines (234 loc) · 8.82 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from django.views.generic.list_detail import object_list
from django.views.generic.list_detail import object_detail
from django.contrib.sites.models import Site
from podcast.models import Episode, Show, Enclosure
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from django.views.generic.create_update import create_object, update_object, delete_object
from podcast.forms import ShowForm, EpisodeForm, EnclosureForm
from random import choice
def episode_detail(request, show_slug, episode_slug):
"""
Episode detail
Template: ``podcast/episode_detail.html``
Context:
object_detail
Detail of episode.
"""
try:
show = Show.objects.get(slug = show_slug, author=request.user)
is_me = True
except:
show = Show.objects.get(slug = show_slug)
is_me = False
context={ 'enclosure_list': Enclosure.objects.filter(episode__show__slug__exact=show_slug).filter(episode__slug__exact=episode_slug).order_by('-episode__date'),\
'show':show, 'is_me': is_me, }
return object_detail(
request,
queryset=Episode.objects.published().filter(show__slug__exact=show_slug),
slug=episode_slug,
slug_field='slug',
template_name='podcast/episode_detail.html',
extra_context = context)
def episode_list(request, slug):
"""
Episode list
Template: ``podcast/episode_list.html``
Context:
object_list
List of episodes.
"""
try:
show = Show.objects.get(slug=slug, author=request.user)
is_me = True
except:
try:
show = Show.objects.get(slug=slug)
is_me = False
except:
return HttpResponseRedirect(reverse('podcast_shows'))
context = {'is_me': is_me, 'show':show}
return object_list(
request,
queryset=Episode.objects.published().filter(show__slug__exact=slug),
template_name='podcast/episode_list.html', extra_context=context)
def episode_sitemap(request, slug):
"""
Episode sitemap
Template: ``podcast/episode_sitemap.html``
Context:
object_list
List of episodes.
"""
return object_list(
request,
mimetype='application/xml',
queryset=Episode.objects.published().filter(show__slug__exact=slug).order_by('-date'),
extra_context={
'enclosure_list': Enclosure.objects.filter(episode__show__slug__exact=slug).order_by('-episode__date')},
template_name='podcast/episode_sitemap.html')
def show_list(request):
"""
Episode list
Template: ``podcast/show_list.html``
Context:
object_list
List of shows.
"""
return object_list(
request,
queryset=Show.objects.all().order_by('title'),
template_name='podcast/show_list.html')
def show_list_feed(request, slug):
"""
Episode feed by show
Template: ``podcast/show_feed.html``
Context:
object_list
List of episodes by show.
"""
try:
show = Show.objects.get(slug=slug)
except:
return HttpResponseRedirect(reverse('podcast_shows'))
site = Site.objects.get_current()
context = {'show':show, 'site':site,}
return object_list(
request,
mimetype='application/rss+xml',
queryset=Episode.objects.filter(show__slug=slug).order_by('-date')[:],
template_name='podcast/show_feed.html',
extra_context = context)
def show_list_media(request, slug):
"""
Episode feed by show
Template: ``podcast/show_feed_media.html``
Context:
object_list
List of episodes by show.
"""
return object_list(
request,
mimetype='application/rss+xml',
queryset=Episode.objects.filter(show__slug__exact=slug).order_by('-date')[0:21],
extra_context={
'enclosure_list': Enclosure.objects.filter(episode__show__slug__exact=slug).order_by('-episode__date')},
template_name='podcast/show_feed_media.html')
def show_list_atom(request, slug):
"""
Episode feed by show
Template: ``podcast/show_feed_atom.html``
Context:
object_list
List of episodes by show.
"""
try:
show = Show.objects.get(slug=slug)
except:
return HttpResponseRedirect(reverse('podcast_shows'))
site = Site.objects.get_current()
encs = Enclosure.objects.filter(episode__show__slug__exact=slug).order_by('-episode__date')
context = {'show':show, 'site':site, 'enclosures':encs, }
return object_list(
request,
mimetype='application/rss+xml',
queryset=Episode.objects.filter(show__slug__exact=slug).order_by('-date')[0:21],
template_name='podcast/show_feed_atom.html',
extra_context=context)
@login_required
def yours(request):
context={}
return object_list(request, template_name = 'podcast/yours.html',\
queryset = Show.objects.filter(author=request.user),
extra_context = context)
@login_required
def friends(request):
context={}
return object_list('podcast/friends.html', context,\
context_instance=RequestContext(request))
@login_required
def create(request):
if request.method == 'POST':
form = ShowForm(request.POST)
if form.is_valid():
s = form.save(commit=False)
s.webmaster = request.user
s.author = request.user
s.slug = slugify(s.title)
s.save()
#except:
# s.slug = slugify(s.title) +\
# ''.join([choice('0123456789-_') for i in range(7)])
# s.save()
#finally:
return HttpResponseRedirect(reverse('podcast_shows'))
else:
context={}
return create_object(request, form_class=ShowForm, template_name="podcast/create_show.html")
@login_required
def update(request, slug):
context={}
return update_object(request, model=Show, slug=slug, slug_field='slug',\
login_required=True, template_name="podcast/update_show.html",\
extra_context=context)
def episode_add(request, slug):
''' create a new episode '''
try:
show = Show.objects.get(slug=slug, author=request.user)
except:
return HttpResponseRedirect(reverse('podcast_shows'))
if request.method == 'POST':
form = EpisodeForm(request.POST)
if form.is_valid():
e = form.save(commit=False)
e.show = show
e.slug = slugify(e.title)
e.save()
#except:
# e.slug = slugify(e.title) + \
# ''.join([choice('0123456789-_') for i in range(7)])
# e.save()
#finally:
return HttpResponseRedirect(reverse('podcast_episode', args=[slug, e.slug]))
context={'show':show}
return create_object(request, form_class=EpisodeForm,\
template_name="podcast/create_episode.html", \
extra_context=context)
def enclosure_add(request, show_slug, episode_slug):
''' create a new enclosure for an episode '''
try:
show = Show.objects.get(slug=show_slug, author=request.user)
episode = Episode.objects.get(slug=episode_slug)
except:
return HttpResponseRedirect(reverse('podcast_shows'))
if request.method == 'POST':
form = EnclosureForm(request.POST, request.FILES)
if form.is_valid():
en = form.save(commit=False)
en.episode = episode
en.slug = slugify(en.title)
en.save()
return HttpResponseRedirect(reverse('podcast_episode',\
args=[show_slug, episode_slug]))
context={'show':show, 'episode':episode, }
return create_object(request, form_class = EnclosureForm,\
template_name="podcast/create_enclosure.html",\
extra_context = context)
@login_required
def episode_delete(request, show_slug, episode_slug):
#e = Episode.objects.get(slug=slug)
#if e.show.author == request.user:
return delete_object(request, model=Episode, slug=episode_slug, slug_field='slug',\
post_delete_redirect=reverse('podcast_episodes', args=[show_slug]),
template_name="podcast/confirm_delete.html")
@login_required
def enclosure_delete(request, show_slug, episode_slug, enclosure_slug):
#e = Episode.objects.get(slug=slug)
#if e.show.author == request.user:
return delete_object(request, model=Enclosure, slug=enclosure_slug, slug_field='slug',\
post_delete_redirect=reverse('podcast_episode', args=[show_slug,\
episode_slug]), template_name="podcast/confirm_delete.html")