From 7140d4c3f5d285eda1dc8e62dc76c7cda162fa30 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 17 Jan 2012 14:28:33 +0530 Subject: [PATCH] move get_matches to annotations --- pandora/annotation/models.py | 48 ++++++++++++++++++++++++++++++++++++ pandora/event/models.py | 30 ++-------------------- pandora/place/models.py | 47 ++--------------------------------- 3 files changed, 52 insertions(+), 73 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index df22e452..97422e28 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -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() diff --git a/pandora/event/models.py b/pandora/event/models.py index 3bfb3190..8c4a9b54 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -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): diff --git a/pandora/place/models.py b/pandora/place/models.py index 45afb55d..29eee36d 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -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):