only update subtitles if needed

This commit is contained in:
j 2016-09-06 00:13:40 +02:00
parent d53659c0c4
commit 4d1711cb01
2 changed files with 66 additions and 69 deletions

View file

@ -133,8 +133,7 @@ class Annotation(models.Model):
return {}
def save(self, *args, **kwargs):
from .tasks import update_matches
async = kwargs.pop('async', False)
delay_matches = kwargs.pop('delay_matches', False)
set_public_id = not self.id or not self.public_id
layer = self.get_layer()
@ -181,11 +180,8 @@ class Annotation(models.Model):
# update clip.findvalue
self.clip.save()
# editAnnotations needs to be in snyc
# load_subtitles can not be in sync
if async:
update_matches.delay(self.id)
else:
# update matches in bulk if called from load_subtitles
if not delay_matches:
self.update_matches()
def update_matches(self):

View file

@ -1588,9 +1588,9 @@ class Item(models.Model):
# only import on 0xdb for now or if forced manually
# since this will remove all existing subtitles
if force or not existing.count() or settings.USE_IMDB:
with transaction.atomic():
Annotation.objects.filter(layer=layer, item=self).delete()
AnnotationSequence.reset(self)
new = []
current = [(v.start, v.end, v.value) for v in Annotation.objects.filter(layer=layer, item=self)]
current.sort()
offset = 0
language = ''
subtitles = self.files.filter(selected=True, is_subtitle=True, available=True)
@ -1624,33 +1624,34 @@ class Item(models.Model):
if data['in'] < self.json['duration'] and data['out'] > self.json['duration']:
data['out'] = self.json['duration']
if data['in'] < self.json['duration']:
annotation = Annotation(
item=self,
layer=layer,
start=float('%0.03f' % data['in']),
end=float('%0.03f' % data['out']),
value=value,
user=user
)
annotation.save(async=True)
new.append((float('%0.03f' % data['in']), float('%0.03f' % data['out']), value))
# otherwise add empty 5 seconds annotation every minute
if not subtitles_added:
start = offset and int(offset / 60) * 60 + 60 or 0
for i in range(start,
int(offset + f.duration) - 5,
60):
new.append((i, i+5, ''))
offset += f.duration
if current != new:
with transaction.atomic():
# FIXME: only reset if most subtitles are new
Annotation.objects.filter(layer=layer, item=self).delete()
AnnotationSequence.reset(self)
for start, end, value in new:
annotation = Annotation(
item=self,
layer=layer,
start=i,
end=i + 5,
value='',
start=start,
end=end,
value=value,
user=user
)
annotation.save(async=True)
offset += f.duration
annotation.save(delay_matches=True)
# remove left over clips without annotations
Clip.objects.filter(item=self, annotations__id=None).delete()
for a in self.annotations.filter(layer=layer):
a.update_matches()
return True
else:
self.add_empty_clips()