add clip at index

This commit is contained in:
j 2013-07-18 10:38:10 +00:00
parent e415eeb45b
commit 2710571eb0
2 changed files with 15 additions and 8 deletions

View file

@ -11,7 +11,7 @@ from urllib import quote
import ox import ox
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models, transaction
from django.db.models import Max from django.db.models import Max
from django.contrib.auth.models import User from django.contrib.auth.models import User
from ox.django.fields import TupleField from ox.django.fields import TupleField
@ -55,7 +55,8 @@ class Edit(models.Model):
def get_absolute_url(self): def get_absolute_url(self):
return ('/edits/%s' % quote(self.get_id())).replace('%3A', ':') return ('/edits/%s' % quote(self.get_id())).replace('%3A', ':')
def add_clip(self, data): def add_clip(self, data, index):
ids = [i['id'] for i in self.clips.order_by('index').values('id')]
clip = Clip(edit=self) clip = Clip(edit=self)
if 'annotation' in data and data['annotation']: if 'annotation' in data and data['annotation']:
clip.annotation = Annotation.objects.get(public_id=data['annotation']) clip.annotation = Annotation.objects.get(public_id=data['annotation'])
@ -64,17 +65,19 @@ class Edit(models.Model):
clip.item = Item.objects.get(itemId=data['item']) clip.item = Item.objects.get(itemId=data['item'])
clip.start = data['in'] clip.start = data['in']
clip.end = data['out'] clip.end = data['out']
clip.index = Clip.objects.filter(edit=self).aggregate(Max('index'))['index__max'] clip.index = index
if clip.index == None:
clip.index = 0
else:
clip.index += 1
# dont add clip if in/out are invalid # dont add clip if in/out are invalid
if not clip.annotation: if not clip.annotation:
duration = clip.item.sort.duration duration = clip.item.sort.duration
if clip.start >= clip.end or clip.start >= duration or clip.end > duration: if clip.start >= clip.end or clip.start >= duration or clip.end > duration:
return False return False
clip.save() clip.save()
ids.insert(index, clip.id)
index = 0
with transaction.commit_on_success():
for i in ids:
Clip.objects.filter(id=i).update(index=index)
index += 1
return clip return clip
def accessible(self, user): def accessible(self, user):

View file

@ -28,6 +28,7 @@ def addClips(request):
''' '''
takes { takes {
edit: string, edit: string,
index: int,
clips: [] clips: []
item: string, item: string,
in: float, in: float,
@ -35,6 +36,7 @@ def addClips(request):
annotation: string annotation: string
} }
add clips with item/in/out or annotation to edit with id add clips with item/in/out or annotation to edit with id
clips are added at index or end if index is not provided
returns { returns {
} }
''' '''
@ -43,8 +45,10 @@ def addClips(request):
edit = get_edit_or_404_json(data['edit']) edit = get_edit_or_404_json(data['edit'])
clips = [] clips = []
if edit.editable(request.user): if edit.editable(request.user):
index = data.get('index', edit.clips.count())
for c in data['clips']: for c in data['clips']:
clip = edit.add_clip(c) clip = edit.add_clip(c, index)
index += 1
if not clip: if not clip:
response = json_response(status=500, text='invalid in/out') response = json_response(status=500, text='invalid in/out')
return render_to_json_response(response) return render_to_json_response(response)