From c49cc47c3d4b5642126aea8998909a3620361962 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 20 Feb 2012 14:09:26 +0000 Subject: [PATCH] update place/event matches in sync --- pandora/event/models.py | 13 +++++++++---- pandora/event/views.py | 6 +++--- pandora/place/models.py | 12 +++++++++--- pandora/place/views.py | 6 +++--- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pandora/event/models.py b/pandora/event/models.py index 8770ad89..513266da 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -90,8 +90,13 @@ class Event(models.Model): def update_matches(self): matches = self.get_matches() numberofmatches = matches.count() - for i in self.annotations.exclude(id__in=matches): - self.annotations.remove(i) + for a in self.annotations.exclude(id__in=matches): + self.annotations.remove(a) + #annotations of type event always need an event + if a.get_layer().get('type') == 'event' and a.events.count() == 0: + a.events.add(Event.get_or_create(a.value)) + for e in a.events.all(): + e.update_matches() for i in matches.exclude(id__in=self.annotations.all()): #need to check again since editEvent might have been called again if self.annotations.filter(id=i.id).count() == 0: @@ -103,10 +108,10 @@ class Event(models.Model): if self.items.filter(id=i.id).count() == 0: self.items.add(i) if self.matches != numberofmatches: + self.matches = numberofmatches if numberofmatches: Event.objects.filter(id=self.id).update(matches=numberofmatches) else: - self.matches = numberofmatches self.save() def set_name_sort(self, value=None): @@ -123,7 +128,7 @@ class Event(models.Model): self.set_name_sort() self.name_find = '||' + self.name + '||'.join(self.alternativeNames) + '||' self.defined = len(filter(None, [getattr(self, key) - for key in ('start', 'end', 'startTime', 'endTime')])) > 0 + for key in ('start', 'end')])) > 0 if self.endTime and self.startTime: self.durationTime = self.endTime - self.startTime diff --git a/pandora/event/views.py b/pandora/event/views.py index f25ca0eb..bbbed6c0 100644 --- a/pandora/event/views.py +++ b/pandora/event/views.py @@ -14,7 +14,6 @@ from ox.django.api import actions from item import utils import models -import tasks @login_required_json def addEvent(request): @@ -48,8 +47,9 @@ def addEvent(request): setattr(event, key, value) if 'nameSort' in data: event.set_name_sort(data['nameSort']) + event.matches = 0 event.save() - tasks.update_matches.delay(event.id) + event.update_matches.() response = json_response(status=200, text='created') response['data'] = event.json() else: @@ -96,7 +96,7 @@ def editEvent(request): event.set_name_sort(data['nameSort']) event.save() if 'name' in data or 'alternativeNames' in data: - tasks.update_matches.delay(event.id) + event.update_matches() response = json_response(status=200, text='updated') response['data'] = event.json() else: diff --git a/pandora/place/models.py b/pandora/place/models.py index c2fc9dbb..14ef7c85 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -13,6 +13,7 @@ from ox.django import fields import managers from annotation.models import Annotation, get_matches +from annotation.tasks import update_matching_places from item.models import Item from changelog.models import Changelog @@ -102,8 +103,13 @@ class Place(models.Model): def update_matches(self): matches = self.get_matches() numberofmatches = matches.count() - for i in self.annotations.exclude(id__in=matches): - self.annotations.remove(i) + for a in self.annotations.exclude(id__in=matches): + self.annotations.remove(a) + #annotations of type place always need a place + if a.get_layer().get('type') == 'place' and a.places.count() == 0: + a.places.add(Place.get_or_create(a.value)) + for p in a.places.all(): + p.update_matches() for i in matches.exclude(id__in=self.annotations.all()): #need to check again since editEvent might have been called again if self.annotations.filter(id=i.id).count() == 0: @@ -115,10 +121,10 @@ class Place(models.Model): if self.items.filter(id=i.id).count() == 0: self.items.add(i) if self.matches != numberofmatches: + self.matches = numberofmatches if numberofmatches: Place.objects.filter(id=self.id).update(matches=numberofmatches) else: - self.matches = numberofmatches self.save() def make_undefined(self): diff --git a/pandora/place/views.py b/pandora/place/views.py index db954729..caa36a04 100644 --- a/pandora/place/views.py +++ b/pandora/place/views.py @@ -15,7 +15,6 @@ from ox.django.api import actions from item import utils import models -import tasks @login_required_json def addPlace(request): @@ -64,8 +63,9 @@ def addPlace(request): if isinstance(value, list): value = tuple(value) setattr(place, key, value) + place.matches = 0 place.save() - tasks.update_matches.delay(place.id) + place.update_matches() response = json_response(place.json()) else: response = json_response(status=409, @@ -122,7 +122,7 @@ def editPlace(request): setattr(place, key, value) place.save() if 'name' in data or 'alternativeNames' in data: - tasks.update_matches.delay(place.id) + place.update_matches() response = json_response(place.json()) else: response = json_response(status=409,