dont save clip/sequence ids in db

This commit is contained in:
j 2012-06-18 16:36:04 +02:00
parent af8f7e2842
commit 81cef44d19
7 changed files with 47 additions and 37 deletions

View file

@ -23,7 +23,6 @@ def parseCondition(condition, user):
k = condition.get('key', 'name') k = condition.get('key', 'name')
k = { k = {
'event': 'annotations__events__id', 'event': 'annotations__events__id',
'id': 'public_id',
'in': 'start', 'in': 'start',
'out': 'end', 'out': 'end',
'place': 'annotations__places__id', 'place': 'annotations__places__id',
@ -53,14 +52,17 @@ def parseCondition(condition, user):
if op == '-': if op == '-':
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \ q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user) & parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
if exclude: return exclude and ~q or q
return ~q
else:
return q
if (not exclude and op == '=' or op in ('$', '^', '>=', '<')) and v == '': if (not exclude and op == '=' or op in ('$', '^', '>=', '<')) and v == '':
return Q() return Q()
if k.endswith('__id'): if k == 'id':
itemId, points = v.split('/')
points = [float('%0.03f'%float(p)) for p in points.split('-')]
q = Q(item__itemId=itemId, start=points[0], end=points[1])
return exclude and ~q or q
elif k.endswith('__id'):
v = decode_id(v) v = decode_id(v)
if isinstance(v, bool): #featured and public flag if isinstance(v, bool): #featured and public flag
key = k key = k

View file

@ -26,7 +26,6 @@ class MetaClip:
self.volume = 0 self.volume = 0
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.public_id = u"%s/%0.03f-%0.03f" %(self.item.itemId, float(self.start), float(self.end))
if self.duration != self.end - self.start: if self.duration != self.end - self.start:
self.update_calculated_values() self.update_calculated_values()
if not self.aspect_ratio and self.item: if not self.aspect_ratio and self.item:
@ -89,10 +88,11 @@ class MetaClip:
def get_or_create(cls, item, start, end): def get_or_create(cls, item, start, end):
start = float(start) start = float(start)
end = float(end) end = float(end)
public_id = u"%s/%0.03f-%0.03f" %(item.itemId, start, end) start = float('%0.03f' % start)
qs = cls.objects.filter(public_id=public_id) end = float('%0.03f' % end)
qs = cls.objects.filter(item=item, start=start, end=end)
if qs.count() == 0: if qs.count() == 0:
clip = Clip(item=item, start=start, end=end, public_id=public_id) clip = Clip(item=item, start=start, end=end)
clip.save() clip.save()
created = True created = True
else: else:
@ -100,6 +100,10 @@ class MetaClip:
created = False created = False
return clip, created return clip, created
@property
def public_id(self):
return u"%s/%0.03f-%0.03f" %(self.item.itemId, float(self.start), float(self.end))
def __unicode__(self): def __unicode__(self):
return self.public_id return self.public_id
@ -112,7 +116,6 @@ attrs = {
'objects': managers.ClipManager(), 'objects': managers.ClipManager(),
'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),
'public_id': models.CharField(max_length=128, unique=True),
'aspect_ratio': models.FloatField(default=0), 'aspect_ratio': models.FloatField(default=0),
'item': models.ForeignKey('item.Item', related_name='clips'), 'item': models.ForeignKey('item.Item', related_name='clips'),

View file

@ -60,6 +60,11 @@ def order_query(qs, sort):
elif key not in clip_keys: elif key not in clip_keys:
#key mgith need to be changed, see order_sort in item/views.py #key mgith need to be changed, see order_sort in item/views.py
key = "sort__%s" % key key = "sort__%s" % key
if key == 'public_id':
order_by.append('%s%s' % (operator, 'sort__itemId'))
order_by.append('%s%s' % (operator, 'start'))
order_by.append('end')
else:
order = '%s%s' % (operator, key) order = '%s%s' % (operator, key)
order_by.append(order) order_by.append(order)
if order_by: if order_by:
@ -102,15 +107,19 @@ def findClips(request):
response['data']['items'] = [add(p) for p in qs] response['data']['items'] = [add(p) for p in qs]
keys = data['keys'] keys = data['keys']
def clip_public_id(c):
return u'%s/%0.03f-%0.03f' % (c['public_id'].split('/')[0], c['clip__start'], c['clip__end'])
def add_annotations(key, qs, add_layer=False): def add_annotations(key, qs, add_layer=False):
values = ['public_id', 'value', 'clip__public_id'] values = ['public_id', 'value', 'clip__start', 'clip__end']
if add_layer: if add_layer:
values.append('layer') values.append('layer')
if query['filter']: if query['filter']:
qs = qs.filter(query['filter']) qs = qs.filter(query['filter'])
for a in qs.values(*values): for a in qs.values(*values):
public_id = clip_public_id(a)
for i in response['data']['items']: for i in response['data']['items']:
if i['id'] == a['clip__public_id']: if i['id'] == public_id:
if not key in i: if not key in i:
i[key] = [] i[key] = []
l = { l = {
@ -130,7 +139,8 @@ def findClips(request):
add_annotations(layer, aqs) add_annotations(layer, aqs)
elif 'position' in query: elif 'position' in query:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
ids = [i['public_id'] for i in qs.values('public_id')] ids = [u'%s/%0.03f-%0.03f' % (c['item__itemId'], c['start'], c['end'])
for c in qs.values('item__itemId', 'start', 'end')]
data['conditions'] = data['conditions'] + { data['conditions'] = data['conditions'] + {
'value': data['position'], 'value': data['position'],
'key': query['sort'][0]['key'], 'key': query['sort'][0]['key'],
@ -142,7 +152,8 @@ def findClips(request):
response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0] response['data']['position'] = utils.get_positions(ids, [qs[0].itemId])[0]
elif 'positions' in data: elif 'positions' in data:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
ids = [i['public_id'] for i in qs.values('public_id')] ids = [u'%s/%0.03f-%0.03f' % (c['item__itemId'], c['start'], c['end'])
for c in qs.values('item__itemId', 'start', 'end')]
response['data']['positions'] = utils.get_positions(ids, data['positions']) response['data']['positions'] = utils.get_positions(ids, data['positions'])
else: else:
response['data']['items'] = qs.count() response['data']['items'] = qs.count()

View file

@ -40,7 +40,7 @@ import archive.models
from person.models import get_name_sort from person.models import get_name_sort
from title.models import get_title_sort from title.models import get_title_sort
from sequence.tasks import get_sequences, update_sequence_ids from sequence.tasks import get_sequences
def get_id(info): def get_id(info):
q = Item.objects.all() q = Item.objects.all()
@ -367,9 +367,8 @@ class Item(models.Model):
for c in self.clips.all(): c.save() for c in self.clips.all(): c.save()
for a in self.annotations.all(): for a in self.annotations.all():
public_id = a.public_id.split('/')[1] public_id = a.public_id.split('/')[1]
a.public_id = "%s/%s" % ( self.itemId, public_id) a.public_id = "%s/%s" % (self.itemId, public_id)
a.save() a.save()
update_sequence_ids.delay(self.itemId)
if update_poster: if update_poster:
return tasks.update_poster.delay(self.itemId) return tasks.update_poster.delay(self.itemId)

View file

@ -22,7 +22,6 @@ def parseCondition(condition, user):
''' '''
k = condition.get('key', 'name') k = condition.get('key', 'name')
k = { k = {
'id': 'public_id',
'in': 'start', 'in': 'start',
'out': 'end' 'out': 'end'
}.get(k, k) }.get(k, k)
@ -41,13 +40,16 @@ def parseCondition(condition, user):
if op == '-': if op == '-':
q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \ q = parseCondition({'key': k, 'value': v[0], 'operator': '>='}, user) \
& parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user) & parseCondition({'key': k, 'value': v[1], 'operator': '<'}, user)
if exclude: return exclude and ~q or q
return ~q
else:
return q
if (not exclude and op == '=' or op in ('$', '^', '>=', '<')) and v == '': if (not exclude and op == '=' or op in ('$', '^', '>=', '<')) and v == '':
return Q() return Q()
if k == 'id':
itemId, points = v.split('/')
points = [float('%0.03f'%float(p)) for p in points.split('-')]
q = Q(item__itemId=itemId, start=points[0], end=points[1])
return exclude and ~q or q
if k.endswith('__id'): if k.endswith('__id'):
v = decode_id(v) v = decode_id(v)
if isinstance(v, bool): #featured and public flag if isinstance(v, bool): #featured and public flag

View file

@ -19,10 +19,9 @@ from django.db import models
class Sequence(models.Model): class Sequence(models.Model):
class Meta: class Meta:
unique_together = ("public_id", "mode") unique_together = ("item", "start", "end", "mode")
mode = models.CharField(max_length=128) mode = models.CharField(max_length=128)
public_id = models.CharField(max_length=128)
item = models.ForeignKey(Item, null=True, related_name='sequences') item = models.ForeignKey(Item, null=True, related_name='sequences')
sort = models.ForeignKey(ItemSort, null=True, related_name='sequences') sort = models.ForeignKey(ItemSort, null=True, related_name='sequences')
user = models.IntegerField(db_index=True, null=True) user = models.IntegerField(db_index=True, null=True)
@ -35,15 +34,16 @@ class Sequence(models.Model):
objects = managers.SequenceManager() objects = managers.SequenceManager()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.public_id = u"%s/%0.03f-%0.03f" % (
self.item.itemId, float(self.start), float(self.end)
)
self.duration = self.end - self.start self.duration = self.end - self.start
if self.item: if self.item:
self.user = self.item.user and self.item.user.id self.user = self.item.user and self.item.user.id
self.sort = self.item.sort self.sort = self.item.sort
super(Sequence, self).save(*args, **kwargs) super(Sequence, self).save(*args, **kwargs)
@property
def public_id(self):
return u"%s/%0.03f-%0.03f" % (self.item.itemId, float(self.start), float(self.end))
def __unicode__(self): def __unicode__(self):
return self.public_id return self.public_id

View file

@ -33,9 +33,6 @@ def get_sequences(itemId):
'end': float('%0.03f' % s['out']), 'end': float('%0.03f' % s['out']),
'hash': s['hash'] 'hash': s['hash']
} }
sequence['public_id'] = u"%s/%0.03f-%0.03f" % (
i.itemId, sequence['start'], sequence['end']
)
sequence['duration'] = sequence['end'] - sequence['start'] sequence['duration'] = sequence['end'] - sequence['start']
if not keys: if not keys:
keys = ', '.join(['"%s"'%k for k in sequence.keys()]) keys = ', '.join(['"%s"'%k for k in sequence.keys()])
@ -48,7 +45,3 @@ def get_sequences(itemId):
cursor.execute(sql) cursor.execute(sql)
transaction.commit_unless_managed() transaction.commit_unless_managed()
@task(ignore_results=True, queue='encoding')
def update_sequence_ids(itemId):
for s in models.Sequence.objects.filter(item__itemId=itemId):
s.save()