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

View file

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