diff options
Diffstat (limited to 'chimere/views.py')
-rw-r--r-- | chimere/views.py | 53 |
1 files changed, 46 insertions, 7 deletions
diff --git a/chimere/views.py b/chimere/views.py index a417fad..5beb5fd 100644 --- a/chimere/views.py +++ b/chimere/views.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2008-2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2008-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # # RSS : Copyright (C) 2010 Pierre Clarenc <pierre.crc_AT_gmailDOTcom>, # Samuel Renard <renard.samuel_AT_gmailDOTcom>, @@ -27,6 +27,7 @@ Views of the project import datetime from itertools import groupby import re +import simplejson as json from django.conf import settings from django.contrib.gis.geos import GEOSGeometry @@ -175,13 +176,15 @@ def index(request, area_name=None, default_area=None, simple=False, 'actions':actions(response_dct['area_name']), 'action_selected':('view',), 'error_message':'', + 'is_map':True, 'news_visible': news_visible, 'areas_visible': settings.CHIMERE_DISPLAY_AREAS, 'map_layer':settings.CHIMERE_DEFAULT_MAP_LAYER, 'dynamic_categories':response_dct['dynamic_categories'], 'zoomout':zoomout, 'has_default_area':Area.objects.filter(default=True).count(), - 'zoomout':zoomout + 'zoomout':zoomout, + 'has_search':settings.CHIMERE_SEARCH_ENGINE }) if hasattr(settings, 'CONTACT_EMAIL') and settings.CONTACT_EMAIL: response_dct['contact_email'] = settings.CONTACT_EMAIL @@ -615,9 +618,10 @@ def getGeoObjects(request, area_name, category_ids, status): current_cat = c_cat colors = list(Color.objects.filter(color_theme = c_cat.color_theme)) if colors: - jsons.append(route.getGeoJSON(color=colors[idx % len(colors)].code)) + jsons.append(json.loads( + route.getGeoJSON(color=colors[idx % len(colors)].code))) else: - jsons.append(route.getGeoJSON(color='000')) + jsons.append(json.loads(route.getGeoJSON(color='000'))) idx += 1 try: q = checkDate(Q(status__in=status, categories__in=category_ids)) @@ -625,11 +629,19 @@ def getGeoObjects(request, area_name, category_ids, status): except: return HttpResponse('no results') category_ids = [int(cat_id) for cat_id in category_ids] - jsons += [geo_object.getGeoJSON(category_ids) for geo_object in list(query)] + for geo_object in list(query): + jsons += json.loads(geo_object.getGeoJSON(category_ids)) if not jsons: return HttpResponse('no results') - data = '{"type": "FeatureCollection", "features":[%s]}' % ",".join(jsons) - return HttpResponse(data) + data = json.dumps({"type": "FeatureCollection", "features":jsons}) + return HttpResponse(data, content_type="application/json") + +def getMarker(request, area_name, pk): + q = Marker.objects.filter(pk=pk, status='A') + if not q.count(): + return HttpResponse('{}') + data = q.all()[0].getGeoJSON() + return HttpResponse(data, content_type="application/json") def get_all_categories(request, area_name=None): ''' @@ -945,3 +957,30 @@ def rss(request, area_name=''): else: return render_to_response('chimere/feeds/rss.html', response_dct, context_instance=RequestContext(request)) + +from django.core.paginator import Paginator, InvalidPage + +SearchView = None +autocomplete = None +if hasattr(settings, 'CHIMERE_SEARCH_ENGINE') \ + and settings.CHIMERE_SEARCH_ENGINE: + from haystack.views import SearchView as HaystackSearchView + from haystack.query import SearchQuerySet + class SearchView(HaystackSearchView): + pass + def autocomplete(request): + sqs = SearchQuerySet().autocomplete( + content_auto=request.GET.get('q', ''))[:5] + suggestions = [result.object.name for result in sqs if result.object] + spelling = [] + if not suggestions: + spelling = SearchQuerySet().spelling_suggestion( + request.GET.get('q', '')) or [] + # convert to list spelling... + # make sure it returns a JSON object, not a bare list. + # otherwise, it could be vulnerable to an XSS attack. + the_data = json.dumps({ + 'results': suggestions, + 'spelling':spelling, + }) + return HttpResponse(the_data, content_type='application/json') |