update place/event matches in sync

This commit is contained in:
j 2012-02-20 14:09:26 +00:00
commit c49cc47c3d
4 changed files with 24 additions and 13 deletions

View file

@ -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):

View file

@ -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,