api06: Merge changesets code from Frederik.
This commit is contained in:
commit
5ed2cbce02
8 changed files with 153 additions and 6 deletions
26
app/controllers/changeset_controller.rb
Normal file
26
app/controllers/changeset_controller.rb
Normal file
|
@ -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
|
9
app/controllers/changeset_tag_controller.rb
Normal file
9
app/controllers/changeset_tag_controller.rb
Normal file
|
@ -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
|
73
app/models/changeset.rb
Normal file
73
app/models/changeset.rb
Normal file
|
@ -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
|
5
app/models/changeset_tag.rb
Normal file
5
app/models/changeset_tag.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class ChangesetTag < ActiveRecord::Base
|
||||
|
||||
belongs_to :changeset, :foreign_key => 'id'
|
||||
|
||||
end
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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+/
|
||||
|
|
32
db/migrate/013_add_changesets.rb
Normal file
32
db/migrate/013_add_changesets.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue