merged with sanj..

This commit is contained in:
Johnson Chetty 2012-10-05 13:59:54 +02:00
commit 9d565b191d
2 changed files with 11 additions and 4 deletions

View File

@ -66,14 +66,20 @@ def areas(request):
def stops(request): def stops(request):
q = request.GET.get("q", "") q = request.GET.get("q", "")
ctype = ContentType.objects.get_for_model(Stop)
stops = []
if q != '': 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: else:
qset = Stop.objects.all() stops = Stop.objects.all()
srid = int(request.GET.get("srid", 4326)) srid = int(request.GET.get("srid", 4326))
return render_to_json_response({ return render_to_json_response({
'type': 'FeatureCollection', 'type': 'FeatureCollection',
'features': [stop.get_geojson(srid=srid) for stop in qset] 'features': [stop.get_geojson(srid=srid) for stop in stops]
}) })

View File

@ -180,7 +180,7 @@ class Stop(models.Model):
'name_mr': self.name_mr, 'name_mr': self.name_mr,
'direction': self.dbdirection, 'direction': self.dbdirection,
'routes': ", ".join([r.alias for r in routes]), '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() 'url': self.get_absolute_url()
} }
@ -427,6 +427,7 @@ ALT_TYPE_CHOICES = (
) )
class AlternativeName(models.Model): class AlternativeName(models.Model):
objects = TrigramSearchManager(('name',))
name = models.CharField(max_length=512) name = models.CharField(max_length=512)
name_mr = models.CharField(max_length=512, blank=True) name_mr = models.CharField(max_length=512, blank=True)
typ = models.CharField(max_length=64, choices=ALT_TYPE_CHOICES, default="alt") typ = models.CharField(max_length=64, choices=ALT_TYPE_CHOICES, default="alt")