From c9bbb322f456c3f12ef896384139839d3019bd82 Mon Sep 17 00:00:00 2001 From: Ernesto Rico-Schmidt Date: Mon, 25 Apr 2011 15:01:00 +0200 Subject: simplified day and week views, use Program slot manager now, get_or_create_current is more robust now. diff --git a/program/models.py b/program/models.py index 5f0f835..08ca76b 100644 --- a/program/models.py +++ b/program/models.py @@ -1,9 +1,9 @@ from django.contrib.auth.models import User -from django.core.exceptions import ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.db import models from django.utils.translation import ugettext_lazy as _ -from datetime import date, datetime +from datetime import date, datetime, time, timedelta from dateutil.relativedelta import relativedelta from dateutil.rrule import rrule @@ -234,10 +234,20 @@ class TimeSlotManager(models.Manager): until, tend = next.start.date(), next.start.time() new_programslot = ProgramSlot(rrule=once, byweekday=today, show=default, dstart=dstart, tstart=tstart, tend=tend, until=until) - new_programslot.save() + try: + new_programslot.validate_unique() + new_programslot.save() + except ValidationError: + pass return new_programslot.timeslots.all()[0] + def get_day_timeslots(self, day): + today = datetime.combine(day, time(6,0)) + tomorrow = today + timedelta(days=1) + + return TimeSlot.objects.filter(start__range=(today, tomorrow)) + class TimeSlot(models.Model): programslot = models.ForeignKey(ProgramSlot, related_name='timeslots', verbose_name=_("Program slot")) start = models.DateTimeField(_("Start time"), unique=True) @@ -245,7 +255,7 @@ class TimeSlot(models.Model): show = models.ForeignKey(Show, editable=False) objects = TimeSlotManager() - + class Meta: ordering = ('start', 'end') verbose_name = _("Time slot") diff --git a/program/views.py b/program/views.py index 59e8fdf..d3e72f5 100644 --- a/program/views.py +++ b/program/views.py @@ -47,12 +47,14 @@ def day_schedule(request, year=None, month=None, day=None): extra_context = dict(day=today, recommendations=recommendations) + 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'] = TimeSlot.objects.filter(start__range=(today, tomorrow), show__broadcastformat=broadcastformat) + extra_context['timeslots'] = timeslots.filter(show__broadcastformat=broadcastformat) else: - extra_context['timeslots'] = TimeSlot.objects.filter(start__range=(today, tomorrow)) + extra_context['timeslots'] = timeslots return simple.direct_to_template(request, extra_context=extra_context, template='program/day_schedule.html') @@ -77,16 +79,15 @@ def week_schedule(request, year=None, week=None): 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)) + extra_context['monday_timeslots'] = TimeSlot.objects.get_day_timeslots(monday) + extra_context['tuesday_timeslots'] = TimeSlot.objects.get_day_timeslots(tuesday) + extra_context['wednesday_timeslots'] = TimeSlot.objects.get_day_timeslots(wednesday) + extra_context['thursday_timeslots'] = TimeSlot.objects.get_day_timeslots(thursday) + extra_context['friday_timeslots'] = TimeSlot.objects.get_day_timeslots(friday) + extra_context['saturday_timeslots'] = TimeSlot.objects.get_day_timeslots(saturday) + extra_context['sunday_timeslots'] = TimeSlot.objects.get_day_timeslots(sunday) return simple.direct_to_template(request, template='program/week_schedule.html', extra_context=extra_context) -- cgit v0.10.2