From b2b2649420ad133678b6e0c893dc327ba51e4585 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 5 Feb 2014 13:21:17 +0000 Subject: [PATCH] make smart edits return clips inline --- pandora/clip/models.py | 46 +++++++++++++++++++++++++++++++++++++++ pandora/edit/models.py | 49 +++++++++++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 4f261dca..bbdce230 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -5,6 +5,8 @@ from __future__ import division, with_statement from django.db import models from django.conf import settings +import ox + from archive import extract import managers @@ -76,6 +78,10 @@ class MetaClip: annotations = annotations.filter(qs) j['annotations'] = [a.json(keys=['value', 'id', 'layer']) 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: if key not in self.clip_keys and key not in j: value = self.item.get(key) or self.item.json.get(key) @@ -85,6 +91,46 @@ class MetaClip: j[key] = value 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 def get_or_create(cls, item, start, end): start = float(start) diff --git a/pandora/edit/models.py b/pandora/edit/models.py index b5c9eb66..1fcc429a 100644 --- a/pandora/edit/models.py +++ b/pandora/edit/models.py @@ -18,6 +18,8 @@ from django.contrib.auth.models import User from annotation.models import Annotation from item.models import Item +from item.utils import get_by_id +import clip.models from archive import extract @@ -158,6 +160,10 @@ class Edit(models.Model): self.description = ox.sanitize_html(data['description']) elif key == '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: pos, created = Position.objects.get_or_create(edit=self, user=user) @@ -171,7 +177,7 @@ class Edit(models.Model): self.query = {"static":True} self.type = 'static' else: - self.type = 'dynamic' + self.type = 'smart' if self.query.get('static', False): self.query = {} 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) 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): frames = [] @@ -260,10 +292,15 @@ class Edit(models.Model): 'type', 'user', ] - response = {} + response = { + 'type': self.type + } _map = { 'posterFrames': 'poster_frames' } + if 'clips' in keys or 'duration' in keys: + clips = self.get_clips(user) + for key in keys: if key == 'id': response[key] = self.get_id() @@ -273,12 +310,8 @@ class Edit(models.Model): if not self.query.get('static', False): response[key] = self.query elif key == 'clips': - response[key] = [c.json(user) for c in self.clips.all().order_by('index')] + response[key] = clips 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]) elif key == 'editable': response[key] = self.editable(user)