From 4be61a3982fae5aa7578ffc1084675ba277bd64a Mon Sep 17 00:00:00 2001 From: j Date: Mon, 14 Feb 2022 13:32:24 +0100 Subject: [PATCH] smaller transaction, use update_fields to update clip values --- pandora/annotation/models.py | 27 ++++++++++++--------------- pandora/clip/models.py | 9 ++++++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index e1e9d553..8b83a3db 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -163,28 +163,25 @@ class Annotation(models.Model): self.sortvalue = None self.languages = None - with transaction.atomic(): - if not self.clip or self.start != self.clip.start or self.end != self.clip.end: - self.clip, created = Clip.get_or_create(self.item, self.start, self.end) + if not self.clip or self.start != self.clip.start or self.end != self.clip.end: + self.clip, created = Clip.get_or_create(self.item, self.start, self.end) + with transaction.atomic(): if set_public_id: self.set_public_id() super(Annotation, self).save(*args, **kwargs) - if self.clip: - Clip.objects.filter(**{ - 'id': self.clip.id, - self.layer: False - }).update(**{self.layer: True}) - # update clip.findvalue - self.clip.save() + if self.clip: + self.clip.update_findvalue() + setattr(self.clip, self.layer, True) + self.clip.save(update_fields=[self.layer, 'sortvalue', 'findvalue']) - # update matches in bulk if called from load_subtitles - if not delay_matches: - self.update_matches() - self.update_documents() - self.update_translations() + # update matches in bulk if called from load_subtitles + if not delay_matches: + self.update_matches() + self.update_documents() + self.update_translations() def update_matches(self): from place.models import Place diff --git a/pandora/clip/models.py b/pandora/clip/models.py index d3639baa..8ae0cd11 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -60,9 +60,7 @@ class MetaClip(object): self.hue = self.saturation = self.lightness = 0 self.volume = 0 - def save(self, *args, **kwargs): - if self.duration != self.end - self.start: - self.update_calculated_values() + def update_findvalue(self): if not self.aspect_ratio and self.item: streams = self.item.streams() if streams: @@ -90,6 +88,11 @@ class MetaClip(object): self.findvalue = '\n'.join(list(filter(None, [a.findvalue for a in anns]))) for l in [k['id'] for k in settings.CONFIG['layers']]: setattr(self, l, l in anns_by_layer and bool(len(anns_by_layer[l]))) + + def save(self, *args, **kwargs): + if self.duration != self.end - self.start: + self.update_calculated_values() + self.update_findvalue() models.Model.save(self, *args, **kwargs) clip_keys = ('id', 'in', 'out', 'position', 'created', 'modified',