This commit is contained in:
Johnson Chetty 2012-02-22 09:24:59 +01:00
commit 8401078ab0
5 changed files with 170 additions and 38 deletions

View File

@ -1,13 +1,21 @@
from django.template.defaultfilters import slugify
from mumbai.models import *
def do():
for cls in [Road, Area, Stop]:
slugIncrements = {}
# theseSlugs = []
for obj in cls.objects.all():
slug = slugify(obj.display_name)
if cls.objects.filter(slug=slug).count() > 1:
slug += "2"
obj.slug = slug
if slug in slugIncrements:
slugIncrements[slug] += 1
finalSlug = slug + "_" + str(slugIncrements[slug])
else:
slugIncrements.update({slug: 1})
finalSlug = slug
obj.slug = finalSlug
obj.save()
for r in Route.objects.all():
r.slug = r.alias

View File

@ -1,7 +1,8 @@
from models import *
from ox.django.shortcuts import get_object_or_404_json, render_to_json_response
from django.contrib.auth.decorators import login_required
import json
from django.views.decorators.csrf import csrf_exempt
def route(request, slug):
srid = int(request.GET.get("srid", 4326))
@ -56,15 +57,16 @@ def stops(request):
})
@csrf_exempt
def stop(request, slug):
if request.POST:
srid = int(request.GET.get("srid", 4326))
if request.POST and request.POST.has_key('geojson'):
if not slug:
stop = Stop() #FIXME: should this return an error instead?
else:
stop = get_object_or_404_json(Stop, slug=slug)
return render_to_json_response(stop.from_geojson(request.POST))
geojson = json.loads(request.POST['geojson'])
return render_to_json_response(stop.from_geojson(geojson, srid))
else:
stop = get_object_or_404_json(Stop, slug=slug)
srid = int(request.GET.get("srid", 4326))
stop = get_object_or_404_json(Stop, slug=slug)
return render_to_json_response(stop.get_geojson(srid=srid)) #FIXME: please don't repeat this code, its retarded.

View File

@ -136,10 +136,10 @@ class Stop(models.Model):
'geometry': geom
}
def from_geojson(self, geojson):
def from_geojson(self, geojson, srid=4326):
geom = geojson['geometry']['coordinates']
data = geojson['properties']
self.point = Point(geom[0], geom[1])
self.point = Point(geom[0], geom[1], srid=srid).transform(4326, True) #FIXME: srid should be passed as param
self.display_name = data['display_name']
self.name_mr = data['name_mr']
if data.has_key('alternative_names') and data['alternative_names'].strip() != '':
@ -154,7 +154,7 @@ class Stop(models.Model):
#FIXME: add alt names logic
self.save()
return self.get_geojson()
return self.get_geojson(srid=srid)
def __unicode__(self):

View File

@ -43,10 +43,17 @@ var API_BASE = "/1.0/",
if ($target.data("loading")) {
return;
}
$('.selectedListItem').find(".stopsList").hide();
$('.selectedListItem').removeClass("selectedListItem");
$target.addClass("selectedListItem");
if ($target.data("hasList")) {
$target.find(".stopsList").toggle();
var $stopsList = $target.find(".stopsList");
if (!$stopsList.is(":visible")) {
$stopsList.slideDown();
} else {
$stopsList.slideUp();
$target.removeClass("selectedListItem");
}
return;
}
var url = API_BASE + name + "/" + $target.find(".listItemText").text();
@ -100,11 +107,19 @@ var API_BASE = "/1.0/",
if ($target.hasClass("selectedStop")) {
return;
}
$('.selectedStop').removeClass("selectedStop");
$target.addClass("selectedStop");
// $('.selectedStop').removeClass("selectedStop");
// $target.addClass("selectedStop");
var props = $target.data("properties");
var $form = getStopForm(props);
$('#formCol').empty();
var geom = $target.data("geometry");
var $form = getStopForm(props, geom);
var slug = $target.data("slug");
if ($target.hasClass("has_point")) {
selectStopOnMap(slug);
} else {
selectStopNotOnMap(slug);
}
$('#formCol').find("#stopForm").remove();
$('#formCol').append($form);
});
@ -115,25 +130,68 @@ var API_BASE = "/1.0/",
.addClass("stopItem")
.data("slug", props.slug)
.addClass(props.slug) //FIXME: please dont set data AND addClass AND include slug in properties.
.data("properties", props).data("geometry", geom)
.data("properties", props)
.data("geometry", geom)
.text(props.display_name)
.hover(function() {
//TODO: when hover over a stop name in list, it should set some styleMap stuff on the map to colour the moused-over location.
}, function() {
})
.appendTo($ul);
$.isEmptyObject(geom) ? $li.addClass("no_has_point") : $li.addClass("has_point");
});
return $ul;
}
function getStopForm(stop) {
function getStopForm(stop, geom) {
// console.log(stop);
console.log(geom);
var lon = !$.isEmptyObject(geom) ? geom.coordinates[0] : '';
var lat = !$.isEmptyObject(geom) ? geom.coordinates[1] : '';
var $div = $('<div />');
var $displayName = $('<div />').text(stop.display_name).appendTo($div);
var $routes = $('<div />').text("Routes: " + stop.routes).appendTo($div);
// var $form = $('<form />').apendTo($div);
var $form = $('<form />').attr("id", "stopForm").appendTo($div);
var $display_name_input = $('<input />')
.val(stop.display_name)
.attr("id", "displayName")
.blur(function() {
$form.submit();
})
.appendTo($form);
var $name_mr_input = $('<input />')
.val(stop.name_mr)
.attr("id", "displayNameMr")
.blur(function() {
$form.submit();
})
.appendTo($form);
var $alt_names_input = $('<input />')
.val(stop.alternative_names)
.attr("id", "altNames")
.blur(function() {
$form.submit();
})
.appendTo($form);
var $lat_input = $('<input />').attr("type", "hidden").val(lat).attr("id", "lat").appendTo($form);
var $lon_input = $('<input />').attr("type", "hidden").val(lon).attr("id", "lon").appendTo($form);
$form.submit(function(e) {
e.preventDefault();
var geojson = {
'type': 'Feature',
'properties': {
'display_name': $display_name_input.val(),
'name_mr': $name_mr_input.val(),
'alternative_names': $alt_names_input.val()
},
'geometry': {
'type': 'Point',
'coordinates': [parseFloat($lon_input.val()), parseFloat($lat_input.val())]
}
};
var geojsonString = JSON.stringify(geojson);
//console.log(geojsonString);
var url = API_BASE + "stop/" + stop.slug + "?srid=3857";
$.post(url, {'geojson': geojsonString}, function(response) {
console.log(response);
});
});
return $div;
}
@ -149,8 +207,8 @@ var API_BASE = "/1.0/",
geojson_format = new OpenLayers.Format.GeoJSON();
//yes, jsonLayer is global. Yes, I know it's wrong.
jsonLayer = layers[1] = new OpenLayers.Layer.Vector({
geometryType: 'Point',
projection: new OpenLayers.Projection("EPSG:4326")
geometryType: 'Point'
// projection: new OpenLayers.Projection("EPSG:4326")
});
// map.addLayer(vector_layer);
map.addLayers(layers);
@ -168,18 +226,79 @@ var API_BASE = "/1.0/",
});
}
function onFeatureSelect(feature) {
var slug = feature.attributes.slug;
alert("selected " + slug);
var matchedStops = $('.' + slug);
matchedStops.addClass('highlightedStop');
function onFeatureSelect(e) {
//alert(arguments);
//console.log(feature);
var slug = e.feature.attributes.slug;
//alert("selected " + slug);
highlightStop(slug);
// var matchedStops = $('.' + slug);
// matchedStops.click();
// matchedStops.addClass('highlightedStop');
}
function onFeatureUnselect(feature) {
var slug = feature.attributes.slug;
alert("unselected " + slug);
var matchedStops = $('.' + slug);
matchedStops.removeClass('highlightedStop');
function onFeatureUnselect(e) {
var slug = e.feature.attributes.slug;
//alert("unselected " + slug);
unhighlightStop(slug);
// var matchedStops = $('.' + slug);
// matchedStops.removeClass('selectedStop');
}
function highlightStop(slug) {
$('.' + slug).addClass("selectedStop");
}
function unhighlightStop(slug) {
$('.' + slug).removeClass("selectedStop");
}
function getStopFromSlug(slug) {
var features = jsonLayer.features;
var matchedLayer = false;
for (var i=0; i<features.length; i++) {
var feature = features[i];
if (feature.attributes.slug === slug) {
matchedLayer = feature;
}
}
return matchedLayer;
}
function selectStopNotOnMap(slug) {
var selectedFeature = getCurrentlySelectedFeature();
if (selectedFeature) {
var currentSlug = selectedFeature.attributes.slug;
unhighlightStop(slug);
mapControl.unselect(selectedFeature);
} else {
$('.selectedStop').removeClass("selectedStop");
}
highlightStop(slug);
}
function selectStopOnMap(slug) {
var feature = getStopFromSlug(slug);
var selectedFeature = getCurrentlySelectedFeature();
if (selectedFeature) {
mapControl.unselect(selectedFeature);
} else {
$('.selectedStop').removeClass("selectedStop");
}
mapControl.select(feature);
}
//return currently selected feature or false
function getCurrentlySelectedFeature() {
var selectedFeatures = jsonLayer.selectedFeatures;
if (selectedFeatures.length > 0) {
return jsonLayer.selectedFeatures[0];
// mapControl.unselect(selectedFeature);
} else {
return false;
}
}
})();

View File

@ -3,8 +3,11 @@
{% block head %}
<link rel="stylesheet" href="/static/css/editstops.css" />
<script type="text/javascript" src="/static/js/editstops.js"></script>
<script type="text/javascript">
console.firebug=true;//fix the openlayer problem
</script>
<script type="text/javascript" src="http://openlayers.org/dev/OpenLayers.js"></script>
<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyBbz_Bjqtf5U9qGZw4xoS5PxLDadHsC0AA'></script>
<!-- <script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyBbz_Bjqtf5U9qGZw4xoS5PxLDadHsC0AA'></script> -->
{% endblock %}