summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@proxience.com>2015-02-22 20:05:53 +0100
committerÉtienne Loks <etienne.loks@proxience.com>2015-02-22 20:05:53 +0100
commita50175acb194390a4462c6db0945e075aefb2299 (patch)
tree79349549c0cce4c137f82a02de906cafa1849abc
parent6c9f991625e5c10e8f88e5af84ddb542d1444237 (diff)
downloadChimère-a50175acb194390a4462c6db0945e075aefb2299.tar.bz2
Chimère-a50175acb194390a4462c6db0945e075aefb2299.zip
Cleaner (and safer) way to generate JSON - add a get-marker view - allow to display only one marker (and no category) with permalinks
-rw-r--r--chimere/models.py55
-rw-r--r--chimere/static/chimere/js/jquery.chimere.js28
-rw-r--r--chimere/templates/chimere/main_map_simple.html2
-rw-r--r--chimere/templates/search/search_js.html9
-rw-r--r--chimere/urls.py2
-rw-r--r--chimere/views.py20
6 files changed, 73 insertions, 43 deletions
diff --git a/chimere/models.py b/chimere/models.py
index caddefc..2ae8481 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -20,7 +20,7 @@
"""
Models description
"""
-import os, datetime, pyexiv2, re, string
+import os, datetime, pyexiv2, re, string, copy
import simplejson as json
from lxml import etree
from PIL import Image
@@ -660,37 +660,27 @@ class Marker(GeographicItem):
'''Return a GeoJSON string
'''
jsons = []
- full_json = u'{"type":"Feature", "geometry":%(geometry)s, '\
- u'"properties":{"pk": %(id)d, "name": %(name)s, '\
- u'"icon_path":"%(icon_path)s", '\
- u'"icon_hover_path":"%(icon_hover_path)s", '\
- u'"icon_width":%(icon_width)d, '\
- u'"icon_height":%(icon_height)d, '\
- u'"category_name":%(category_name)s}}'
- light_json = u'{"type":"Feature", "geometry":%(geometry)s, '\
- u'"properties":{"pk": %(id)d, "name": %(name)s, '\
- u'"icon_path":"%(icon_path)s", '\
- u'"icon_hover_path":"%(icon_hover_path)s", '\
- u'"category_name":%(category_name)s}}'
+ json_tpl = {"type":"Feature", "properties":{}}
for cat in self.categories.all():
if categories_id and cat.id not in categories_id:
continue
- items = {'id':self.id, 'name':json.dumps(self.name),
- 'geometry':self.point.geojson,
- 'icon_path':cat.icon.image,
+ items = copy.deepcopy(json_tpl)
+ items['geometry'] = json.loads(self.point.geojson)
+ items['properties'].update({'pk':self.id,
+ 'name':self.name,
+ 'icon_path':unicode(cat.icon.image),
'icon_hover_path':cat.hover_icon.image \
if cat.hover_icon else '',
- 'category_name':json.dumps(cat.name)}
+ 'category_name':cat.name})
try:
- items.update({'icon_width':cat.icon.image.width,
- 'icon_height':cat.icon.image.height,})
- cjson = full_json % items
+ items['properties'].update({'icon_width':cat.icon.image.width,
+ 'icon_height':cat.icon.image.height,})
except IOError:
- cjson = light_json % items
+ pass
- jsons.append(cjson)
+ jsons.append(items)
- return ",".join(jsons)
+ return json.dumps(jsons)
@property
def default_category(self):
@@ -1156,11 +1146,11 @@ class Route(GeographicItem):
'''
if '#' not in color:
color = '#' + color
- attributes = {'id':self.id, 'name':json.dumps(self.name),
- 'color':color, 'geometry':self.route.geojson,}
- return u'{"type":"Feature", "geometry":%(geometry)s, '\
- u'"properties":{"pk": %(id)d, "name": %(name)s, '\
- u'"color":"%(color)s"}}' % attributes
+ attributes = {"type":"Feature",
+ "geometry":json.loads(self.route.geojson),
+ "properties":{"pk":self.id, "name":self.name,
+ "color":color}}
+ return json.dumps(attributes)
def getTinyUrl(self):
parameters = 'current_feature=%d&checked_categories=%s' % (self.id,
@@ -1255,11 +1245,10 @@ class AggregatedRoute(models.Model):
'''
if '#' not in color:
color = '#' + color
- attributes = {'id':self.id, 'name':json.dumps(u'Aggregated route'),
- 'color':color, 'geometry':self.route.geojson,}
- return u'{"type":"Feature", "geometry":%(geometry)s, '\
- u'"properties":{"pk": %(id)d, "name": %(name)s, '\
- u'"color":"%(color)s"}}' % attributes
+ attributes = {'color':color, 'geometry':json.loads(self.route.geojson),
+ 'type':"Feature", "properties":{"pk": self.id,
+ "name": u'Aggregated route',}}
+ return json.dumps(attributes)
class SimplePoint:
"""
diff --git a/chimere/static/chimere/js/jquery.chimere.js b/chimere/static/chimere/js/jquery.chimere.js
index 9eaf4a0..77c27b0 100644
--- a/chimere/static/chimere/js/jquery.chimere.js
+++ b/chimere/static/chimere/js/jquery.chimere.js
@@ -441,6 +441,22 @@ OpenLayers.Layer.MapQuestOSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
methods.routingAddStep();
}
}
+
+ // verify that the initial display_feature is displayed
+ if (settings.display_feature){
+ var is_displayed = false;
+ for(j=0; j<settings.layerMarkers.markers.length;j++){
+ var c_marker = settings.layerMarkers.markers[j];
+ if(c_marker.pk == settings.display_feature){
+ is_displayed = true;
+ }
+ }
+ if (!is_displayed){
+ methods.loadMarker(settings.display_feature);
+ }
+ }
+
+
methods.preload_images();
}, // end of init
/* Preload icons */
@@ -581,6 +597,15 @@ OpenLayers.Layer.MapQuestOSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
$('#chimere_map_menu').css('left', offsetX);
}
},
+ loadMarker: function(object_id) {
+ var uri = extra_url + "get-marker/" + object_id;
+ $.ajax({url: uri,
+ dataType: "json",
+ success: function (data) {
+ for (idx in data) methods.addMarker(data[idx]);
+ }
+ });
+ },
/*
* Load markers and route from DB
*/
@@ -1567,9 +1592,10 @@ OpenLayers.Layer.MapQuestOSM = OpenLayers.Class(OpenLayers.Layer.XYZ, {
var c_marker = settings.layerMarkers.markers[j];
if(c_marker.pk == feature_pk){
c_marker.events.triggerEvent('click');
- return
+ return true
}
}
+ return false;
//feature.markerClick();
//OpenLayers.Popup.popupSelect.clickFeature(feature);
/*
diff --git a/chimere/templates/chimere/main_map_simple.html b/chimere/templates/chimere/main_map_simple.html
index 4a1b603..4e93f8c 100644
--- a/chimere/templates/chimere/main_map_simple.html
+++ b/chimere/templates/chimere/main_map_simple.html
@@ -7,7 +7,7 @@
{% block sidebar %}
<div id='panel'>
<a href='#' onclick='showHide("categories")'>
- <h2>{% trans "Categories"%}</h2>
+ <h2 class='btn'>{% trans "Categories"%}</h2>
</a>
<form method='post' name='frm_categories' id='frm_categories'>
<div id='categories' name='categories'></div>
diff --git a/chimere/templates/search/search_js.html b/chimere/templates/search/search_js.html
index 25edca6..c8d9812 100644
--- a/chimere/templates/search/search_js.html
+++ b/chimere/templates/search/search_js.html
@@ -4,7 +4,11 @@ $(function(){
$('.subcategory').each(function(){ $(this).removeClass('selected'); });
$('.subcategory input[type=checkbox]').attr('checked', false);
- var geo_objects = [{% for result in page.object_list %}{{result.object.getGeoJSON|safe}}{% if not forloop.last %}, {% endif %}{% endfor %}];
+ var geo_objects = [];
+ {% for result in page.object_list %}var c_lst ={{result.object.getGeoJSON|safe}};
+ for (idx in c_lst){
+ geo_objects.push(c_lst[idx]);
+ }{% endfor %}
var geo_features = {};
for (idx=0 ; idx < geo_objects.length ; idx++){
var c_idx = geo_objects[idx].properties.pk;
@@ -14,8 +18,7 @@ $(function(){
geo_objects[idx]);
}
}
- {% if page.object_list.count %}$("#main-map").chimere("zoomToMarkerExtent");{% endif %}
-
+ window.setTimeout(function(){$("#main-map").chimere("zoomToMarkerExtent")}, 500);
});
</script>
diff --git a/chimere/urls.py b/chimere/urls.py
index f67662b..28a7098 100644
--- a/chimere/urls.py
+++ b/chimere/urls.py
@@ -103,6 +103,8 @@ urlpatterns += patterns('chimere.views',
url(r'^(?P<area_name>[a-zA-Z0-9_-]+/)?getGeoObjects/'\
r'(?P<category_ids>[a-zA-Z0-9_-]+)(/(?P<status>\w+))?$', 'getGeoObjects',
name="getgeoobjects"),
+ url(r'^(?P<area_name>[a-zA-Z0-9_-]+/)?get-marker/'\
+ r'(?P<pk>[0-9]+)$', 'getMarker', name="get-marker"),
url(r'^(?P<area_name>[a-zA-Z0-9_-]+/)?getAvailableCategories/$',
'get_available_categories', name="get_categories"),
url(r'^(?P<area_name>[a-zA-Z0-9_-]+/)?getAllCategories/$',
diff --git a/chimere/views.py b/chimere/views.py
index 8daa432..5beb5fd 100644
--- a/chimere/views.py
+++ b/chimere/views.py
@@ -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
@@ -617,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))
@@ -627,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):
'''