edit metadata works

This commit is contained in:
Sanj 2012-02-22 12:49:35 +05:30
parent f50124f634
commit d9f6d90eea
5 changed files with 170 additions and 38 deletions

View File

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

View File

@ -1,7 +1,8 @@
from models import * from models import *
from ox.django.shortcuts import get_object_or_404_json, render_to_json_response from ox.django.shortcuts import get_object_or_404_json, render_to_json_response
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
import json
from django.views.decorators.csrf import csrf_exempt
def route(request, slug): def route(request, slug):
srid = int(request.GET.get("srid", 4326)) srid = int(request.GET.get("srid", 4326))
@ -56,15 +57,16 @@ def stops(request):
}) })
@csrf_exempt
def stop(request, slug): 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: if not slug:
stop = Stop() #FIXME: should this return an error instead? stop = Stop() #FIXME: should this return an error instead?
else: else:
stop = get_object_or_404_json(Stop, slug=slug) 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: else:
stop = get_object_or_404_json(Stop, slug=slug) stop = get_object_or_404_json(Stop, slug=slug)
srid = int(request.GET.get("srid", 4326))
return render_to_json_response(stop.get_geojson(srid=srid)) #FIXME: please don't repeat this code, its retarded. 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 'geometry': geom
} }
def from_geojson(self, geojson): def from_geojson(self, geojson, srid=4326):
geom = geojson['geometry']['coordinates'] geom = geojson['geometry']['coordinates']
data = geojson['properties'] 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.display_name = data['display_name']
self.name_mr = data['name_mr'] self.name_mr = data['name_mr']
if data.has_key('alternative_names') and data['alternative_names'].strip() != '': if data.has_key('alternative_names') and data['alternative_names'].strip() != '':
@ -154,7 +154,7 @@ class Stop(models.Model):
#FIXME: add alt names logic #FIXME: add alt names logic
self.save() self.save()
return self.get_geojson() return self.get_geojson(srid=srid)
def __unicode__(self): def __unicode__(self):

View File

@ -43,10 +43,17 @@ var API_BASE = "/1.0/",
if ($target.data("loading")) { if ($target.data("loading")) {
return; return;
} }
$('.selectedListItem').find(".stopsList").hide();
$('.selectedListItem').removeClass("selectedListItem"); $('.selectedListItem').removeClass("selectedListItem");
$target.addClass("selectedListItem"); $target.addClass("selectedListItem");
if ($target.data("hasList")) { 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; return;
} }
var url = API_BASE + name + "/" + $target.find(".listItemText").text(); var url = API_BASE + name + "/" + $target.find(".listItemText").text();
@ -100,11 +107,19 @@ var API_BASE = "/1.0/",
if ($target.hasClass("selectedStop")) { if ($target.hasClass("selectedStop")) {
return; return;
} }
$('.selectedStop').removeClass("selectedStop"); // $('.selectedStop').removeClass("selectedStop");
$target.addClass("selectedStop"); // $target.addClass("selectedStop");
var props = $target.data("properties"); var props = $target.data("properties");
var $form = getStopForm(props); var geom = $target.data("geometry");
$('#formCol').empty(); 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); $('#formCol').append($form);
}); });
@ -115,25 +130,68 @@ var API_BASE = "/1.0/",
.addClass("stopItem") .addClass("stopItem")
.data("slug", props.slug) .data("slug", props.slug)
.addClass(props.slug) //FIXME: please dont set data AND addClass AND include slug in properties. .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) .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); .appendTo($ul);
$.isEmptyObject(geom) ? $li.addClass("no_has_point") : $li.addClass("has_point"); $.isEmptyObject(geom) ? $li.addClass("no_has_point") : $li.addClass("has_point");
}); });
return $ul; return $ul;
} }
function getStopForm(stop) { function getStopForm(stop, geom) {
// console.log(stop); // console.log(stop);
console.log(geom);
var lon = !$.isEmptyObject(geom) ? geom.coordinates[0] : '';
var lat = !$.isEmptyObject(geom) ? geom.coordinates[1] : '';
var $div = $('<div />'); var $div = $('<div />');
var $displayName = $('<div />').text(stop.display_name).appendTo($div); var $displayName = $('<div />').text(stop.display_name).appendTo($div);
var $routes = $('<div />').text("Routes: " + stop.routes).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; return $div;
} }
@ -149,8 +207,8 @@ var API_BASE = "/1.0/",
geojson_format = new OpenLayers.Format.GeoJSON(); geojson_format = new OpenLayers.Format.GeoJSON();
//yes, jsonLayer is global. Yes, I know it's wrong. //yes, jsonLayer is global. Yes, I know it's wrong.
jsonLayer = layers[1] = new OpenLayers.Layer.Vector({ jsonLayer = layers[1] = new OpenLayers.Layer.Vector({
geometryType: 'Point', geometryType: 'Point'
projection: new OpenLayers.Projection("EPSG:4326") // projection: new OpenLayers.Projection("EPSG:4326")
}); });
// map.addLayer(vector_layer); // map.addLayer(vector_layer);
map.addLayers(layers); map.addLayers(layers);
@ -168,18 +226,79 @@ var API_BASE = "/1.0/",
}); });
} }
function onFeatureSelect(feature) { function onFeatureSelect(e) {
var slug = feature.attributes.slug; //alert(arguments);
alert("selected " + slug); //console.log(feature);
var matchedStops = $('.' + slug); var slug = e.feature.attributes.slug;
matchedStops.addClass('highlightedStop'); //alert("selected " + slug);
highlightStop(slug);
// var matchedStops = $('.' + slug);
// matchedStops.click();
// matchedStops.addClass('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;
} }
function onFeatureUnselect(feature) {
var slug = feature.attributes.slug;
alert("unselected " + slug);
var matchedStops = $('.' + slug);
matchedStops.removeClass('highlightedStop');
} }
})(); })();

View File

@ -3,8 +3,11 @@
{% block head %} {% block head %}
<link rel="stylesheet" href="/static/css/editstops.css" /> <link rel="stylesheet" href="/static/css/editstops.css" />
<script type="text/javascript" src="/static/js/editstops.js"></script> <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 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 %} {% endblock %}