make smart edits return clips inline

This commit is contained in:
j 2014-02-05 13:21:17 +00:00
parent 7a4bab0e84
commit b2b2649420
2 changed files with 87 additions and 8 deletions

View file

@ -5,6 +5,8 @@ from __future__ import division, with_statement
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
import ox
from archive import extract from archive import extract
import managers import managers
@ -76,6 +78,10 @@ class MetaClip:
annotations = annotations.filter(qs) annotations = annotations.filter(qs)
j['annotations'] = [a.json(keys=['value', 'id', 'layer']) j['annotations'] = [a.json(keys=['value', 'id', 'layer'])
for a in annotations] for a in annotations]
if 'layers' in keys:
j['layers'] = self.get_layers()
if 'cuts' in keys:
j['cuts'] = tuple([c for c in self.item.get('cuts') if c > self.start and c < self.end])
for key in keys: for key in keys:
if key not in self.clip_keys and key not in j: if key not in self.clip_keys and key not in j:
value = self.item.get(key) or self.item.json.get(key) value = self.item.get(key) or self.item.json.get(key)
@ -85,6 +91,46 @@ class MetaClip:
j[key] = value j[key] = value
return j return j
def edit_json(self, user=None):
data = {
'id': ox.toAZ(self.id),
}
data['item'] = self.item.itemId
data['in'] = self.start
data['out'] = self.end
data['parts'] = self.item.json['parts']
data['durations'] = self.item.json['durations']
for key in ('title', 'director', 'year', 'videoRatio'):
value = self.item.json.get(key)
if value:
data[key] = value
data['duration'] = data['out'] - data['in']
data['cuts'] = tuple([c for c in self.item.get('cuts') if c > self.start and c < self.end])
data['layers'] = self.get_layers(user)
return data
def get_layers(self, user=None):
from annotation.models import Annotation
start = self.start
end = self.end
item = self.item
layers = {}
for l in settings.CONFIG['layers']:
name = l['id']
ll = layers.setdefault(name, [])
qs = Annotation.objects.filter(layer=name, item=item).order_by(
'start', 'end', 'sortvalue')
if name == 'subtitles':
qs = qs.exclude(value='')
qs = qs.filter(start__lt=end, end__gt=start)
if l.get('private'):
if user and user.is_anonymous():
user = None
qs = qs.filter(user=user)
for a in qs.order_by('start'):
ll.append(a.json(user=user))
return layers
@classmethod @classmethod
def get_or_create(cls, item, start, end): def get_or_create(cls, item, start, end):
start = float(start) start = float(start)

View file

@ -18,6 +18,8 @@ from django.contrib.auth.models import User
from annotation.models import Annotation from annotation.models import Annotation
from item.models import Item from item.models import Item
from item.utils import get_by_id
import clip.models
from archive import extract from archive import extract
@ -158,6 +160,10 @@ class Edit(models.Model):
self.description = ox.sanitize_html(data['description']) self.description = ox.sanitize_html(data['description'])
elif key == 'rightslevel': elif key == 'rightslevel':
self.rightslevel = int(data['rightslevel']) self.rightslevel = int(data['rightslevel'])
if key == 'query' and not data['query']:
setattr(self, key, {"static":True})
elif key == 'query':
setattr(self, key, data[key])
if 'position' in data: if 'position' in data:
pos, created = Position.objects.get_or_create(edit=self, user=user) pos, created = Position.objects.get_or_create(edit=self, user=user)
@ -171,7 +177,7 @@ class Edit(models.Model):
self.query = {"static":True} self.query = {"static":True}
self.type = 'static' self.type = 'static'
else: else:
self.type = 'dynamic' self.type = 'smart'
if self.query.get('static', False): if self.query.get('static', False):
self.query = {} self.query = {}
if 'posterFrames' in data: if 'posterFrames' in data:
@ -185,7 +191,33 @@ class Edit(models.Model):
return os.path.join('edits', h[:2], h[2:4], h[4:6], h[6:], name) return os.path.join('edits', h[:2], h[2:4], h[4:6], h[6:], name)
def get_items(self, user=None): def get_items(self, user=None):
return Item.objects.filter(editclips__id__in=self.clips.all()).distinct() if self.type == 'static':
return Item.objects.filter(editclip__id__in=self.clips.all()).distinct()
else:
return Item.objects.find({'query': self.query}, user)
def get_clips(self, user=None):
if self.type == 'static':
clips = [c.json(user) for c in self.clips.all().order_by('index')]
else:
#FIXME: limit results!!
clips = [c.edit_json(user) for c in clip.models.Clip.objects.find(self.clip_query(), user)]
index = 0
for c in clips:
c['index'] = index
index += 1
return clips
def clip_query(self):
query = {
'conditions': [],
'operator': self.query['operator']
}
for condition in self.query['conditions']:
if condition['key'] == 'annotations' or \
get_by_id(settings.CONFIG['layers'], condition['key']):
query['conditions'].append(condition)
return {'query': query}
def update_icon(self): def update_icon(self):
frames = [] frames = []
@ -260,10 +292,15 @@ class Edit(models.Model):
'type', 'type',
'user', 'user',
] ]
response = {} response = {
'type': self.type
}
_map = { _map = {
'posterFrames': 'poster_frames' 'posterFrames': 'poster_frames'
} }
if 'clips' in keys or 'duration' in keys:
clips = self.get_clips(user)
for key in keys: for key in keys:
if key == 'id': if key == 'id':
response[key] = self.get_id() response[key] = self.get_id()
@ -273,12 +310,8 @@ class Edit(models.Model):
if not self.query.get('static', False): if not self.query.get('static', False):
response[key] = self.query response[key] = self.query
elif key == 'clips': elif key == 'clips':
response[key] = [c.json(user) for c in self.clips.all().order_by('index')] response[key] = clips
elif key == 'duration': elif key == 'duration':
if 'clips' in response:
clips = response['clips']
else:
clips = [c.json(user) for c in self.clips.all().order_by('index')]
response[key] = sum([c['duration'] for c in clips]) response[key] = sum([c['duration'] for c in clips])
elif key == 'editable': elif key == 'editable':
response[key] = self.editable(user) response[key] = self.editable(user)