forked from 0x2620/pandora
move get_matches to annotations
This commit is contained in:
parent
6b99044f85
commit
7140d4c3f5
3 changed files with 52 additions and 73 deletions
|
@ -1,8 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import re
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
import ox
|
||||
|
@ -16,6 +18,52 @@ import utils
|
|||
from tasks import update_matching_events, update_matching_places
|
||||
|
||||
|
||||
def get_matches(obj, model, layer_type):
|
||||
super_matches = []
|
||||
q = Q(name_find__contains=" " + obj.name)|Q(name_find__contains="|%s"%obj.name)
|
||||
for name in obj.alternativeNames:
|
||||
q = q|Q(name_find__contains=" " + name)|Q(name_find__contains="|%s"%name)
|
||||
for p in model.objects.filter(q).exclude(id=obj.id):
|
||||
for othername in [p.name] + list(p.alternativeNames):
|
||||
for name in [obj.name] + list(obj.alternativeNames):
|
||||
if name in othername:
|
||||
super_matches.append(othername)
|
||||
|
||||
|
||||
exact = [l['id'] for l in filter(lambda l: l['type'] == layer_type, settings.CONFIG['layers'])]
|
||||
if exact:
|
||||
q = Q(value__iexact=obj.name)
|
||||
for name in obj.alternativeNames:
|
||||
q = q|Q(value__iexact=name)
|
||||
f = q&Q(layer__in=exact)
|
||||
else:
|
||||
f = None
|
||||
|
||||
has_type = 'has%ss' % layer_type.capitalize()
|
||||
contains = [l['id'] for l in filter(lambda l: l.get(has_type), settings.CONFIG['layers'])]
|
||||
if contains:
|
||||
q = Q(value__icontains=" " + obj.name)|Q(value__istartswith=obj.name)
|
||||
for name in obj.alternativeNames:
|
||||
q = q|Q(value__icontains=" " + name)|Q(value__istartswith=name)
|
||||
contains_matches = q&Q(layer__in=contains)
|
||||
if f:
|
||||
f = contains_matches | f
|
||||
else:
|
||||
f = contains_matches
|
||||
|
||||
matches = []
|
||||
for a in Annotation.objects.filter(f):
|
||||
value = a.value.lower()
|
||||
for name in super_matches:
|
||||
value = value.replace(name.lower(), '')
|
||||
for name in [obj.name] + list(obj.alternativeNames):
|
||||
name = name.lower()
|
||||
if name in value and re.compile('((^|\s)%s([\.,;:!?\-\/\s]|$))'%name).findall(value):
|
||||
matches.append(a.id)
|
||||
break
|
||||
if not matches:
|
||||
matches = [-1]
|
||||
return Annotation.objects.filter(id__in=matches)
|
||||
|
||||
class Annotation(models.Model):
|
||||
objects = managers.AnnotationManager()
|
||||
|
|
|
@ -11,7 +11,7 @@ from django.conf import settings
|
|||
import ox
|
||||
from ox.django import fields
|
||||
|
||||
from annotation.models import Annotation
|
||||
from annotation.models import Annotation, get_matches
|
||||
from item.models import Item
|
||||
from item import utils
|
||||
from person.models import get_name_sort
|
||||
|
@ -70,33 +70,7 @@ class Event(models.Model):
|
|||
return False
|
||||
|
||||
def get_matches(self):
|
||||
layers = [l['id'] for l in filter(lambda l: l['type'] == 'event' or l.get('hasEvents'),
|
||||
settings.CONFIG['layers'])]
|
||||
super_matches = []
|
||||
q = Q(name_find__contains=" " + self.name)|Q(name_find__contains="|%s"%self.name)
|
||||
for name in self.alternativeNames:
|
||||
q = q|Q(name_find__contains=" " + name)|Q(name_find__contains="|%s"%name)
|
||||
for p in Event.objects.filter(q).exclude(id=self.id):
|
||||
for othername in [p.name] + list(p.alternativeNames):
|
||||
for name in [self.name] + list(self.alternativeNames):
|
||||
if name in othername:
|
||||
super_matches.append(othername)
|
||||
q = Q(value__icontains=" " + self.name)|Q(value__startswith=self.name)
|
||||
for name in self.alternativeNames:
|
||||
q = q|Q(value__icontains=" " + name)|Q(value__startswith=name)
|
||||
matches = []
|
||||
for a in Annotation.objects.filter(layer__in=layers).filter(q):
|
||||
value = a.value.lower()
|
||||
for name in super_matches:
|
||||
value = value.replace(name.lower(), '')
|
||||
for name in [self.name] + list(self.alternativeNames):
|
||||
name = name.lower()
|
||||
if name in value and re.compile('((^|\s)%s([\.,;:!?\-\/\s]|$))'%name).findall(value):
|
||||
matches.append(a.id)
|
||||
break
|
||||
if not matches:
|
||||
matches = [-1]
|
||||
return Annotation.objects.filter(id__in=matches)
|
||||
return get_matches(self, Event, 'event')
|
||||
|
||||
@transaction.commit_on_success
|
||||
def update_matches(self):
|
||||
|
|
|
@ -12,7 +12,7 @@ import ox
|
|||
from ox.django import fields
|
||||
|
||||
import managers
|
||||
from annotation.models import Annotation
|
||||
from annotation.models import Annotation, get_matches
|
||||
from item.models import Item
|
||||
|
||||
|
||||
|
@ -80,50 +80,7 @@ class Place(models.Model):
|
|||
return j
|
||||
|
||||
def get_matches(self):
|
||||
super_matches = []
|
||||
q = Q(name_find__contains=" " + self.name)|Q(name_find__contains="|%s"%self.name)
|
||||
for name in self.alternativeNames:
|
||||
q = q|Q(name_find__contains=" " + name)|Q(name_find__contains="|%s"%name)
|
||||
for p in Place.objects.filter(q).exclude(id=self.id):
|
||||
for othername in [p.name] + list(p.alternativeNames):
|
||||
for name in [self.name] + list(self.alternativeNames):
|
||||
if name in othername:
|
||||
super_matches.append(othername)
|
||||
|
||||
|
||||
exact = [l['id'] for l in filter(lambda l: l['type'] == 'place', settings.CONFIG['layers'])]
|
||||
if exact:
|
||||
q = Q(value__iexact=self.name)
|
||||
for name in self.alternativeNames:
|
||||
q = q|Q(value__iexact=name)
|
||||
f = q&Q(layer__in=exact)
|
||||
else:
|
||||
f = None
|
||||
|
||||
contains = [l['id'] for l in filter(lambda l: l.get('hasPlaces'), settings.CONFIG['layers'])]
|
||||
if contains:
|
||||
q = Q(value__icontains=" " + self.name)|Q(value__istartswith=self.name)
|
||||
for name in self.alternativeNames:
|
||||
q = q|Q(value__icontains=" " + name)|Q(value__istartswith=name)
|
||||
contains_matches = q&Q(layer__in=contains)
|
||||
if f:
|
||||
f = contains_matches | f
|
||||
else:
|
||||
f = contains_matches
|
||||
|
||||
matches = []
|
||||
for a in Annotation.objects.filter(f):
|
||||
value = a.value.lower()
|
||||
for name in super_matches:
|
||||
value = value.replace(name.lower(), '')
|
||||
for name in [self.name] + list(self.alternativeNames):
|
||||
name = name.lower()
|
||||
if name in value and re.compile('((^|\s)%s([\.,;:!?\-\/\s]|$))'%name).findall(value):
|
||||
matches.append(a.id)
|
||||
break
|
||||
if not matches:
|
||||
matches = [-1]
|
||||
return Annotation.objects.filter(id__in=matches)
|
||||
return get_matches(self, Place, 'place')
|
||||
|
||||
@transaction.commit_on_success
|
||||
def update_matches(self):
|
||||
|
|
Loading…
Reference in a new issue