From 2b7a40069f7af2e72c81f9488411135f286515f9 Mon Sep 17 00:00:00 2001 From: Shaun McDonald Date: Wed, 8 Oct 2008 18:03:41 +0000 Subject: [PATCH] adding the controller code to be able to get a specific version of a node, way or relation. Add some node checks on update. Some code indentation cleanup. --- app/controllers/old_node_controller.rb | 15 +++++++++++++++ app/controllers/old_relation_controller.rb | 16 ++++++++++++++++ app/controllers/old_way_controller.rb | 15 +++++++++++++++ app/controllers/relation_controller.rb | 1 + app/models/node.rb | 17 ++++++++++------- app/models/relation.rb | 2 +- lib/osm.rb | 2 +- 7 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/controllers/old_node_controller.rb b/app/controllers/old_node_controller.rb index e27898336..8c43941ec 100644 --- a/app/controllers/old_node_controller.rb +++ b/app/controllers/old_node_controller.rb @@ -22,4 +22,19 @@ class OldNodeController < ApplicationController render :nothing => true, :status => :internal_server_error end end + + def version + begin + old_node = OldNode.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) + + doc = OSM::API.new.get_xml_doc + doc.root << old_node.to_xml_node + + render :text => doc.to_s, :content_type => "text/xml" + rescue ActiveRecord::RecordNotFound + render :nothing => true, :status => :not_found + rescue + render :nothing => true, :status => :internal_server_error + end + end end diff --git a/app/controllers/old_relation_controller.rb b/app/controllers/old_relation_controller.rb index 0b5aa89be..9307ee1ce 100644 --- a/app/controllers/old_relation_controller.rb +++ b/app/controllers/old_relation_controller.rb @@ -2,6 +2,7 @@ class OldRelationController < ApplicationController require 'xml/libxml' session :off + before_filter :check_read_availability after_filter :compress_output def history @@ -20,4 +21,19 @@ class OldRelationController < ApplicationController render :nothing => true, :status => :internal_server_error end end + + def version + begin + old_relation = OldRelation.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) + + doc = OSM::API.new.get_xml_doc + doc.root << old_relation.to_xml_node + + render :text => doc.to_s, :content_type => "text/xml" + rescue ActiveRecord::RecordNotFound + render :nothing => true, :status => :not_found + rescue + render :nothing => true, :status => :internetal_service_error + end + end end diff --git a/app/controllers/old_way_controller.rb b/app/controllers/old_way_controller.rb index e72c97a00..6cd8c8f22 100644 --- a/app/controllers/old_way_controller.rb +++ b/app/controllers/old_way_controller.rb @@ -22,4 +22,19 @@ class OldWayController < ApplicationController render :nothing => true, :status => :internal_server_error end end + + def version + begin + old_way = OldWay.find(:first, :conditions => {:id => params[:id], :version => params[:version]} ) + + doc = OSM::API.new.get_xml_doc + doc.root << old_way.to_xml_node + + render :text => doc.to_s, :content_type => "text/xml" + rescue ActiveRecord::RecordNotFound + render :nothing => true, :status => :not_found + rescue + render :nothing => true, :status => :internal_server_error + end + end end diff --git a/app/controllers/relation_controller.rb b/app/controllers/relation_controller.rb index b38d90c02..d87905059 100644 --- a/app/controllers/relation_controller.rb +++ b/app/controllers/relation_controller.rb @@ -46,6 +46,7 @@ class RelationController < ApplicationController end def update + logger.debug request.raw_post begin relation = Relation.find(params[:id]) new_relation = Relation.from_xml(request.raw_post) diff --git a/app/models/node.rb b/app/models/node.rb index d152be6d3..a25a19f70 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -134,13 +134,13 @@ class Node < ActiveRecord::Base def delete_with_history(user) if self.visible if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = 1 AND current_way_nodes.node_id = ?", self.id ]) - raise OSM::APIPreconditionFailedError.new + raise OSM::APIPreconditionFailedError.new elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='node' and member_id=?", self.id]) - raise OSM::APIPreconditionFailedError.new + raise OSM::APIPreconditionFailedError.new else - self.user_id = user.id - self.visible = 0 - save_with_history! + self.user_id = user.id + self.visible = 0 + save_with_history! end else raise OSM::APIAlreadyDeletedError.new @@ -150,10 +150,14 @@ class Node < ActiveRecord::Base def update_from(new_node, user) if new_node.version != version raise OSM::APIVersionMismatchError.new(new_node.version, version) + elsif new_node.changeset.user_id != user.id + raise OSM::APIUserChangesetMismatchError.new + elsif not new_node.changeset.open? + raise OSM::APIChangesetAlreadyClosedError.new end # FIXME logic need looked at - self.changeset_id = user.id + self.changeset_id = new_node.changeset_id self.latitude = new_node.latitude self.longitude = new_node.longitude self.tags = new_node.tags @@ -196,7 +200,6 @@ class Node < ActiveRecord::Base el1['visible'] = self.visible.to_s el1['timestamp'] = self.timestamp.xmlschema - el1['version'] = self.version.to_s return el1 end diff --git a/app/models/relation.rb b/app/models/relation.rb index 658cf06f3..93f0001da 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -246,7 +246,7 @@ class Relation < ActiveRecord::Base else # FIXME need to deal with changeset etc #self.user_id = user.id - self.changeset = new_relation.changeset + self.changeset_id = new_relation.changeset_id self.tags = new_relation.tags self.members = new_relation.members self.visible = true diff --git a/lib/osm.rb b/lib/osm.rb index 3ddf10f98..e0e83845e 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -57,7 +57,7 @@ module OSM def render_opts { :text => "Version mismatch: Provided " + provided.to_s + - ", server had: " + latest.to_s, :status => :bad_request } + ", server had: " + latest.to_s, :status => :conflict } end end -- 2.39.5