Commit aaaa9e09 authored by Simon Daron's avatar Simon Daron

Import views locally for emissions & episodes

parent 49c0295f
......@@ -14,7 +14,8 @@ urlpatterns = patterns('',
url(r'^(?P<year>\d{4})/(?P<week>\d+)/$', 'panikweb.views.week', name='program_week'),
url(r'^grid$', 'panikweb.views.grid', name='grid'),
url(r'^emissions/$', 'panikweb.views.emissions', name='emissions'),
url(r'^emissions/', include('emissions.urls')),
url(r'^emissions/(?P<slug>[\w,-]+)/$', 'panikweb.views.emission', name='emission-view'),
url(r'^emissions/(?P<emission_slug>[\w,-]+)/(?P<slug>[\w,-]+)/$', 'panikweb.views.episode', name='episode-view'),
url(r'^ckeditor/', include('ckeditor.urls')),
url(r'^get$', 'panikweb.views.get', name='get'),
url(r'^listen$', 'panikweb.views.listen', name='listen'),
......
......@@ -15,6 +15,76 @@ from emissions.models import Category, Emission, Episode, Diffusion, SoundFile,
from emissions.utils import whatsonair, period_program
class EmissionDetailView(DetailView):
model = Emission
def get_context_data(self, **kwargs):
context = super(EmissionDetailView, self).get_context_data(**kwargs)
context['schedules'] = Schedule.objects.filter(emission=self.object).order_by('datetime')
# get all episodes, with an additional attribute to get the date of
# their first diffusion
context['episodes'] = \
Episode.objects.select_related().filter(emission=self.object
).extra(select={
'first_diffusion': 'emissions_diffusion.datetime',
},
select_params=(False, True),
where=['''datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)'''],
tables=['emissions_diffusion'],
).order_by('-first_diffusion')
# get all related soundfiles in a single query
soundfiles = {}
for soundfile in SoundFile.objects.filter(podcastable=True,
fragment=False, episode__emission=self.object):
soundfiles[soundfile.episode_id] = soundfile
# replace dynamic property by a static attribute, to avoid database
# lookups
for episode in context['episodes']:
episode.main_sound = soundfiles.get(episode.id)
return context
emission = EmissionDetailView.as_view()
class EpisodeDetailView(DetailView):
model = Episode
def get_context_data(self, **kwargs):
context = super(EpisodeDetailView, self).get_context_data(**kwargs)
context['diffusions'] = Diffusion.objects.filter(episode=self.object.id)
context['soundfiles'] = SoundFile.objects.filter(episode=self.object.id)
context['emission'] = Emission.objects.get(slug=self.kwargs.get('emission_slug'))
# get all episodes, with an additional attribute to get the date of
# their first diffusion
context['episodes'] = \
Episode.objects.filter(emission=context['emission']
).extra(select={
'first_diffusion': 'emissions_diffusion.datetime',
},
select_params=(False, True),
where=['''datetime = (SELECT MIN(datetime)
FROM emissions_diffusion
WHERE episode_id = emissions_episode.id)'''],
tables=['emissions_diffusion'],
).order_by('-first_diffusion')
# get all related soundfiles in a single query
soundfiles = {}
for soundfile in SoundFile.objects.filter(podcastable=True,
fragment=False, episode__emission=self.object):
soundfiles[soundfile.episode_id] = soundfile
# replace dynamic property by a static attribute, to avoid database
# lookups
for episode in context['episodes']:
episode.main_sound = soundfiles.get(episode.id)
return context
episode = EpisodeDetailView.as_view()
class ProgramView(TemplateView):
template_name = 'program.html'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment