From: Frederik Ramm Date: Sun, 4 May 2008 10:41:42 +0000 (+0000) Subject: initial changeset support X-Git-Tag: live~7557^2~373^2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/8c5638d6968be3286414f04b8dee2261ada84e76 initial changeset support --- diff --git a/app/controllers/changeset_controller.rb b/app/controllers/changeset_controller.rb new file mode 100644 index 000000000..1041c7469 --- /dev/null +++ b/app/controllers/changeset_controller.rb @@ -0,0 +1,26 @@ +# The ChangesetController is the RESTful interface to Changeset objects + +class ChangesetController < ApplicationController + require 'xml/libxml' + + before_filter :authorize, :only => [:create, :update, :delete] + before_filter :check_write_availability, :only => [:create, :update, :delete] + before_filter :check_read_availability, :except => [:create, :update, :delete] + + # Create a changeset from XML. + def create + if request.put? + cs = Changeset.from_xml(request.raw_post, true) + + if cs + cs.user_id = @user.id + cs.save_with_tags! + render :text => cs.id.to_s, :content_type => "text/plain" + else + render :nothing => true, :status => :bad_request + end + else + render :nothing => true, :status => :method_not_allowed + end + end +end diff --git a/app/controllers/changeset_tag_controller.rb b/app/controllers/changeset_tag_controller.rb new file mode 100644 index 000000000..3e8db3fc2 --- /dev/null +++ b/app/controllers/changeset_tag_controller.rb @@ -0,0 +1,9 @@ +class ChangesetTagController < ApplicationController + layout 'site' + + def search + @tags = ChangesetTag.find(:all, :limit => 11, :conditions => ["match(v) against (?)", params[:query][:query].to_s] ) + end + + +end diff --git a/app/models/changeset.rb b/app/models/changeset.rb new file mode 100644 index 000000000..85621dce2 --- /dev/null +++ b/app/models/changeset.rb @@ -0,0 +1,73 @@ +class Changeset < ActiveRecord::Base + require 'xml/libxml' + + belongs_to :user + + has_many :changeset_tags, :foreign_key => 'id' + + def self.from_xml(xml, create=false) + begin + p = XML::Parser.new + p.string = xml + doc = p.parse + + cs = Changeset.new + + doc.find('//osm/changeset').each do |pt| + if create + cs.created_at = Time.now + end + + pt.find('tag').each do |tag| + cs.add_tag_keyval(tag['k'], tag['v']) + end + end + rescue Exception => ex + print "noes "+ ex.to_s + "\n" + cs = nil + end + + return cs + end + + def tags + unless @tags + @tags = {} + self.changeset_tags.each do |tag| + @tags[tag.k] = tag.v + end + end + @tags + end + + def tags=(t) + @tags = t + end + + def add_tag_keyval(k, v) + @tags = Hash.new unless @tags + @tags[k] = v + end + + def save_with_tags! + t = Time.now + + Changeset.transaction do + # fixme update modified_at time? + self.save! + end + + ChangesetTag.transaction do + tags = self.tags + ChangesetTag.delete_all(['id = ?', self.id]) + + tags.each do |k,v| + tag = ChangesetTag.new + tag.k = k + tag.v = v + tag.id = self.id + tag.save! + end + end + end +end diff --git a/app/models/changeset_tag.rb b/app/models/changeset_tag.rb new file mode 100644 index 000000000..6298fbe77 --- /dev/null +++ b/app/models/changeset_tag.rb @@ -0,0 +1,5 @@ +class ChangesetTag < ActiveRecord::Base + + belongs_to :changeset, :foreign_key => 'id' + +end diff --git a/config/database.yml b/config/database.yml index b884f3b93..c0b5dd98c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -12,9 +12,9 @@ # http://dev.mysql.com/doc/refman/5.0/en/old-client.html development: adapter: mysql - database: openstreetmap - username: openstreetmap - password: openstreetmap + database: osm + username: osm + password: osm host: localhost # Warning: The database defined as 'test' will be erased and @@ -29,8 +29,8 @@ test: production: adapter: mysql - database: openstreetmap - username: openstreetmap - password: openstreetmap - host: db.openstreetmap.org + database: osm + username: osm + password: osm + host: localhost diff --git a/config/environment.rb b/config/environment.rb index 02d99f20c..45c9ed4d6 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -6,13 +6,13 @@ ENV['RAILS_ENV'] ||= 'production' # Specifies gem version of Rails to use when vendor/rails is not present # DO NOT BUMP THIS TO 2.0.2 AS THE LIVE SERVERS CAN'T RUN THAT -RAILS_GEM_VERSION = '2.0.1' unless defined? RAILS_GEM_VERSION +RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION # Set the server URL SERVER_URL = ENV['OSM_SERVER_URL'] || 'www.openstreetmap.org' # Application constants needed for routes.rb - must go before Initializer call -API_VERSION = ENV['OSM_API_VERSION'] || '0.5' +API_VERSION = ENV['OSM_API_VERSION'] || '0.6' # Set to :readonly to put the API in read-only mode or :offline to # take it completely offline diff --git a/config/routes.rb b/config/routes.rb index 2ac48cc29..3b7af885d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,8 @@ ActionController::Routing::Routes.draw do |map| # API + map.connect "api/#{API_VERSION}/changeset/create", :controller => 'changeset', :action => 'create' + map.connect "api/#{API_VERSION}/node/create", :controller => 'node', :action => 'create' map.connect "api/#{API_VERSION}/node/:id/ways", :controller => 'way', :action => 'ways_for_node', :id => /\d+/ map.connect "api/#{API_VERSION}/node/:id/relations", :controller => 'relation', :action => 'relations_for_node', :id => /\d+/ diff --git a/db/migrate/013_add_changesets.rb b/db/migrate/013_add_changesets.rb new file mode 100644 index 000000000..40455ec68 --- /dev/null +++ b/db/migrate/013_add_changesets.rb @@ -0,0 +1,32 @@ +class AddChangesets < ActiveRecord::Migration + def self.up + create_table "changesets", innodb_table do |t| + t.column "id", :bigint, :limit => 20, :null => false + t.column "user_id", :bigint, :limit => 20, :null => false + t.column "created_at", :datetime, :null => false + t.column "open", :boolean, :null => false, :default => true + t.column "min_lat", :integer, :null => true + t.column "max_lat", :integer, :null => true + t.column "min_lon", :integer, :null => true + t.column "max_lon", :integer, :null => true + end + + add_primary_key "changesets", ["id"] + # FIXME add indexes? + + change_column "changesets", "id", :bigint, :limit => 20, :null => false, :options => "AUTO_INCREMENT" + + create_table "changeset_tags", innodb_table do |t| + t.column "id", :bigint, :limit => 64, :null => false + t.column "k", :string, :default => "", :null => false + t.column "v", :string, :default => "", :null => false + end + + add_index "changeset_tags", ["id"], :name => "changeset_tags_id_idx" + end + + def self.down + drop_table "changesets" + drop_table "changeset_tags" + end +end