reduce db roundtrip in update_matching

This commit is contained in:
j 2012-03-09 13:12:11 +01:00
parent 25c20f2190
commit e09fd09bdf

View file

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import json
from django.conf import settings from django.conf import settings
from celery.task import task from celery.task import task
@ -19,16 +21,20 @@ def update_matching_events(id):
a.events.add(Event.get_or_create(a.value)) a.events.add(Event.get_or_create(a.value))
for e in a.events.all(): for e in a.events.all():
e.update_matches() e.update_matches()
ids = [e['id'] for e in Event.objects.all().values('id')]
for i in ids: names = {}
try: for n in Event.objects.all().values('id', 'name', 'alternativeNames'):
e = Event.objects.get(pk=i) names[n['id']] = [n['name']] + json.loads(n['alternativeNames'])
for name in [e.name] + list(e.alternativeNames): value = a.value.lower()
if name.lower() in a.value.lower(): update = []
e.update_matches() for i in names:
break for name in names[i]:
except Event.DoesNotExist: if name.lower() in value:
pass update.append(i)
break
if update:
for e in Event.objects.filter(id__in=update):
e.update_matches()
@task(ignore_resulsts=True, queue='default') @task(ignore_resulsts=True, queue='default')
def update_matching_places(id): def update_matching_places(id):
@ -43,16 +49,20 @@ def update_matching_places(id):
a.places.add(Place.get_or_create(a.value)) a.places.add(Place.get_or_create(a.value))
for p in a.places.all(): for p in a.places.all():
p.update_matches() p.update_matches()
ids = [e['id'] for e in Place.objects.all().values('id')]
for i in ids: names = {}
try: for n in Place.objects.all().values('id', 'name', 'alternativeNames'):
e = Place.objects.get(pk=i) names[n['id']] = [n['name']] + json.loads(n['alternativeNames'])
for name in [e.name] + list(e.alternativeNames): value = a.value.lower()
if name.lower() in a.value.lower(): update = []
e.update_matches() for i in names:
break for name in names[i]:
except Place.DoesNotExist: if name.lower() in value:
pass update.append(i)
break
if update:
for e in Place.objects.filter(id__in=update):
e.update_matches()
@task(ignore_resulsts=True, queue='default') @task(ignore_resulsts=True, queue='default')
def update_item(id): def update_item(id):