async update of annotation matches, fixes deadlock in load_subtitles

This commit is contained in:
j 2016-08-31 18:03:19 +02:00
commit 1c580a2df7
4 changed files with 23 additions and 18 deletions

View file

@ -18,10 +18,13 @@ def update_matches(id, type):
elif type == 'event':
from event.models import Event as Model
a = Annotation.objects.get(pk=id)
try:
a = Annotation.objects.get(pk=id)
except Annotation.DoesNotExist:
return
a_matches = getattr(a, type == 'place' and 'places' or 'events')
#remove undefined matches that only have this annotation
# remove undefined matches that only have this annotation
for p in a_matches.filter(defined=False).exclude(name=a.value):
if p.annotations.exclude(id=id).count() == 0:
p.delete()
@ -33,8 +36,7 @@ def update_matches(id, type):
if a.findvalue:
names = {}
for n in Model.objects.all().values('id', 'name', 'alternativeNames'):
names[n['id']] = [ox.decode_html(x)
for x in (n['name'],) + n['alternativeNames']]
names[n['id']] = [ox.decode_html(x) for x in (n['name'],) + n['alternativeNames']]
value = a.findvalue.lower()
current = [p.id for p in a_matches.all()]
@ -49,19 +51,19 @@ def update_matches(id, type):
new = []
for i in matches:
p = Model.objects.get(pk=i)
#only add places/events that did not get added as a super match
#i.e. only add The Paris Region and not Paris
# only add places/events that did not get added as a super match
# i.e. only add The Paris Region and not Paris
if not filter(lambda n: n in name_matches,
[n.lower() for n in p.get_super_matches()]):
new.append(i)
removed = list(filter(lambda p: p not in new, current))
added = list(filter(lambda p: p not in current, new))
update = removed + added
removed = set(filter(lambda p: p not in new, current))
added = set(filter(lambda p: p not in current, new))
update = list(removed | added)
if update:
for e in Model.objects.filter(id__in=update):
e.update_matches(Annotation.objects.filter(pk=a.id))
else:
#annotation has no value, remove all exisint matches
# annotation has no value, remove all exisint matches
for e in a_matches.all():
e.update_matches(Annotation.objects.filter(pk=a.id))