add channel for all items

This commit is contained in:
j 2011-12-20 03:24:53 +05:30
parent d20923703c
commit 000ad14083
2 changed files with 17 additions and 7 deletions

View file

@ -7,20 +7,25 @@ from random import randint
from django.db import models from django.db import models
from django.db.models import Max from django.db.models import Max
from item.models import Item
class Channel(models.Model): class Channel(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True) modified = models.DateTimeField(auto_now=True)
run = models.IntegerField(default=0) run = models.IntegerField(default=0)
list = models.ForeignKey('itemlist.List', related_name='channel') list = models.ForeignKey('itemlist.List', related_name='channel', null=True, unique=True, blank=True)
def __unicode__(self): def __unicode__(self):
return u"%s %s" % (self.list, self.run) return u"%s %s" % (self.list, self.run)
def json(self, user): def json(self, user):
now = datetime.now() now = datetime.now()
items = self.list.get_items().filter(rendered=True) if self.list:
items = self.list.get_items().filter(rendered=True)
else:
items = Item.objects.filter(rendered=True)
if items.count() == 0: if items.count() == 0:
return {} return {}

View file

@ -11,12 +11,17 @@ from api.actions import actions
def tv(request): def tv(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
list = get_list_or_404_json(data['list']) if 'list' in data and data['list']:
if list.accessible(request.user): list = get_list_or_404_json(data['list'])
channel, created = models.Channel.objects.get_or_create(list=list) if list.accessible(request.user):
channel, created = models.Channel.objects.get_or_create(list=list)
response = json_response(status=200, text='created')
response['data'] = channel.json(request.user)
else:
response = json_response(status=404, text='list not found')
else:
channel, created = models.Channel.objects.get_or_create(list=None)
response = json_response(status=200, text='created') response = json_response(status=200, text='created')
response['data'] = channel.json(request.user) response['data'] = channel.json(request.user)
else:
response = json_response(status=404, text='list not found')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(tv, cache=False) actions.register(tv, cache=False)