From 220e091460bb550d6fc4324c5ed2fcca2c4d3981 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 16 Mar 2014 13:52:56 +0000 Subject: [PATCH] only add/remove empty clips as needed --- pandora/item/models.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pandora/item/models.py b/pandora/item/models.py index 258e5172..f5581eed 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -1417,15 +1417,27 @@ class Item(models.Model): duration = sum([s.duration for s in self.streams()]) start = 0 layer = subtitles['id'] - Annotation.objects.filter(layer=layer, item=self, value='').delete() #FIXME: allow annotations from no user instead? user = User.objects.all().order_by('id')[0] - for i in range(0, int(duration) - 5, 60): + + clips = [(i, i+5) for i in range(0, int(duration) - 5, 60)] + exist = [] + delete = [] + for a in Annotation.objects.filter(layer=layer, item=self, value=''): + clip = (a.start, a.end) + if clip not in clips: + delete.append(a.id) + else: + exist.append(clip) + if delete: + Annotation.objects.filter(layer=layer, item=self, value='', id__in=delete).delete() + clips = list(set(clips) - set(exist)) + for clip in clips: annotation = Annotation( item=self, layer=layer, - start=i, - end=i + 5, + start=clip[0], + end=clip[1], value='', user=user )