From 3312e2f25886835de4cdd56d64dd076468d56528 Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 5 Oct 2012 16:52:29 +0530 Subject: [PATCH 1/2] trigram / alt names --- chaloBEST/mumbai/apiviews.py | 12 +++++++++--- chaloBEST/mumbai/models.py | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/chaloBEST/mumbai/apiviews.py b/chaloBEST/mumbai/apiviews.py index 2765641..09e0b6a 100644 --- a/chaloBEST/mumbai/apiviews.py +++ b/chaloBEST/mumbai/apiviews.py @@ -66,14 +66,20 @@ def areas(request): def stops(request): q = request.GET.get("q", "") + ctype = ContentType.objects.get_for_model(Stop) + stops = [] if q != '': - qset = Stop.objects.find_approximate(q, TRIGRAM_THRESHOLD) + alt_name_matches = AlternativeName.objects.find_approximate(q, TRIGRAM_THRESHOLD).filter(content_type=ctype) + for alt_name in alt_name_matches: + stop = alt_name.content_object + if stop not in stops: + stops.append(stop) else: - qset = Stop.objects.all() + stops = Stop.objects.all() srid = int(request.GET.get("srid", 4326)) return render_to_json_response({ 'type': 'FeatureCollection', - 'features': [stop.get_geojson(srid=srid) for stop in qset] + 'features': [stop.get_geojson(srid=srid) for stop in stops] }) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index 89e63b6..c4025ff 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -426,6 +426,7 @@ ALT_TYPE_CHOICES = ( ) class AlternativeName(models.Model): + objects = TrigramSearchManager(('name',)) name = models.CharField(max_length=512) name_mr = models.CharField(max_length=512, blank=True) typ = models.CharField(max_length=64, choices=ALT_TYPE_CHOICES, default="alt") From ce68ea4fb3790b9374e5f5fbba4ca37af102c1f0 Mon Sep 17 00:00:00 2001 From: Sanj Date: Fri, 5 Oct 2012 16:59:47 +0530 Subject: [PATCH 2/2] oopsies --- chaloBEST/mumbai/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index 2cd6ecc..1851bfd 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -180,7 +180,7 @@ class Stop(models.Model): 'name_mr': self.name_mr, 'direction': self.dbdirection, 'routes': ", ".join([r.alias for r in routes]), - 'alternative_names': ", ".join([a.name for a in self.alt_names.all().filter(typ='common')]), + 'alternative_names': ", ".join([a.name for a in self.alt_names.all()]), 'url': self.get_absolute_url() }