From 928cc11663eb1a3604b7caa7044332257e83b56b Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 18 Nov 2014 14:55:21 +0000 Subject: [PATCH] wrap annotation updates into transactions --- pandora/annotation/models.py | 50 +++++++++++++++++++----------------- pandora/annotation/tasks.py | 19 +++++++------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 555008a77..31a65332b 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -4,7 +4,7 @@ from __future__ import division, with_statement import re import unicodedata -from django.db import models +from django.db import models, transaction from django.db.models import Q from django.contrib.auth.models import User from django.conf import settings @@ -162,34 +162,36 @@ class Annotation(models.Model): self.sortvalue = None self.languages = None - 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.commit_on_success(): + 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) - super(Annotation, self).save(*args, **kwargs) - if set_public_id: - self.set_public_id() + super(Annotation, self).save(*args, **kwargs) + if set_public_id: + self.set_public_id() - 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: + Clip.objects.filter(**{ + 'id': self.clip.id, + self.layer: False + }).update(**{self.layer: True}) + #update clip.findvalue + self.clip.save() - #editAnnotations needs to be in snyc - if layer.get('type') == 'place' or layer.get('hasPlaces'): - update_matches(self.id, 'place') - if layer.get('type') == 'event' or layer.get('hasEvents'): - update_matches(self.id, 'event') + #editAnnotations needs to be in snyc + if layer.get('type') == 'place' or layer.get('hasPlaces'): + update_matches(self.id, 'place') + if layer.get('type') == 'event' or layer.get('hasEvents'): + update_matches(self.id, 'event') def delete(self, *args, **kwargs): - super(Annotation, self).delete(*args, **kwargs) - if self.clip and self.clip.annotations.count() == 0: - self.clip.delete() - self.item.update_find() - self.item.update_sort() - self.item.update_facets() + with transaction.commit_on_success(): + super(Annotation, self).delete(*args, **kwargs) + if self.clip and self.clip.annotations.count() == 0: + self.clip.delete() + self.item.update_find() + self.item.update_sort() + self.item.update_facets() def cleanup_undefined_relations(self): layer = self.get_layer() diff --git a/pandora/annotation/tasks.py b/pandora/annotation/tasks.py index 7fa71c7ff..9eabd28af 100644 --- a/pandora/annotation/tasks.py +++ b/pandora/annotation/tasks.py @@ -100,12 +100,13 @@ def update_item(id): #cleanup orphaned clips Clip.objects.filter(item__id=a.item.id, annotations__id=None).delete() #update facets if needed - if filter(lambda f: f['id'] == a.layer and f.get('filter'), settings.CONFIG['itemKeys']): - a.item.update_layer_facet(a.layer) - Item.objects.filter(id=a.item.id).update(modified=a.modified) - a.item.modified = a.modified - a.item.update_find() - a.item.update_sort() - a.item.update_facets() - if a.item.update_languages(): - a.item.save() + with transaction.commit_on_sucess(): + if filter(lambda f: f['id'] == a.layer and f.get('filter'), settings.CONFIG['itemKeys']): + a.item.update_layer_facet(a.layer) + Item.objects.filter(id=a.item.id).update(modified=a.modified) + a.item.modified = a.modified + a.item.update_find() + a.item.update_sort() + a.item.update_facets() + if a.item.update_languages(): + a.item.save()