diff options
author | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2016-01-06 19:12:21 (GMT) |
---|---|---|
committer | Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com> | 2016-01-06 19:12:21 (GMT) |
commit | 7f505ce017f7e28e4e3e7f55f26c425b1d1375a7 (patch) | |
tree | 26cb117a585419dd6f3800adeeab294f1f3bcf7d /program | |
parent | 7a172c49277a17008f4f6c3303501b86f6df2118 (diff) |
moved to class based generic views
Diffstat (limited to 'program')
-rw-r--r-- | program/urls.py | 45 | ||||
-rw-r--r-- | program/views.py | 240 |
2 files changed, 164 insertions, 121 deletions
diff --git a/program/urls.py b/program/urls.py index d02ee31..49d0488 100644 --- a/program/urls.py +++ b/program/urls.py @@ -1,30 +1,43 @@ from django.conf import settings from django.conf.urls import patterns, url +from django.db.models import Q from django.views.decorators.cache import cache_page +from django.views.generic.detail import DetailView +from django.views.generic.list import ListView -from views import current_show, day_schedule, recommendations, show_list, show_detail, timeslot_detail, week_schedule, styles, host_list, host_detail +from views import ShowListView, CurrentShowBoxView, RecommendationsListView, RecommendationsBoxView, DayScheduleView, StylesView, WeekScheduleView +from models import Host, Show, TimeSlot import os +from datetime import date, datetime, timedelta PROGRAM_SITE_MEDIA = os.path.join(os.path.dirname(__file__), '../site_media') -recommendations_dict = {'template_name': 'boxes/recommendations.html'} +now = datetime.now() +end = now + timedelta(weeks=1) -urlpatterns = patterns('', - url(r'^today/?$', day_schedule), - url(r'^week/?$', week_schedule), - url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/?$', day_schedule), - url(r'^(?P<year>\d{4})/(?P<week>\d{1,2})/?$', week_schedule), - url(r'^current_box/?$', cache_page(60)(current_show)), - url(r'^hosts/?$', host_list), - url(r'^hosts/(?P<object_id>\d+)/?$', host_detail, name='host-detail'), - url(r'^tips/?$', recommendations), - url(r'^tips_box/?$', recommendations, recommendations_dict), - url(r'^shows/?$', show_list), - url(r'^shows/(?P<slug>[\w-]+)/?$', show_detail, name='show-detail'), - url(r'^(?P<object_id>\d+)/?$', timeslot_detail, name='timeslot-detail'), - url(r'^styles.css$', styles)) +hosts = Host.objects.filter(Q(shows__programslots__until__gte=date.today()) | + Q(always_visible=True)).distinct() +shows = Show.objects.filter(programslots__until__gt=date.today()).exclude(id=1).distinct() +timeslots = TimeSlot.objects.all() +recommendations = TimeSlot.objects.filter(Q(note__isnull=False, note__status=1, start__range=(now, end)) | + Q(show__broadcastformat__slug='sondersendung', start__range=(now, end))).order_by('start')[:20] +urlpatterns = patterns('', + url(r'^today/?$', DayScheduleView.as_view()), + url(r'^week/?$', WeekScheduleView.as_view()), + url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/?$', DayScheduleView.as_view()), + url(r'^(?P<year>\d{4})/(?P<week>\d{1,2})/?$', WeekScheduleView.as_view()), + url(r'^current_box/?$', cache_page(60)(CurrentShowBoxView.as_view())), + url(r'^hosts/?$', ListView.as_view(context_object_name='host_list', queryset=hosts, template_name='host_list.html')), + url(r'^hosts/(?P<pk>\d+)/?$', DetailView.as_view(context_object_name='host', queryset=hosts, template_name='host_detail.html'), name='host-detail'), + url(r'^tips/?$', RecommendationsListView.as_view()), + url(r'^tips_box/?$', RecommendationsBoxView.as_view()), + url(r'^shows/?$', ShowListView.as_view(context_object_name='show_list', queryset=shows, template_name='show_list.html')), + url(r'^shows/(?P<slug>[\w-]+)/?$', DetailView.as_view(queryset=shows, template_name='show_detail.html'), name='show-detail'), + url(r'^(?P<pk>\d+)/?$', DetailView.as_view(queryset=timeslots, template_name='timeslot_detail.html'), name='timeslot-detail'), + url(r'^styles.css$', StylesView.as_view()) + ) if settings.DEBUG: urlpatterns += patterns('', url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': PROGRAM_SITE_MEDIA})) diff --git a/program/views.py b/program/views.py index cba3a25..7e70175 100644 --- a/program/views.py +++ b/program/views.py @@ -1,115 +1,149 @@ -from datetime import date, datetime, time, timedelta import json +from datetime import date, datetime, time, timedelta -from django.views.generic.base import TemplateView -from django.views.generic.detail import DetailView -from django.views.generic.list import ListView -from django.shortcuts import get_object_or_404 from django.db.models import Q from django.http import HttpResponse +from django.shortcuts import get_object_or_404 +from django.views.generic.base import TemplateView +from django.views.generic.list import ListView -from models import BroadcastFormat, MusicFocus, Note, Show, ShowInformation, ShowTopic, TimeSlot, Host - - -def host_list(request): - queryset = Host.objects.filter(Q(shows__programslots__until__gte=date.today()) | - Q(always_visible=True)).distinct() - - return ListView.as_view(request, queryset=queryset, template_name='host_list.html') - - -def host_detail(request): - queryset = Host.objects.filter(Q(shows__programslots__until__gte=date.today()) | - Q(always_visible=True)).distinct() - - return DetailView.as_view(request, queryset=queryset, template_name='host_detail.html') - - -def show_list(request): - queryset = Show.objects.filter(programslots__until__gt=date.today()).exclude(id=1).distinct() - - if 'broadcastformat' in request.GET: - broadcastformat = get_object_or_404(BroadcastFormat, slug=request.GET['broadcastformat']) - queryset = queryset.filter(broadcastformat=broadcastformat) - elif 'musicfocus' in request.GET: - musicfocus = get_object_or_404(MusicFocus, slug=request.GET['musicfocus']) - queryset = queryset.filter(musicfocus=musicfocus) - elif 'showinformation' in request.GET: - showinformation = get_object_or_404(ShowInformation, slug=request.GET['showinformation']) - queryset = queryset.filter(showinformation=showinformation) - elif 'showtopic' in request.GET: - showtopic = get_object_or_404(ShowTopic, slug=request.GET['showtopic']) - queryset = queryset.filter(showtopic=showtopic) - - return ListView.as_view(request, queryset=queryset, template_object_name='show', template_name='show_list.html') - +from models import BroadcastFormat, MusicFocus, Note, Show, ShowInformation, ShowTopic, TimeSlot +from program.utils import tofirstdayinisoweek -def show_detail(request): - queryset = Show.objects.filter(programslots__until__gt=date.today()).exclude(id=1).distinct() - return DetailView.as_view(request, queryset=queryset, template_name='show_detail.html',) +class ShowListView(ListView): + def get_queryset(self): + queryset = Show.objects.filter(programslots__until__gt=date.today()).exclude(id=1).distinct() + if 'broadcastformat' in self.request.GET: + broadcastformat = get_object_or_404(BroadcastFormat, slug=self.request.GET['broadcastformat']) + queryset = queryset.filter(broadcastformat=broadcastformat) + elif 'musicfocus' in self.request.GET: + musicfocus = get_object_or_404(MusicFocus, slug=self.request.GET['musicfocus']) + queryset = queryset.filter(musicfocus=musicfocus) + elif 'showinformation' in self.request.GET: + showinformation = get_object_or_404(ShowInformation, slug=self.request.GET['showinformation']) + queryset = queryset.filter(showinformation=showinformation) + elif 'showtopic' in self.request.GET: + showtopic = get_object_or_404(ShowTopic, slug=self.request.GET['showtopic']) + queryset = queryset.filter(showtopic=showtopic) -def timeslot_detail(request): - queryset = TimeSlot.objects.all() + return queryset - return DetailView.as_view(request, queryset=queryset, template_name='timeslot_detail.html') +class RecommendationsListView(ListView): + context_object_name = 'recommendation_list' + template_name = 'recommendation_list.html' -def recommendations(request, template_name='recommendations.html'): now = datetime.now() end = now + timedelta(weeks=1) queryset = TimeSlot.objects.filter(Q(note__isnull=False, note__status=1, start__range=(now, end)) | Q(show__broadcastformat__slug='sondersendung', start__range=(now, end))).order_by('start')[:20] - return DetailView.as_view(request, queryset=queryset, template_name=template_name, template_object_name='recommendation') - - -def day_schedule(request, year=None, month=None, day=None): - if year is None and month is None and day is None: - today = datetime.combine(date.today(), time(6, 0)) - else: - today = datetime.strptime('%s__%s__%s__06__00' % (year, month, day), '%Y__%m__%d__%H__%M') - tomorrow = today + timedelta(days=1) +class RecommendationsBoxView(RecommendationsListView): + template_name='boxes/recommendation.html' - recommendations = Note.objects.filter(status=1, timeslot__start__range=(today, tomorrow)) - - default_show = Show.objects.get(pk=1) - - extra_context = dict(day=today, recommendations=recommendations, default_show=default_show) - - timeslots = TimeSlot.objects.get_day_timeslots(today) - - if 'broadcastformat' in request.GET: - broadcastformat = get_object_or_404(BroadcastFormat, slug=request.GET['broadcastformat']) - extra_context['timeslots'] = timeslots.filter(show__broadcastformat=broadcastformat) - elif 'musicfocus' in request.GET: - musicfocus = get_object_or_404(MusicFocus, slug=request.GET['musicfocus']) - extra_context['timeslots'] = timeslots.filter(show__musicfocus=musicfocus) - elif 'showinformation' in request.GET: - showinformation = get_object_or_404(ShowInformation, slug=request.GET['showinformation']) - extra_context['timeslots'] = timeslots.filter(show__showinformation=showinformation) - elif 'showtopic' in request.GET: - showtopic = get_object_or_404(ShowTopic, slug=request.GET['showtopic']) - extra_context['showtopic'] = timeslots.filter(show__showtopic=showtopic) - else: - extra_context['timeslots'] = timeslots - return TemplateView.as_view(request, extra_context=extra_context, template='day_schedule.html') +class DayScheduleView(TemplateView): + template_name = 'day_schedule.html' + def get_context_data(self, **kwargs): + year = self.kwargs.get('year', None) + month = self.kwargs.get('month', None) + day = self.kwargs.get('day', None) -def current_show(request): - current = TimeSlot.objects.get_or_create_current() - previous = current.get_previous_by_start() - next = current.get_next_by_start() - after_next = next.get_next_by_start() - - extra_context = dict(current=current, previous=previous, next=next, after_next=after_next) - - return TemplateView.as_view(request, template='boxes/current.html', extra_context=extra_context) - + if year is None and month is None and day is None: + today = datetime.combine(date.today(), time(6, 0)) + else: + today = datetime.strptime('%s__%s__%s__06__00' % (year, month, day), '%Y__%m__%d__%H__%M') + + tomorrow = today + timedelta(days=1) + + context = super(DayScheduleView, self).get_context_data(**kwargs) + context['day'] = today + context['recommendations'] = Note.objects.filter(status=1, timeslot__start__range=(today, tomorrow)) + context['default_show'] = Show.objects.get(pk=1) + + timeslots = TimeSlot.objects.get_day_timeslots(today) + + if 'broadcastformat' in self.request.GET: + broadcastformat = get_object_or_404(BroadcastFormat, slug=self.request.GET['broadcastformat']) + context['timeslots'] = timeslots.filter(show__broadcastformat=broadcastformat) + elif 'musicfocus' in self.request.GET: + musicfocus = get_object_or_404(MusicFocus, slug=self.request.GET['musicfocus']) + context['timeslots'] = timeslots.filter(show__musicfocus=musicfocus) + elif 'showinformation' in self.request.GET: + showinformation = get_object_or_404(ShowInformation, slug=self.request.GET['showinformation']) + context['timeslots'] = timeslots.filter(show__showinformation=showinformation) + elif 'showtopic' in self.request.GET: + showtopic = get_object_or_404(ShowTopic, slug=self.request.GET['showtopic']) + context['showtopic'] = timeslots.filter(show__showtopic=showtopic) + else: + context['timeslots'] = timeslots + return context + + +class CurrentShowBoxView(TemplateView): + context_object_name = 'recommendation_list' + template_name = 'boxes/current.html' + + def get_context_data(self, **kwargs): + current_timeslot = TimeSlot.objects.get_or_create_current() + previuos_timeslot = current_timeslot.get_previous_by_start() + next_timeslot = current_timeslot.get_next_by_start() + after_next_timeslot = next_timeslot.get_next_by_start() + + context = super(CurrentShowBoxView, self).get_context_data(**kwargs) + context['current_timeslot'] = current_timeslot + context['previuos_timeslot'] = previuos_timeslot + context['next_timeslot'] = next_timeslot + context['after_next_timeslot'] = after_next_timeslot + return context + + +class WeekScheduleView(TemplateView): + template_name = 'week_schedule.html' + + def get_context_data(self, **kwargs): + year = self.kwargs.get('year', None) + week = self.kwargs.get('week', None) + + if year is None and week is None: + year, week = datetime.now().strftime('%G__%V').split('__') + + monday = tofirstdayinisoweek(int(year), int(week)) + 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) + + context = super(WeekScheduleView, self).get_context_data() + context['monday'] = monday + context['tuesday'] = tuesday + context['wednesday'] = wednesday + context['thursday'] = thursday + context['friday'] = friday + context['saturday'] = saturday + context['sunday'] = sunday + context['default_show'] = Show.objects.get(pk=1) + context['monday_timeslots'] = TimeSlot.objects.get_day_timeslots(monday) + context['tuesday_timeslots'] = TimeSlot.objects.get_day_timeslots(tuesday) + context['wednesday_timeslots'] = TimeSlot.objects.get_day_timeslots(wednesday) + context['thursday_timeslots'] = TimeSlot.objects.get_day_timeslots(thursday) + context['friday_timeslots'] = TimeSlot.objects.get_day_timeslots(friday) + context['saturday_timeslots'] = TimeSlot.objects.get_day_timeslots(saturday) + context['sunday_timeslots'] = TimeSlot.objects.get_day_timeslots(sunday) + context['last_w'] = datetime.strftime(monday - timedelta(days=7), '%G/%V') + context['cur_w'] = datetime.strftime(monday, '%G/%V') + context['next_w1'] = datetime.strftime(monday + timedelta(days=7), '%G/%V') + context['next_w2'] = datetime.strftime(monday + timedelta(days=14), '%G/%V') + context['next_w3'] = datetime.strftime(monday + timedelta(days=21), '%G/%V') + context['next_w4'] = datetime.strftime(monday + timedelta(days=28), '%G/%V') + return context def week_schedule(request, year=None, week=None): if year is None and week is None: @@ -143,17 +177,21 @@ def week_schedule(request, year=None, week=None): extra_context['next_w3'] = datetime.strftime(monday + timedelta(days=21), '%G/%V') extra_context['next_w4'] = datetime.strftime(monday + timedelta(days=28), '%G/%V') - return TemplateView.as_view(request, template='week_schedule.html', extra_context=extra_context) + return TemplateView.as_view(template='week_schedule.html', extra_context=extra_context)(request) + -def styles(request): - extra_context = dict() - extra_context['broadcastformats'] = BroadcastFormat.objects.filter(enabled=True) - extra_context['musicfocus'] = MusicFocus.objects.all() - extra_context['showinformation'] = ShowInformation.objects.all() - extra_context['showtopic'] = ShowTopic.objects.all() +class StylesView(TemplateView): + template_name = 'styles.css' + content_type = 'text/css' - return TemplateView.as_view(request, template='styles.css', mimetype='text/css', extra_context=extra_context) + def get_context_data(self, **kwargs): + context = super(StylesView, self).get_context_data(**kwargs) + context['broadcastformats'] = BroadcastFormat.objects.filter(enabled=True) + context['musicfocus'] = MusicFocus.objects.all() + context['showinformation'] = ShowInformation.objects.all() + context['showtopic'] = ShowTopic.objects.all() + return context def json_day_schedule(request, year=None, month=None, day=None): @@ -173,11 +211,3 @@ def json_day_schedule(request, year=None, month=None, day=None): schedule.append((ts.start.strftime('%H:%M:%S'), ts.programslot.show.name, -1)) return HttpResponse(json.dumps(schedule, ensure_ascii=False, encoding='utf8').encode('utf8'), content_type="application/json; charset=utf-8") - - -def tofirstdayinisoweek(year, week): - # http://stackoverflow.com/questions/5882405/get-date-from-iso-week-number-in-python - ret = datetime.strptime('%04d-%02d-1' % (year, week), '%Y-%W-%w') - if date(year, 1, 4).isoweekday() > 4: - ret -= timedelta(days=7) - return ret |