From: Steve Coast Date: Sat, 23 Feb 2008 13:37:36 +0000 (+0000) Subject: User prference system basically done X-Git-Tag: live~7913 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/2e44f9ccf3ac8c54c407d74422247b5a721b269c User prference system basically done --- diff --git a/app/controllers/user_preference_controller.rb b/app/controllers/user_preference_controller.rb index 53db769ef..7f841b72b 100644 --- a/app/controllers/user_preference_controller.rb +++ b/app/controllers/user_preference_controller.rb @@ -1,2 +1,68 @@ class UserPreferenceController < ApplicationController + before_filter :authorize + + def read + + doc = OSM::API.new.get_xml_doc + + prefs = @user.preferences + + el1 = XML::Node.new 'preferences' + + prefs.each do |pref| + el1 << pref.to_xml_node + end + + doc.root << el1 + render :text => doc.to_s, :content_type => "text/xml" + + end + + def update + begin + p = XML::Parser.new + p.string = request.raw_post + doc = p.parse + + prefs = [] + + keyhash = {} + + doc.find('//preferences/preference').each do |pt| + pref = UserPreference.new + + unless keyhash[pt['k']].nil? # already have that key + render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => 406 + return + end + + keyhash[pt['k']] = 1 + + pref.k = pt['k'] + pref.v = pt['v'] + pref.user_id = @user.id + prefs << pref + end + + if prefs.size > 150 + render :text => 'Too many preferences', :status => 413 + return + end + + # kill the existing ones + UserPreference.delete_all(['user_id = ?', @user.id]) + + # save the new ones + prefs.each do |pref| + pref.save! + end + + rescue Exception => ex + render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => 500 + return + end + + render :nothing => true + end + end diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index 9584d13e4..2a73f2233 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -1,3 +1,13 @@ class UserPreference < ActiveRecord::Base belongs_to :user + + # Turn this Node in to an XML Node without the wrapper. + def to_xml_node + el1 = XML::Node.new 'preference' + el1['k'] = self.k + el1['v'] = self.v + + return el1 + end + end diff --git a/config/routes.rb b/config/routes.rb index 025d5d2b0..c6a9de38a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,8 @@ ActionController::Routing::Routes.draw do |map| map.connect "api/#{API_VERSION}/nodes/search", :controller => 'search', :action => 'search_nodes' map.connect "api/#{API_VERSION}/user/details", :controller => 'user', :action => 'api_details' + map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'read', :conditions => { :method => :get } + map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'update', :conditions => { :method => :put } map.connect "api/#{API_VERSION}/user/gpx_files", :controller => 'user', :action => 'api_gpx_files' map.connect "api/#{API_VERSION}/gpx/create", :controller => 'trace', :action => 'api_create'