pandora/pandora/tv/models.py

90 lines
3.3 KiB
Python
Raw Normal View History

2011-12-19 19:41:37 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from datetime import datetime, timedelta
from random import randint
from django.db import models
2011-12-19 21:12:23 +00:00
from django.db.models import Max
2011-12-23 10:18:07 +00:00
from django.conf import settings
2011-12-19 19:41:37 +00:00
2011-12-19 21:54:53 +00:00
from item.models import Item
2011-12-19 19:41:37 +00:00
class Channel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
2011-12-19 21:12:23 +00:00
run = models.IntegerField(default=0)
2011-12-19 21:54:53 +00:00
list = models.ForeignKey('itemlist.List', related_name='channel', null=True, unique=True, blank=True)
2011-12-19 19:41:37 +00:00
2011-12-19 21:12:23 +00:00
def __unicode__(self):
2011-12-19 22:02:10 +00:00
return u"%s %s" % (self.list or 'All', self.run)
2011-12-19 21:12:23 +00:00
2011-12-19 19:41:37 +00:00
def json(self, user):
now = datetime.now()
2011-12-23 10:18:07 +00:00
cansee = settings.CONFIG['capabilities']['canSeeItem']['guest']
2011-12-19 21:54:53 +00:00
if self.list:
2012-09-18 09:35:16 +00:00
items = self.list.get_items(self.list.user).filter(rendered=True, level__lte=cansee, sort__duration__gt=0)
2011-12-19 21:54:53 +00:00
else:
2012-09-18 09:35:16 +00:00
items = Item.objects.filter(rendered=True, level__lte=cansee, sort__duration__gt=0)
2011-12-19 19:41:37 +00:00
if items.count() == 0:
2011-12-19 21:12:23 +00:00
return {}
2011-12-19 19:41:37 +00:00
program = self.program.order_by('-start')
2012-03-20 16:23:13 +00:00
changed = False
2011-12-19 21:12:23 +00:00
while program.count() < 1 or program[0].end < now:
2011-12-19 19:41:37 +00:00
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
2012-03-20 16:23:13 +00:00
changed = True
2011-12-19 19:41:37 +00:00
not_played = items
not_played_count = not_played.count()
2012-03-20 16:23:13 +00:00
if not_played_count > 1:
not_played = not_played.exclude(id=program[0].id)
not_played_count = not_played.count()
2011-12-19 19:41:37 +00:00
item = not_played[randint(0, not_played_count-1)]
if program.count() > 0:
2011-12-19 21:12:23 +00:00
start = program.aggregate(Max('end'))['end__max']
2011-12-19 19:41:37 +00:00
else:
start = now
p = Program()
2011-12-19 21:12:23 +00:00
p.item = item
p.run = self.run
p.start = start
2012-09-18 09:35:16 +00:00
p.end = start + timedelta(seconds=item.sort.duration)
2011-12-19 19:41:37 +00:00
p.channel = self
p.save()
program = self.program.order_by('-start')
2012-03-20 16:23:13 +00:00
if changed:
self.save()
2011-12-19 21:12:23 +00:00
return program[0].json(user, now)
2011-12-19 19:41:37 +00:00
class Program(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
run = models.IntegerField(default=0)
start = models.DateTimeField()
end = models.DateTimeField()
item = models.ForeignKey('item.Item', related_name='program')
channel = models.ForeignKey(Channel, related_name='program')
def __unicode__(self):
return u"%s %s" % (self.item, self.start)
def json(self, user, current=False):
item_json = self.item.get_json()
r = {
'item': self.item.itemId,
}
2011-12-19 21:12:23 +00:00
for key in ('title', 'director', 'year', 'durations', 'parts', 'rightslevel'):
r[key] = item_json.get(key, '')
2011-12-19 19:41:37 +00:00
r['layers'] = self.item.get_layers(user)
if current:
2011-12-19 21:12:23 +00:00
#requires python2.7
#r['position'] = (current - self.start).total_seconds()
td = current - self.start
r['position'] = td.seconds + td.days * 24 * 3600 + float(td.microseconds)/10**6
2011-12-19 19:41:37 +00:00
return r