smarter places/events update
This commit is contained in:
parent
59f575d489
commit
12226662f8
4 changed files with 87 additions and 70 deletions
|
|
@ -12,8 +12,7 @@ import ox
|
|||
from ox.django import fields
|
||||
|
||||
import managers
|
||||
from annotation.models import Annotation, get_matches
|
||||
from annotation.tasks import update_matching_places
|
||||
from annotation.models import Annotation, get_matches, get_super_matches
|
||||
from item.models import Item
|
||||
from changelog.models import Changelog
|
||||
|
||||
|
|
@ -96,30 +95,39 @@ class Place(models.Model):
|
|||
j[key] = getattr(self, key)
|
||||
return j
|
||||
|
||||
def get_matches(self):
|
||||
return get_matches(self, Place, 'place')
|
||||
def get_matches(self, qs=None):
|
||||
return get_matches(self, Place, 'place', qs)
|
||||
|
||||
def get_super_matches(self):
|
||||
return get_super_matches(self, Place)
|
||||
|
||||
@transaction.commit_on_success
|
||||
def update_matches(self):
|
||||
matches = self.get_matches()
|
||||
numberofmatches = matches.count()
|
||||
for a in self.annotations.exclude(id__in=matches):
|
||||
def update_matches(self, annotations=None):
|
||||
matches = self.get_matches(annotations)
|
||||
if not annotations:
|
||||
numberofmatches = matches.count()
|
||||
annotations = self.annotations.all()
|
||||
else:
|
||||
numberofmatches = -1
|
||||
for a in 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()):
|
||||
for a 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:
|
||||
self.annotations.add(i)
|
||||
ids = list(set([a.item.id for a in matches]))
|
||||
if self.annotations.filter(id=a.id).count() == 0:
|
||||
self.annotations.add(a)
|
||||
ids = list(set([a['item_id'] for a in self.annotations.all().values('item_id')]))
|
||||
for i in self.items.exclude(id__in=ids):
|
||||
self.items.remove(i)
|
||||
for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()):
|
||||
if self.items.filter(id=i.id).count() == 0:
|
||||
self.items.add(i)
|
||||
if numberofmatches < 0:
|
||||
numberofmatches = self.annotations.all().count()
|
||||
if self.matches != numberofmatches:
|
||||
self.matches = numberofmatches
|
||||
if numberofmatches:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue