From d9f6d90eea5ae694d67185e901a3af53cb972eb0 Mon Sep 17 00:00:00 2001 From: Sanj Date: Wed, 22 Feb 2012 12:49:35 +0530 Subject: [PATCH] edit metadata works --- chaloBEST/imports/make_slugs.py | 14 ++- chaloBEST/mumbai/apiviews.py | 14 ++- chaloBEST/mumbai/models.py | 6 +- chaloBEST/static/js/editstops.js | 169 ++++++++++++++++++++++++----- chaloBEST/templates/editstops.html | 5 +- 5 files changed, 170 insertions(+), 38 deletions(-) diff --git a/chaloBEST/imports/make_slugs.py b/chaloBEST/imports/make_slugs.py index 3ef2daa..05b02b8 100644 --- a/chaloBEST/imports/make_slugs.py +++ b/chaloBEST/imports/make_slugs.py @@ -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 diff --git a/chaloBEST/mumbai/apiviews.py b/chaloBEST/mumbai/apiviews.py index b9c0eba..a20999b 100644 --- a/chaloBEST/mumbai/apiviews.py +++ b/chaloBEST/mumbai/apiviews.py @@ -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. diff --git a/chaloBEST/mumbai/models.py b/chaloBEST/mumbai/models.py index f3d9b7b..3e8cc10 100644 --- a/chaloBEST/mumbai/models.py +++ b/chaloBEST/mumbai/models.py @@ -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): diff --git a/chaloBEST/static/js/editstops.js b/chaloBEST/static/js/editstops.js index fd011ed..72ac626 100644 --- a/chaloBEST/static/js/editstops.js +++ b/chaloBEST/static/js/editstops.js @@ -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 = $('
'); var $displayName = $('
').text(stop.display_name).appendTo($div); var $routes = $('
').text("Routes: " + stop.routes).appendTo($div); - // var $form = $('
').apendTo($div); + var $form = $('').attr("id", "stopForm").appendTo($div); + var $display_name_input = $('') + .val(stop.display_name) + .attr("id", "displayName") + .blur(function() { + $form.submit(); + }) + .appendTo($form); + var $name_mr_input = $('') + .val(stop.name_mr) + .attr("id", "displayNameMr") + .blur(function() { + $form.submit(); + }) + .appendTo($form); + var $alt_names_input = $('') + .val(stop.alternative_names) + .attr("id", "altNames") + .blur(function() { + $form.submit(); + }) + .appendTo($form); + var $lat_input = $('').attr("type", "hidden").val(lat).attr("id", "lat").appendTo($form); + var $lon_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 0) { + return jsonLayer.selectedFeatures[0]; + // mapControl.unselect(selectedFeature); + } else { + return false; + } + } })(); diff --git a/chaloBEST/templates/editstops.html b/chaloBEST/templates/editstops.html index 0583921..d981826 100644 --- a/chaloBEST/templates/editstops.html +++ b/chaloBEST/templates/editstops.html @@ -3,8 +3,11 @@ {% block head %} + - + {% endblock %}