2011-10-02 18:16:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division, with_statement
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from archive import extract
|
|
|
|
import managers
|
|
|
|
|
2011-10-20 19:32:31 +00:00
|
|
|
|
2011-11-02 11:57:10 +00:00
|
|
|
class MetaClip:
|
2011-10-02 18:16:28 +00:00
|
|
|
def update_calculated_values(self):
|
2011-11-22 16:49:38 +00:00
|
|
|
start = self.start
|
|
|
|
end = self.end
|
|
|
|
if self.item.sort.duration:
|
|
|
|
start = min(self.start, self.item.sort.duration)
|
|
|
|
end = min(self.end, self.item.sort.duration)
|
2011-11-07 15:32:50 +00:00
|
|
|
self.duration = end - start
|
2011-10-02 18:16:28 +00:00
|
|
|
if self.duration > 0:
|
|
|
|
self.hue, self.saturation, self.lightness = extract.average_color(
|
|
|
|
self.item.timeline_prefix, self.start, self.end)
|
|
|
|
self.volume = extract.average_volume(self.item.timeline_prefix, self.start, self.end)
|
|
|
|
else:
|
|
|
|
self.hue = self.saturation = self.lightness = 0
|
|
|
|
self.volume = 0
|
2011-10-29 10:01:41 +00:00
|
|
|
|
2011-10-02 18:16:28 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2011-10-20 19:32:31 +00:00
|
|
|
self.public_id = u"%s/%s-%s" %(self.item.itemId, float(self.start), float(self.end))
|
2011-10-02 18:16:28 +00:00
|
|
|
if self.duration != self.end - self.start:
|
|
|
|
self.update_calculated_values()
|
2011-10-29 10:01:41 +00:00
|
|
|
if not self.aspect_ratio and self.item:
|
|
|
|
streams = self.item.streams()
|
|
|
|
if streams:
|
|
|
|
self.aspect_ratio = streams[0].aspect_ratio
|
2012-03-22 21:33:42 +00:00
|
|
|
if self.item:
|
|
|
|
self.user = self.item.user and self.item.user.id
|
|
|
|
self.sort = self.item.sort
|
2011-11-02 14:46:27 +00:00
|
|
|
if self.id:
|
2012-03-07 10:36:32 +00:00
|
|
|
sortvalue = ''
|
|
|
|
if self.id:
|
2012-04-02 15:33:09 +00:00
|
|
|
for l in settings.CONFIG.get('clipLayers', []):
|
2012-03-07 10:36:32 +00:00
|
|
|
sortvalue += ''.join(filter(lambda s: s,
|
|
|
|
[a.sortvalue
|
|
|
|
for a in self.annotations.filter(layer=l).order_by('sortvalue')]))
|
|
|
|
if sortvalue:
|
|
|
|
self.sortvalue = sortvalue[:900]
|
|
|
|
else:
|
|
|
|
self.sortvalue = None
|
|
|
|
self.findvalue = '\n'.join(filter(None, [a.findvalue for a in self.annotations.all()]))
|
2012-01-20 17:53:21 +00:00
|
|
|
for l in settings.CONFIG['clipLayers']:
|
2011-11-02 14:46:27 +00:00
|
|
|
setattr(self, l, self.annotations.filter(layer=l).count()>0)
|
2011-11-02 11:57:10 +00:00
|
|
|
models.Model.save(self, *args, **kwargs)
|
2011-10-02 18:16:28 +00:00
|
|
|
|
2011-12-29 17:04:53 +00:00
|
|
|
clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified',
|
|
|
|
'hue', 'saturation', 'lightness', 'volume', 'videoRatio')
|
2012-01-05 12:31:56 +00:00
|
|
|
def json(self, keys=None, qs=None):
|
2011-10-02 18:16:28 +00:00
|
|
|
j = {}
|
2011-12-29 17:04:53 +00:00
|
|
|
for key in self.clip_keys:
|
2011-10-09 11:44:50 +00:00
|
|
|
j[key] = getattr(self, {
|
2011-10-02 18:16:28 +00:00
|
|
|
'id': 'public_id',
|
|
|
|
'in': 'start',
|
|
|
|
'out': 'end',
|
2011-10-09 12:02:16 +00:00
|
|
|
'position': 'start',
|
2011-10-29 10:01:41 +00:00
|
|
|
'videoRatio': 'aspect_ratio',
|
2011-10-09 11:44:50 +00:00
|
|
|
}.get(key, key))
|
2011-12-30 06:44:12 +00:00
|
|
|
if not j['videoRatio']:
|
|
|
|
j['videoRatio'] = 4/3
|
2011-10-02 18:16:28 +00:00
|
|
|
if keys:
|
2011-10-09 11:44:50 +00:00
|
|
|
for key in j.keys():
|
|
|
|
if key not in keys:
|
|
|
|
del j[key]
|
2011-11-03 11:17:50 +00:00
|
|
|
#needed here to make item find with clips work
|
|
|
|
if 'annotations' in keys:
|
2012-01-20 17:53:21 +00:00
|
|
|
annotations = self.annotations.filter(layer__in=settings.CONFIG['clipLayers'])
|
2012-01-05 12:31:56 +00:00
|
|
|
if qs:
|
|
|
|
annotations = annotations.filter(qs)
|
2011-11-03 11:17:50 +00:00
|
|
|
j['annotations'] = [a.json(keys=['value', 'id', 'layer'])
|
2012-01-05 12:31:56 +00:00
|
|
|
for a in annotations]
|
2011-10-09 11:44:50 +00:00
|
|
|
for key in keys:
|
2011-12-29 17:04:53 +00:00
|
|
|
if key not in self.clip_keys and key not in j:
|
2011-10-28 23:35:01 +00:00
|
|
|
value = self.item.get(key)
|
|
|
|
if not value and hasattr(self.item.sort, key):
|
|
|
|
value = getattr(self.item.sort, key)
|
|
|
|
j[key] = value
|
2011-10-02 18:16:28 +00:00
|
|
|
return j
|
|
|
|
|
2011-10-20 19:32:31 +00:00
|
|
|
@classmethod
|
|
|
|
def get_or_create(cls, item, start, end):
|
|
|
|
start = float(start)
|
|
|
|
end = float(end)
|
|
|
|
public_id = u"%s/%s-%s" %(item.itemId, start, end)
|
|
|
|
qs = cls.objects.filter(public_id=public_id)
|
|
|
|
if qs.count() == 0:
|
|
|
|
clip = Clip(item=item, start=start, end=end, public_id=public_id)
|
|
|
|
clip.save()
|
|
|
|
created = True
|
|
|
|
else:
|
|
|
|
clip = qs[0]
|
|
|
|
created = False
|
|
|
|
return clip, created
|
|
|
|
|
2011-10-02 18:16:28 +00:00
|
|
|
def __unicode__(self):
|
2011-10-20 19:32:31 +00:00
|
|
|
return self.public_id
|
2011-10-02 18:16:28 +00:00
|
|
|
|
2011-11-02 11:57:10 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("item", "start", "end")
|
|
|
|
|
|
|
|
attrs = {
|
|
|
|
'__module__': 'clip.models',
|
|
|
|
'Meta': Meta,
|
|
|
|
'objects': managers.ClipManager(),
|
|
|
|
'created': models.DateTimeField(auto_now_add=True),
|
|
|
|
'modified': models.DateTimeField(auto_now=True),
|
|
|
|
'public_id': models.CharField(max_length=128, unique=True),
|
|
|
|
'aspect_ratio': models.FloatField(default=0),
|
|
|
|
|
|
|
|
'item': models.ForeignKey('item.Item', related_name='clips'),
|
2012-03-23 15:52:20 +00:00
|
|
|
'sort': models.ForeignKey('item.ItemSort', related_name='matching_clips'),
|
2012-03-22 21:33:42 +00:00
|
|
|
'user': models.IntegerField(db_index=True, null=True),
|
2011-11-02 11:57:10 +00:00
|
|
|
|
|
|
|
#seconds
|
|
|
|
'start': models.FloatField(default=-1, db_index=True),
|
|
|
|
'end': models.FloatField(default=-1),
|
|
|
|
'duration': models.FloatField(default=0, db_index=True),
|
|
|
|
|
2012-03-08 18:56:31 +00:00
|
|
|
|
2011-11-02 11:57:10 +00:00
|
|
|
#get from annotation
|
|
|
|
'hue': models.FloatField(default=0, db_index=True),
|
|
|
|
'saturation': models.FloatField(default=0, db_index=True),
|
|
|
|
'lightness': models.FloatField(default=0, db_index=True),
|
|
|
|
'volume': models.FloatField(default=0, null=True, db_index=True),
|
|
|
|
|
2012-01-20 17:53:21 +00:00
|
|
|
'sortvalue': models.CharField(max_length=1000, null=True, db_index=True),
|
2012-03-27 11:26:51 +00:00
|
|
|
'findvalue': models.TextField(null=True, db_index=settings.DB_GIN_TRGM),
|
2011-11-02 11:57:10 +00:00
|
|
|
}
|
2012-01-20 17:53:21 +00:00
|
|
|
for name in settings.CONFIG['clipLayers']:
|
2011-11-02 11:57:10 +00:00
|
|
|
attrs[name] = models.BooleanField(default=False, db_index=True)
|
|
|
|
|
|
|
|
Clip = type('Clip', (MetaClip,models.Model), attrs)
|
|
|
|
|
2012-03-22 19:40:37 +00:00
|
|
|
class Random(models.Model):
|
|
|
|
clip = models.ForeignKey(Clip, primary_key=True)
|
|
|
|
random = models.BigIntegerField(db_index=True, null=True)
|
|
|
|
|