From 37117407b0737bf36d2a8583957bae15829be9d7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 20 Mar 2012 17:23:13 +0100 Subject: [PATCH] save channel only once --- pandora/tv/models.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pandora/tv/models.py b/pandora/tv/models.py index 62a2c941..551e398f 100644 --- a/pandora/tv/models.py +++ b/pandora/tv/models.py @@ -32,16 +32,18 @@ class Channel(models.Model): return {} program = self.program.order_by('-start') + changed = False while program.count() < 1 or program[0].end < now: not_played = items.exclude(program__in=self.program.filter(run=self.run)) not_played_count = not_played.count() if not_played_count == 0: self.run += 1 - self.save() + changed = True not_played = items - if not_played.count() > 1: - not_played = not_played.exclude(id=program[0].id) not_played_count = not_played.count() + if not_played_count > 1: + not_played = not_played.exclude(id=program[0].id) + not_played_count = not_played.count() item = not_played[randint(0, not_played_count-1)] if program.count() > 0: start = program.aggregate(Max('end'))['end__max'] @@ -55,6 +57,8 @@ class Channel(models.Model): p.channel = self p.save() program = self.program.order_by('-start') + if changed: + self.save() return program[0].json(user, now) class Program(models.Model):