From fda38026520669e5edd02723e4c3f567304b69a1 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 30 Oct 2011 15:17:21 +0100 Subject: [PATCH] only update if number of matches changed --- pandora/event/models.py | 9 ++++++--- pandora/place/models.py | 8 +++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pandora/event/models.py b/pandora/event/models.py index a378c4c3..f7d1baaa 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -4,7 +4,7 @@ from __future__ import division, with_statement import re -from django.db import models +from django.db import models, transaction from django.contrib.auth.models import User from django.db.models import Q import ox @@ -85,9 +85,11 @@ class Event(models.Model): matches = [-1] return Annotation.objects.filter(id__in=matches) + + @transaction.commit_on_success def update_matches(self): matches = self.get_matches() - self.matches = matches.count() + numberofmatches = matches.count() for i in self.annotations.exclude(id__in=matches): self.annotations.remove(i) for i in matches.exclude(id__in=self.annotations.all()): @@ -98,7 +100,8 @@ class Event(models.Model): for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): self.items.add(i) #only update matches, other values might have been changed - Event.objects.filter(id=self.id).update(matches=self.matches) + if self.matches != numberofmatches: + Event.objects.filter(id=self.id).update(matches=numberofmatches) def set_name_sort(self, value=None): if not value: diff --git a/pandora/place/models.py b/pandora/place/models.py index 9c01b2ee..31ed45f4 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -4,7 +4,7 @@ from __future__ import division, with_statement import re -from django.db import models +from django.db import models, transaction from django.contrib.auth.models import User, Group from django.db.models import Q import ox @@ -94,9 +94,10 @@ class Place(models.Model): matches = [-1] return Annotation.objects.filter(id__in=matches) + @transaction.commit_on_success def update_matches(self): matches = self.get_matches() - self.matches = matches.count() + numberofmatches = matches.count() for i in self.annotations.exclude(id__in=matches): self.annotations.remove(i) for i in matches.exclude(id__in=self.annotations.all()): @@ -106,7 +107,8 @@ class Place(models.Model): self.items.remove(i) for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): self.items.add(i) - Place.objects.filter(id=self.id).update(matches=self.matches) + if self.matches != numberofmatches: + Place.objects.filter(id=self.id).update(matches=numberofmatches) def save(self, *args, **kwargs): if not self.name_sort: