From 9b09c2ce17bbdd65a210b41f983ff8b6702d2545 Mon Sep 17 00:00:00 2001 From: Ernesto Rico-Schmidt Date: Sat, 2 Apr 2011 20:05:07 +0200 Subject: changed to function based generic views. diff --git a/program/urls.py b/program/urls.py index 637ad97..bf798ec 100644 --- a/program/urls.py +++ b/program/urls.py @@ -1,20 +1,20 @@ from django.conf.urls.defaults import * -from django.views.generic.detail import DetailView -from django.views.generic.list import ListView + +from django.views.generic.list_detail import object_detail, object_list from models import Host, Show, TimeSlot -from views import CurrentShowView, DayScheduleView, RecommendationsView, ShowListView, TodayScheduleView, WeekScheduleView +from views import current_show, day_schedule, recommendations, show_list, today_schedule, week_schedule urlpatterns = patterns('', - ('^$', TodayScheduleView.as_view()), - ('^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/$', DayScheduleView.as_view()), - ('^(?P\d{4})/(?P\d{1,2})/$', WeekScheduleView.as_view()), - ('^current_box/$', CurrentShowView.as_view()), - ('^hosts/$', ListView.as_view(model=Host, context_object_name='hosts')), - url('^host/(?P\d+)/$', DetailView.as_view(model=Host), name='host-detail'), - ('^recommendations/$', RecommendationsView.as_view()), - ('^recommendations_box/$', RecommendationsView.as_view(template_name='program/recommendations_box.html')), - ('^shows/$', ShowListView.as_view()), - url('^show/(?P[\w-]+)/$', DetailView.as_view(model=Show), name='show-detail'), - url('^(?P\d+)/$', DetailView.as_view(model=TimeSlot), name='timeslot-detail'), -) \ No newline at end of file + ('^$', today_schedule), + ('^(?P\d{4})/(?P\d{1,2})/(?P\d{1,2})/$', day_schedule), + ('^(?P\d{4})/(?P\d{1,2})/$', week_schedule), + ('^current_box/$', current_show), + ('^hosts/$', object_list, dict(template_object_name='host', queryset=Host.objects.all())), + url('^host/(?P\d+)/$', object_detail, dict(template_object_name='host', queryset=Host.objects.all()), name='host-detail'), + ('^recommendations/$', recommendations), + ('^recommendations_box/$', recommendations, dict(template_name='program/recommendations_box.html')), + ('^shows/$', show_list), + url('^show/(?P[\w-]+)/$', object_detail, dict(template_object_name='show', queryset=Show.objects.all()), name='show-detail'), + url('^(?P\d+)/$', object_detail, dict(template_object_name='timeslot', queryset=TimeSlot.objects.all()), name='timeslot-detail'), +) diff --git a/program/views.py b/program/views.py index 23c1483..b5f02b6 100644 --- a/program/views.py +++ b/program/views.py @@ -1,150 +1,113 @@ -from django.views.generic.list import ListView -from django.views.generic.base import TemplateView +from django.views.generic import list_detail +from django.views.generic import simple from django.shortcuts import get_object_or_404 from models import BroadcastFormat, MusicFocus, Note, Show, ShowInformation, ShowTopic, TimeSlot from datetime import date, datetime, time, timedelta -class ShowListView(ListView): - context_object_name = 'shows' +def show_list(request): + broadcastformats = BroadcastFormat.objects.all() + musicfoci = MusicFocus.objects.all() + showinformation = ShowInformation.objects.all() + showtopics = ShowTopic.objects.all() - def get_context_data(self, **kwargs): - context = super(ShowListView, self).get_context_data(**kwargs) + extra_context = dict(broadcastformats=broadcastformats, musicfoci=musicfoci, showinformation=showinformation, showtopics=showtopics) + + if 'broadcastformat' in request.GET: + broadcastformat = get_object_or_404(BroadcastFormat, slug=request.GET['broadcastformat']) - context['broadcastformats'] = BroadcastFormat.objects.all() - context['musicfoci'] = MusicFocus.objects.all() - context['showinformations'] = ShowInformation.objects.all() - context['showtopics'] = ShowTopic.objects.all() + queryset = Show.objects.filter(broadcastformat=broadcastformat) + elif 'musicfocus' in request.GET: + musicfocus = get_object_or_404(MusicFocus, slug=request.GET['musicfocus']) - return context + queryset = Show.objects.filter(musicfocus=musicfocus) + elif 'showinformation' in request.GET: + showinformation = get_object_or_404(ShowInformation, slug=request.GET['showinformation']) - def get_queryset(self): - if 'broadcastformat' in self.request.GET: - broadcastformat = get_object_or_404(BroadcastFormat, slug=self.request.GET['broadcastformat']) + queryset = Show.objects.filter(showinformation=showinformation) + elif 'showtopic' in request.GET: + showtopic = get_object_or_404(ShowTopic, slug=request.GET['showtopic']) - return Show.objects.filter(broadcastformat=broadcastformat) - elif 'musicfocus' in self.request.GET: - musicfocus = get_object_or_404(MusicFocus, slug=self.request.GET['musicfocus']) + queryset = Show.objects.filter(showtopic=showtopic) + else: + queryset = Show.objects.all() - return Show.objects.filter(musicfocus=musicfocus) - elif 'showinformation' in self.request.GET: - showinformation = get_object_or_404(ShowInformation, slug=self.request.GET['showinformation']) - return Show.objects.filter(showinformation=showinformation) - elif 'showtopic' in self.request.GET: - showtopic = get_object_or_404(ShowTopic, slug=self.request.GET['showtopic']) + return list_detail.object_list(request, queryset=queryset, extra_context=extra_context, template_object_name='show') - return Show.objects.filter(showtopic=showtopic) - else: - return Show.objects.all() +def recommendations(request, template_name='program/recommendations.html'): + now = datetime.now() + in_one_week = now + timedelta(weeks=1) -class RecommendationsView(ListView): - context_object_name = 'recommendations' - template_name = 'program/recommendations.html' + queryset = Note.objects.filter(status=1, timeslot__start__range=(now, in_one_week))[:10] - def get_queryset(self): - now = datetime.now() - in_one_week = now + timedelta(weeks=1) + return list_detail.object_list(request, queryset=queryset, template_name=template_name, template_object_name='recommendation') - return Note.objects.filter(status=1, timeslot__start__range=(now, in_one_week))[:10] +def today_schedule(request): + now = datetime.now() + today = datetime.combine(date.today(), time(6, 0)) + tomorrow = today + timedelta(days=1) -class TodayScheduleView(TemplateView): - template_name = 'program/day_schedule.html' + broadcastformats = BroadcastFormat.objects.all() + recommendations = Note.objects.filter(status=1, timeslot__start__range=(now, tomorrow)) - def get_context_data(self, **kwargs): - context = super(TodayScheduleView, self).get_context_data(**kwargs) + extra_context = dict(day=today, broadcastformats=broadcastformats, recommendations=recommendations) - now = datetime.now() - today = datetime.combine(date.today(), time(6, 0)) - tomorrow = datetime.combine(date.today()+timedelta(days=1), time(6, 0)) + if 'broadcastformat' in request.GET: + broadcastformat = get_object_or_404(BroadcastFormat, slug=request.GET['broadcastformat']) - context['day'] = today - context['broadcastformats'] = BroadcastFormat.objects.all() - context['recommendations'] = Note.objects.filter(status=1, timeslot__start__range=(now, tomorrow)) + extra_context['timeslots'] = TimeSlot.objects.filter(start__range=(today, tomorrow), show__broadcastformat=broadcastformat) + else: + extra_context['timeslots'] = TimeSlot.objects.filter(start__range=(today, tomorrow)) - if 'broadcastformat' in self.request.GET: - broadcastformat = get_object_or_404(BroadcastFormat, slug=self.request.GET['broadcastformat']) + return simple.direct_to_template(request, extra_context=extra_context, template='program/day_schedule.html') - context['timeslots'] = TimeSlot.objects.filter(start__range=(today, tomorrow), show__broadcastformat=broadcastformat) - else: - context['timeslots'] = TimeSlot.objects.filter(start__range=(today, tomorrow)) +def day_schedule(request, year, month, day): + this_day = datetime.strptime('%s__%s__%s__06__00' % (year, month, day), '%Y__%m__%d__%H__%M') + that_day = this_day+timedelta(days=1) - return context + broadcastformats = BroadcastFormat.objects.all() + recommendations = Note.objects.filter(status=1, timeslot__start__range=(this_day, that_day)) -class DayScheduleView(TemplateView): - template_name = 'program/day_schedule.html' + extra_context = dict(day=this_day, broadcastformats=broadcastformats, recommendations=recommendations) - def get_context_data(self, **kwargs): - context = super(DayScheduleView, self).get_context_data(**kwargs) + if 'broadcastformat' in request.GET: + broadcastformat = get_object_or_404(BroadcastFormat, slug=request.GET['broadcastformat']) - year = context['params']['year'] - month = context['params']['month'] - day = context['params']['day'] + extra_context['timeslots'] = TimeSlot.objects.filter(start__range=(this_day, that_day), show__broadcastformat=broadcastformat) + else: + extra_context['timeslots'] = TimeSlot.objects.filter(start__range=(this_day, that_day)) - # start the day at 6 - this_day = datetime.strptime('%s__%s__%s__06__00' % (year, month, day), '%Y__%m__%d__%H__%M') - that_day = this_day+timedelta(days=1) + return simple.direct_to_template(request, extra_context=extra_context, template='program/day_schedule.html') - context['day'] = this_day - context['broadcastformats'] = BroadcastFormat.objects.all() - context['recommendations'] = Note.objects.filter(status=1, timeslot__start__range=(this_day, that_day)) +def current_show(request): + current = TimeSlot.objects.get_or_create_current() + next = current.get_next_by_start() + after_next = next.get_next_by_start() - if 'broadcastformat' in self.request.GET: - broadcastformat = get_object_or_404(BroadcastFormat, slug=self.request.GET['broadcastformat']) + extra_context = dict(current=current, next=next, after_next=after_next) - context['timeslots'] = TimeSlot.objects.filter(start__range=(this_day, that_day), show__broadcastformat=broadcastformat) - else: - context['timeslots'] = TimeSlot.objects.filter(start__range=(this_day, that_day)) + return simple.direct_to_template(request, template='program/current_box.html', extra_context=extra_context) - return context - -class CurrentShowView(TemplateView): - template_name = 'program/current_box.html' - - def get_context_data(self, **kwargs): - context = super(CurrentShowView, self).get_context_data(**kwargs) - - context['current'] = TimeSlot.objects.get_or_create_current() - context['next'] = TimeSlot.objects.get_or_create_current().get_next_by_start() - context['after_next'] = TimeSlot.objects.get_or_create_current().get_next_by_start().get_next_by_start() - - return context - -class WeekScheduleView(TemplateView): - template_name = 'program/week_schedule.html' - - def get_context_data(self, **kwargs): - context = super(WeekScheduleView, self).get_context_data(**kwargs) - - year = context['params']['year'] - week = context['params']['week'] - - # start the day at 6 - monday = datetime.strptime('%s__%s__1__06__00' % (year, week), '%Y__%W__%w__%H__%M') - - tuesday = monday+timedelta(days=1) - wednesday = monday+timedelta(days=2) - thursday = monday+timedelta(days=3) - friday = monday+timedelta(days=4) - saturday = monday+timedelta(days=5) - sunday = monday+timedelta(days=6) - next_monday = monday+timedelta(days=7) - - context['monday'] = monday - context['tuesday'] = tuesday - context['wednesday'] = wednesday - context['thursday'] = thursday - context['friday'] = friday - context['saturday'] = saturday - context['sunday'] = sunday - - context['monday_timeslots'] = TimeSlot.objects.filter(start__range=(monday, tuesday)) - context['tuesday_timeslots'] = TimeSlot.objects.filter(start__range=(tuesday, wednesday)) - context['wednesday_timeslots'] = TimeSlot.objects.filter(start__range=(wednesday, thursday)) - context['thursday_timeslots'] = TimeSlot.objects.filter(start__range=(thursday, friday)) - context['friday_timeslots'] = TimeSlot.objects.filter(start__range=(friday, saturday)) - context['saturday_timeslots'] = TimeSlot.objects.filter(start__range=(saturday, sunday)) - context['sunday_timeslots'] = TimeSlot.objects.filter(start__range=(sunday, next_monday)) - - return context +def week_schedule(request, year, week): + monday = datetime.strptime('%s__%s__1__06__00' % (year, week), '%Y__%W__%w__%H__%M') + tuesday = monday+timedelta(days=1) + wednesday = monday+timedelta(days=2) + thursday = monday+timedelta(days=3) + friday = monday+timedelta(days=4) + saturday = monday+timedelta(days=5) + sunday = monday+timedelta(days=6) + next_monday = monday+timedelta(days=7) + + extra_context = dict(monday=monday, tuesday=tuesday, wednesday=wednesday, thursday=thursday, friday=friday, saturday=saturday, sunday=sunday) + + extra_context['monday_timeslots'] = TimeSlot.objects.filter(start__range=(monday, tuesday)) + extra_context['tuesday_timeslots'] = TimeSlot.objects.filter(start__range=(tuesday, wednesday)) + extra_context['wednesday_timeslots'] = TimeSlot.objects.filter(start__range=(wednesday, thursday)) + extra_context['thursday_timeslots'] = TimeSlot.objects.filter(start__range=(thursday, friday)) + extra_context['friday_timeslots'] = TimeSlot.objects.filter(start__range=(friday, saturday)) + extra_context['saturday_timeslots'] = TimeSlot.objects.filter(start__range=(saturday, sunday)) + extra_context['sunday_timeslots'] = TimeSlot.objects.filter(start__range=(sunday, next_monday)) + + return simple.direct_to_template(request, template='program/week_schedule.html', extra_context=extra_context) -- cgit v0.10.2