split program update and current json

This commit is contained in:
j 2012-09-18 12:20:18 +02:00
parent 103f3396d6
commit 9ee71ce6dd
2 changed files with 14 additions and 6 deletions

View file

@ -21,15 +21,16 @@ class Channel(models.Model):
def __unicode__(self): def __unicode__(self):
return u"%s %s" % (self.list or 'All', self.run) return u"%s %s" % (self.list or 'All', self.run)
def json(self, user): def update_program(self, now=None):
now = datetime.now() if not now:
now = datetime.now()
cansee = settings.CONFIG['capabilities']['canSeeItem']['guest'] cansee = settings.CONFIG['capabilities']['canSeeItem']['guest']
if self.list: if self.list:
items = self.list.get_items(self.list.user).filter(rendered=True, level__lte=cansee, sort__duration__gt=0) items = self.list.get_items(self.list.user).filter(rendered=True, level__lte=cansee, sort__duration__gt=0)
else: else:
items = Item.objects.filter(rendered=True, level__lte=cansee, sort__duration__gt=0) items = Item.objects.filter(rendered=True, level__lte=cansee, sort__duration__gt=0)
if items.count() == 0: if items.count() == 0:
return {} return None
program = self.program.order_by('-start') program = self.program.order_by('-start')
changed = False changed = False
@ -59,7 +60,15 @@ class Channel(models.Model):
program = self.program.order_by('-start') program = self.program.order_by('-start')
if changed: if changed:
self.save() self.save()
return program[0].json(user, now) return program[0]
def json(self, user):
now = datetime.now()
program = self.update_program(now)
if not program:
return {}
else:
return program.json(user, now)
class Program(models.Model): class Program(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)

View file

@ -9,6 +9,5 @@ import models
@periodic_task(run_every=timedelta(days=1), queue='encoding') @periodic_task(run_every=timedelta(days=1), queue='encoding')
def update_program(**kwargs): def update_program(**kwargs):
user = User.objects.all()[0]
for c in models.Channel.objects.all(): for c in models.Channel.objects.all():
c.json(user) c.update_program()