diff --git a/pandora/edit/models.py b/pandora/edit/models.py index 6383db29..c8cbc96a 100644 --- a/pandora/edit/models.py +++ b/pandora/edit/models.py @@ -11,7 +11,7 @@ from urllib import quote import ox from django.conf import settings -from django.db import models +from django.db import models, transaction from django.db.models import Max from django.contrib.auth.models import User from ox.django.fields import TupleField @@ -55,7 +55,8 @@ class Edit(models.Model): def get_absolute_url(self): 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) if 'annotation' in data and 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.start = data['in'] clip.end = data['out'] - clip.index = Clip.objects.filter(edit=self).aggregate(Max('index'))['index__max'] - if clip.index == None: - clip.index = 0 - else: - clip.index += 1 + clip.index = index # dont add clip if in/out are invalid if not clip.annotation: duration = clip.item.sort.duration if clip.start >= clip.end or clip.start >= duration or clip.end > duration: return False 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 def accessible(self, user): diff --git a/pandora/edit/views.py b/pandora/edit/views.py index 936a99fe..07993476 100644 --- a/pandora/edit/views.py +++ b/pandora/edit/views.py @@ -28,6 +28,7 @@ def addClips(request): ''' takes { edit: string, + index: int, clips: [] item: string, in: float, @@ -35,6 +36,7 @@ def addClips(request): annotation: string } 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 { } ''' @@ -43,8 +45,10 @@ def addClips(request): edit = get_edit_or_404_json(data['edit']) clips = [] if edit.editable(request.user): + index = data.get('index', edit.clips.count()) for c in data['clips']: - clip = edit.add_clip(c) + clip = edit.add_clip(c, index) + index += 1 if not clip: response = json_response(status=500, text='invalid in/out') return render_to_json_response(response)