submit fixed.

This commit is contained in:
Karen 2011-08-31 17:56:56 +05:30
commit 81f2fe513b

View File

@ -3,6 +3,7 @@ from django.shortcuts import render_to_response, get_object_or_404
from ox.django.shortcuts import render_to_json_response from ox.django.shortcuts import render_to_json_response
from django.template import RequestContext from django.template import RequestContext
from models import Feature, FeatureType, AuthorityRecord from models import Feature, FeatureType, AuthorityRecord
from django.core.paginator import Paginator, InvalidPage, EmptyPage
def search(request): def search(request):
d = RequestContext(request, {}) d = RequestContext(request, {})
@ -12,6 +13,8 @@ def search_json(request):
search_term = request.GET.get("q", "") search_term = request.GET.get("q", "")
threshold = request.GET.get("threshold", 0.5) threshold = request.GET.get("threshold", 0.5)
bbox = request.GET.get("bbox", False) bbox = request.GET.get("bbox", False)
page = request.GET.get("page", 1)
count = request.GET.get("count", 100)
country = request.GET.get("adm0", "US") # right now, unused country = request.GET.get("adm0", "US") # right now, unused
adm1 = request.GET.get("adm1", "") adm1 = request.GET.get("adm1", "")
adm2 = request.GET.get("adm2", "") adm2 = request.GET.get("adm2", "")
@ -33,10 +36,22 @@ def search_json(request):
if not bbox and not search_term: if not bbox and not search_term:
return render_to_json_response({'error': 'must supply either a valid `bbox` or a `search` parameter'}) return render_to_json_response({'error': 'must supply either a valid `bbox` or a `search` parameter'})
features_qset = Feature.search.find(bbox=bbox, text=search_term, threshold=threshold, adm1=adm1, adm2=adm2, srid=srid)[0:20] features_qset = Feature.search.find(bbox=bbox, text=search_term, threshold=threshold, adm1=adm1, adm2=adm2, srid=srid)
features = [f.get_geojson(srid) for f in features_qset] total_results = features_qset.count()
paginator = Paginator(features_qset, count)
num_pages = paginator.num_pages
try:
results = paginator.page(page)
except (EmptyPage, InvalidPage):
results = paginator.page(paginator.num_pages)
features = [f.get_geojson(srid) for f in results.object_list]
d = { d = {
'type': 'FeatureCollection', 'type': 'FeatureCollection',
'results': total_results,
'current_page': page,
'pages': num_pages,
'features': features 'features': features
} }
return render_to_json_response(d) return render_to_json_response(d)