From b5fabf412cf55b83a0ceda7991ace83b983ad0a0 Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Tue, 28 Feb 2012 18:14:19 +0530 Subject: [PATCH] Add TrigramSearchManager to ChaloBEST models. --- chaloBEST/mumbai/models.py | 21 +++++++++++++++++++++ gateway/settings.py | 3 +-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index dcae8e9..e5e81a4 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -42,7 +42,27 @@ SCHED = { '2nd &4th':['???'] } +class TrigramSearchManager(models.Manager): + def set_threshold(self, threshold): + """Set the limit for trigram similarity matching.""" + cursor = connection.cursor() + cursor.execute("""SELECT set_limit(%f)""" % threshold) + + def find_approximate(self, match=0.5, **kwargs): + self.set_threshold(match) + assert(len(kwargs) == 1) + column, value = kwargs.items()[0] + qset = self.get_query_set() + # use the pg_trgm index via the % operator + qset = qset.extra(select={"similarity":"similarity(" + column + ", %s)"}, + select_params=[value], + where=[column + " %% %s"], + params=[value], + order_by=["-similarity"]) + return qset + class Area(models.Model): + objects = TrigramSearchManager() # name, name_mr code = models.IntegerField() #primary_key=True) slug = models.SlugField(null=True) name = models.TextField(blank=True, max_length=255) @@ -93,6 +113,7 @@ class Fare(models.Model): class Stop(models.Model): + objects = TrigramSearchManager() # name, display, name_mr code = models.IntegerField() slug = models.SlugField(null=True) name = models.TextField(blank=True, max_length=255) diff --git a/gateway/settings.py b/gateway/settings.py index 5cef581..ce79173 100755 --- a/gateway/settings.py +++ b/gateway/settings.py @@ -2,8 +2,6 @@ # vim: ai ts=4 sts=4 et sw=4 # encoding=utf-8 -# Put this in /srv/smsBEST/gateway and change the gateway secret. - # -------------------------------------------------------------------- # # MAIN CONFIGURATION # # -------------------------------------------------------------------- # @@ -51,6 +49,7 @@ GATEWAY = { "push": "http://chalobest.in:8086/?from=%(from)s&txt=%(txt)s&secret=%(secret)s" } +AJAX_PROXY_HOST = "0.0.0.0" # to open the gateway from the outside # to help you get started quickly, many django/rapidsms apps are enabled # by default. you may wish to remove some and/or add your own.