Permalink
Browse files

modifications to keep json generation running in REST API -- as an al…

…ternative to XML; also couldnt get i18n running so commented it out for now
  • Loading branch information...
2 parents cc95a2b + b2e490e commit 624a2a1c42381f98785017f2f80fa4bad27e06ae @jywarren committed May 25, 2011
Showing 596 changed files with 17,698 additions and 4,263 deletions.
@@ -172,7 +172,7 @@ def amf_handle_error(call,rootobj,rootid)
def amf_handle_error_with_timeout(call,rootobj,rootid)
amf_handle_error(call,rootobj,rootid) do
- Timeout::timeout(APP_CONFIG['api_timeout'], OSM::APITimeoutError) do
+ Timeout::timeout(API_TIMEOUT, OSM::APITimeoutError) do
yield
end
end
@@ -187,6 +187,11 @@ def startchangeset(usertoken, cstags, closeid, closecomment, opennew)
if !user then return -1,"You are not logged in, so Potlatch can't write any changes to the database." end
unless user.active_blocks.empty? then return -1,t('application.setup_user_auth.blocked') end
+ if cstags
+ if !tags_ok(cstags) then return -1,"One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1." end
+ cstags = strip_non_xml_chars cstags
+ end
+
# close previous changeset and add comment
if closeid
cs = Changeset.find(closeid.to_i)
@@ -197,6 +202,8 @@ def startchangeset(usertoken, cstags, closeid, closecomment, opennew)
cs.save!
else
cs.tags['comment']=closecomment
+ # in case closecomment has chars not allowed in xml
+ cs.tags = strip_non_xml_chars cs.tags
cs.save_with_tags!
end
end
@@ -206,7 +213,11 @@ def startchangeset(usertoken, cstags, closeid, closecomment, opennew)
cs = Changeset.new
cs.tags = cstags
cs.user_id = user.id
- if !closecomment.empty? then cs.tags['comment']=closecomment end
+ if !closecomment.empty?
+ cs.tags['comment']=closecomment
+ # in case closecomment has chars not allowed in xml
+ cs.tags = strip_non_xml_chars cs.tags
+ end
# smsm1 doesn't like the next two lines and thinks they need to be abstracted to the model more/better
cs.created_at = Time.now.getutc
cs.closed_at = cs.created_at + Changeset::IDLE_TIMEOUT
View
41 app/controllers/api_controller.rb 100644 → 100755
@@ -18,7 +18,7 @@ def trackpoints
return
end
- offset = page * APP_CONFIG['tracepoints_per_page']
+ offset = page * TRACEPOINTS_PER_PAGE
# Figure out the bbox
bbox = params['bbox']
@@ -39,14 +39,14 @@ def trackpoints
end
# get all the points
- points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => APP_CONFIG['tracepoints_per_page'], :order => "gpx_id DESC, trackid ASC, timestamp ASC" )
+ points = Tracepoint.find_by_area(min_lat, min_lon, max_lat, max_lon, :offset => offset, :limit => TRACEPOINTS_PER_PAGE, :order => "gpx_id DESC, trackid ASC, timestamp ASC" )
doc = XML::Document.new
doc.encoding = XML::Encoding::UTF_8
root = XML::Node.new 'gpx'
root['version'] = '1.0'
root['creator'] = 'OpenStreetMap.org'
- root['xmlns'] = "http://www.topografix.com/GPX/1/0/"
+ root['xmlns'] = "http://www.topografix.com/GPX/1/0"
doc.root = root
@@ -145,30 +145,43 @@ def map
end
# FIXME um why is this area using a different order for the lat/lon from above???
- @nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => {:visible => true}, :include => :node_tags, :limit => APP_CONFIG['max_number_of_nodes']+1)
+ @nodes = Node.find_by_area(min_lat, min_lon, max_lat, max_lon, :conditions => {:visible => true}, :include => :node_tags, :limit => MAX_NUMBER_OF_NODES+1)
# get all the nodes, by tag not yet working, waiting for change from NickB
# need to be @nodes (instance var) so tests in /spec can be performed
#@nodes = Node.search(bbox, params[:tag])
node_ids = @nodes.collect(&:id)
- if node_ids.length > APP_CONFIG['max_number_of_nodes']
- report_error("You requested too many nodes (limit is #{APP_CONFIG['max_number_of_nodes']}). Either request a smaller area, or use planet.osm")
+ if node_ids.length > MAX_NUMBER_OF_NODES
+ report_error("You requested too many nodes (limit is #{MAX_NUMBER_OF_NODES}). Either request a smaller area, or use planet.osm")
return
end
if node_ids.length == 0
render :text => "<osm version='#{API_VERSION}' generator='#{GENERATOR}'></osm>", :content_type => "text/xml"
return
end
- doc = OSM::API.new.get_xml_doc
-
# add bounds
- bounds = XML::Node.new 'bounds'
+ if params[:format] == 'json'
+ bounds = {}
+ else
+ bounds = XML::Node.new 'bounds'
+ end
bounds['minlat'] = min_lat.to_s
bounds['minlon'] = min_lon.to_s
bounds['maxlat'] = max_lat.to_s
bounds['maxlon'] = max_lon.to_s
- doc.root << bounds
+
+ if params[:format] == 'json'
+ doc = {}
+ doc['bounds'] = bounds
+ doc['node'] = []
+ doc['way'] = []
+ doc['relation'] = []
+ else
+ doc = OSM::API.new.get_xml_doc
+ doc.root << bounds
+ end
+
# get ways
# find which ways are needed
@@ -320,19 +333,19 @@ def capabilities
version['maximum'] = "#{API_VERSION}";
api << version
area = XML::Node.new 'area'
- area['maximum'] = APP_CONFIG['max_request_area'].to_s;
+ area['maximum'] = MAX_REQUEST_AREA.to_s;
api << area
tracepoints = XML::Node.new 'tracepoints'
- tracepoints['per_page'] = APP_CONFIG['tracepoints_per_page'].to_s
+ tracepoints['per_page'] = TRACEPOINTS_PER_PAGE.to_s
api << tracepoints
waynodes = XML::Node.new 'waynodes'
- waynodes['maximum'] = APP_CONFIG['max_number_of_way_nodes'].to_s
+ waynodes['maximum'] = MAX_NUMBER_OF_WAY_NODES.to_s
api << waynodes
changesets = XML::Node.new 'changesets'
changesets['maximum_elements'] = Changeset::MAX_ELEMENTS.to_s
api << changesets
timeout = XML::Node.new 'timeout'
- timeout['seconds'] = APP_CONFIG['api_timeout'].to_s
+ timeout['seconds'] = API_TIMEOUT.to_s
api << timeout
doc.root << api
@@ -2,7 +2,7 @@
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
- if OSM_STATUS == :database_readonly or OSM_STATUS == :database_offline
+ if STATUS == :database_readonly or STATUS == :database_offline
session :off
end
@@ -120,39 +120,36 @@ def authorize(realm='Web Password', errormessage="Couldn't authenticate you")
end
def check_database_readable(need_api = false)
- if OSM_STATUS == :database_offline or (need_api and OSM_STATUS == :api_offline)
+ if STATUS == :database_offline or (need_api and STATUS == :api_offline)
redirect_to :controller => 'site', :action => 'offline'
end
end
def check_database_writable(need_api = false)
- if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
- (need_api and (OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly))
+ if STATUS == :database_offline or STATUS == :database_readonly or
+ (need_api and (STATUS == :api_offline or STATUS == :api_readonly))
redirect_to :controller => 'site', :action => 'offline'
end
end
def check_api_readable
- if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline
- response.headers['Error'] = "Database offline for maintenance"
- render :nothing => true, :status => :service_unavailable
+ if STATUS == :database_offline or STATUS == :api_offline
+ report_error "Database offline for maintenance", :service_unavailable
return false
end
end
def check_api_writable
- if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
- OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly
- response.headers['Error'] = "Database offline for maintenance"
- render :nothing => true, :status => :service_unavailable
+ if STATUS == :database_offline or STATUS == :database_readonly or
+ STATUS == :api_offline or STATUS == :api_readonly
+ report_error "Database offline for maintenance", :service_unavailable
return false
end
end
def require_public_data
unless @user.data_public?
- response.headers['Error'] = "You must make your edits public to upload new data"
- render :nothing => true, :status => :forbidden
+ report_error "You must make your edits public to upload new data", :forbidden
return false
end
end
@@ -165,7 +162,18 @@ def require_public_data
def report_error(message, status = :bad_request)
# Todo: some sort of escaping of problem characters in the message
response.headers['Error'] = message
- render :text => message, :status => status
+
+ if request.headers['X-Error-Format'] and
+ request.headers['X-Error-Format'].downcase == "xml"
+ result = OSM::API.new.get_xml_doc
+ result.root.name = "osmError"
+ result.root << (XML::Node.new("status") << interpret_status(status))
+ result.root << (XML::Node.new("message") << message)
+
+ render :text => result.to_s, :content_type => "text/xml"
+ else
+ render :text => message, :status => status
+ end
end
def set_locale
@@ -219,7 +227,7 @@ def assert_method(method)
##
# wrap an api call in a timeout
def api_call_timeout
- SystemTimer.timeout_after(APP_CONFIG['api_timeout']) do
+ SystemTimer.timeout_after(API_TIMEOUT) do
yield
end
rescue Timeout::Error
@@ -229,7 +237,7 @@ def api_call_timeout
##
# wrap a web page in a timeout
def web_timeout
- SystemTimer.timeout_after(APP_CONFIG['web_timeout']) do
+ SystemTimer.timeout_after(WEB_TIMEOUT) do
yield
end
rescue ActionView::TemplateError => ex
@@ -72,8 +72,10 @@ def changeset
@next = Changeset.find(:first, :order => "id ASC", :conditions => [ "id > :id", { :id => @changeset.id }] )
@prev = Changeset.find(:first, :order => "id DESC", :conditions => [ "id < :id", { :id => @changeset.id }] )
- @next_by_user = Changeset.find(:first, :order => "id ASC", :conditions => [ "id > :id AND user_id = :user_id", {:id => @changeset.id, :user_id => @changeset.user_id }] )
- @prev_by_user = Changeset.find(:first, :order => "id DESC", :conditions => [ "id < :id AND user_id = :user_id", {:id => @changeset.id, :user_id => @changeset.user_id }] )
+ if @changeset.user.data_public?
+ @next_by_user = Changeset.find(:first, :order => "id ASC", :conditions => [ "id > :id AND user_id = :user_id", { :id => @changeset.id, :user_id => @changeset.user_id }] )
+ @prev_by_user = Changeset.find(:first, :order => "id DESC", :conditions => [ "id < :id AND user_id = :user_id", { :id => @changeset.id, :user_id => @changeset.user_id }] )
+ end
rescue ActiveRecord::RecordNotFound
render :action => "not_found", :status => :not_found
end
@@ -254,67 +254,71 @@ def update
##
# list edits (open changesets) in reverse chronological order
def list
- conditions = conditions_nonempty
-
- if params[:display_name]
- user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
+ if request.format == :atom and params[:page]
+ redirect_to params.merge({ :page => nil }), :status => :moved_permanently
+ else
+ conditions = conditions_nonempty
- if user
- if user.data_public? or user == @user
- conditions = cond_merge conditions, ['user_id = ?', user.id]
- else
- conditions = cond_merge conditions, ['false']
+ if params[:display_name]
+ user = User.find_by_display_name(params[:display_name], :conditions => { :status => ["active", "confirmed"] })
+
+ if user
+ if user.data_public? or user == @user
+ conditions = cond_merge conditions, ['user_id = ?', user.id]
+ else
+ conditions = cond_merge conditions, ['false']
+ end
+ elsif request.format == :html
+ @title = t 'user.no_such_user.title'
+ @not_found_user = params[:display_name]
+ render :template => 'user/no_such_user', :status => :not_found
end
- elsif request.format == :html
- @title = t 'user.no_such_user.title'
- @not_found_user = params[:display_name]
- render :template => 'user/no_such_user', :status => :not_found
end
- end
-
- if params[:bbox]
- bbox = params[:bbox]
- elsif params[:minlon] and params[:minlat] and params[:maxlon] and params[:maxlat]
- bbox = params[:minlon] + ',' + params[:minlat] + ',' + params[:maxlon] + ',' + params[:maxlat]
- end
-
- if bbox
- conditions = cond_merge conditions, conditions_bbox(bbox)
- bbox = BoundingBox.from_s(bbox)
- bbox_link = render_to_string :partial => "bbox", :object => bbox
- end
+
+ if params[:bbox]
+ bbox = params[:bbox]
+ elsif params[:minlon] and params[:minlat] and params[:maxlon] and params[:maxlat]
+ bbox = params[:minlon] + ',' + params[:minlat] + ',' + params[:maxlon] + ',' + params[:maxlat]
+ end
+
+ if bbox
+ conditions = cond_merge conditions, conditions_bbox(bbox)
+ bbox = BoundingBox.from_s(bbox)
+ bbox_link = render_to_string :partial => "bbox", :object => bbox
+ end
+
+ if user
+ user_link = render_to_string :partial => "user", :object => user
+ end
+
+ if user and bbox
+ @title = t 'changeset.list.title_user_bbox', :user => user.display_name, :bbox => bbox.to_s
+ @heading = t 'changeset.list.heading_user_bbox', :user => user.display_name, :bbox => bbox.to_s
+ @description = t 'changeset.list.description_user_bbox', :user => user_link, :bbox => bbox_link
+ elsif user
+ @title = t 'changeset.list.title_user', :user => user.display_name
+ @heading = t 'changeset.list.heading_user', :user => user.display_name
+ @description = t 'changeset.list.description_user', :user => user_link
+ elsif bbox
+ @title = t 'changeset.list.title_bbox', :bbox => bbox.to_s
+ @heading = t 'changeset.list.heading_bbox', :bbox => bbox.to_s
+ @description = t 'changeset.list.description_bbox', :bbox => bbox_link
+ else
+ @title = t 'changeset.list.title'
+ @heading = t 'changeset.list.heading'
+ @description = t 'changeset.list.description'
+ end
- if user
- user_link = render_to_string :partial => "user", :object => user
- end
+ @page = (params[:page] || 1).to_i
+ @page_size = 20
- if user and bbox
- @title = t 'changeset.list.title_user_bbox', :user => user.display_name, :bbox => bbox.to_s
- @heading = t 'changeset.list.heading_user_bbox', :user => user.display_name, :bbox => bbox.to_s
- @description = t 'changeset.list.description_user_bbox', :user => user_link, :bbox => bbox_link
- elsif user
- @title = t 'changeset.list.title_user', :user => user.display_name
- @heading = t 'changeset.list.heading_user', :user => user.display_name
- @description = t 'changeset.list.description_user', :user => user_link
- elsif bbox
- @title = t 'changeset.list.title_bbox', :bbox => bbox.to_s
- @heading = t 'changeset.list.heading_bbox', :bbox => bbox.to_s
- @description = t 'changeset.list.description_bbox', :bbox => bbox_link
- else
- @title = t 'changeset.list.title'
- @heading = t 'changeset.list.heading'
- @description = t 'changeset.list.description'
+ @edits = Changeset.find(:all,
+ :include => [:user, :changeset_tags],
+ :conditions => conditions,
+ :order => "changesets.created_at DESC",
+ :offset => (@page - 1) * @page_size,
+ :limit => @page_size)
end
-
- @page = (params[:page] || 1).to_i
- @page_size = 20
-
- @edits = Changeset.find(:all,
- :include => [:user, :changeset_tags],
- :conditions => conditions,
- :order => "changesets.created_at DESC",
- :offset => (@page - 1) * @page_size,
- :limit => @page_size)
end
private
@@ -1,8 +0,0 @@
-class ChangesetTagController < ApplicationController
- layout 'site'
-
- def search
- @tags = ChangesetTag.find(:all, :limit => 11, :conditions => ["match(v) against (?)", params[:query][:query].to_s] )
- end
-
-end
@@ -10,7 +10,7 @@ class DiaryEntryController < ApplicationController
caches_action :list, :view, :layout => false
caches_action :rss, :layout => true
- cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment], :unless => OSM_STATUS == :database_offline
+ cache_sweeper :diary_sweeper, :only => [:new, :edit, :comment, :hide, :hidecomment], :unless => STATUS == :database_offline
def new
@title = t 'diary_entry.new.title'
Oops, something went wrong.

0 comments on commit 624a2a1

Please sign in to comment.