update place/event matches in sync

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

View file

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

View file

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

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,