This commit is contained in:
j 2011-12-20 01:11:37 +05:30
parent 277a0991b0
commit 110debe26d
4 changed files with 98 additions and 0 deletions

View file

@ -110,6 +110,7 @@ INSTALLED_APPS = (
'user',
'api',
'urlalias',
'tv',
)
# Log errors into db

0
pandora/tv/__init__.py Normal file
View file

76
pandora/tv/models.py Normal file
View file

@ -0,0 +1,76 @@
# -*- 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
class Channel(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
run = models.IntegerField(default=0)
list = models.ForeignKey('itemlist.List', related_name='channel')
def json(self, user):
now = datetime.now()
items = self.list.get_items().filter(rendered=True)
if items.count() == 0:
return
program = self.program.order_by('-start')
while program.count() < 2 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()
not_played = items
not_played_count = not_played.count()
item = not_played[randint(0, not_played_count-1)]
if program.count() > 0:
start = program[0].end
else:
start = now
end = start + timedelta(seconds=item.get_json()['duration'])
p = Program()
p.item=item
p.run=self.run
p.start=start
p.end=end
p.channel = self
p.save()
program = self.program.order_by('-start')
print program.count(), now, p.start, p.end
current = program[1]
return {
'current': current.json(user, now),
'next': program[0].json(user)
}
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,
'durations': item_json['durations'],
'parts': item_json['parts'],
}
r['layers'] = self.item.get_layers(user)
if current:
r['currentTime'] = (current - self.start).total_seconds()
return r

21
pandora/tv/views.py Normal file
View file

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import models
from ox.utils import json
from ox.django.decorators import login_required_json
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
from itemlist.views import get_list_or_404_json
from api.actions import actions
@login_required_json
def tv(request):
data = json.loads(request.POST['data'])
list = get_list_or_404_json(data['list'])
channel, created = models.Channel.objects.get_or_create(list=list)
response = json_response(status=200, text='created')
response['data'] = channel.json(request.user)
return render_to_json_response(response)
actions.register(tv, cache=False)