1 # Update and read user preferences, which are arbitrayr key/val pairs
2 class UserPreferenceController < ApplicationController
3 skip_before_filter :verify_authenticity_token
4 before_filter :authorize
5 before_filter :require_allow_read_prefs, :only => [:read_one, :read]
6 before_filter :require_allow_write_prefs, :except => [:read_one, :read]
9 pref = UserPreference.find(@user.id, params[:preference_key])
11 render :text => pref.v.to_s
12 rescue ActiveRecord::RecordNotFound => ex
13 render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
18 pref = UserPreference.find(@user.id, params[:preference_key])
19 pref.v = request.raw_post.chomp
21 rescue ActiveRecord::RecordNotFound
22 pref = UserPreference.new
24 pref.k = params[:preference_key]
25 pref.v = request.raw_post.chomp
29 render :nothing => true
33 UserPreference.delete(@user.id, params[:preference_key])
35 render :nothing => true
36 rescue ActiveRecord::RecordNotFound => ex
37 render :text => "param: #{params[:preference_key]} not found", :status => :not_found
40 # print out all the preferences as a big xml block
42 doc = OSM::API.new.get_xml_doc
44 prefs = @user.preferences
46 el1 = XML::Node.new 'preferences'
49 el1 << pref.to_xml_node
53 render :text => doc.to_s, :content_type => "text/xml"
56 # update the entire set of preferences
59 p = XML::Parser.string(request.raw_post)
60 rescue LibXML::XML::Error, ArgumentError => ex
61 raise OSM::APIBadXMLError.new("preferences", xml, ex.message)
69 doc.find('//preferences/preference').each do |pt|
70 pref = UserPreference.new
72 unless keyhash[pt['k']].nil? # already have that key
73 render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
80 pref.user_id = @user.id
85 render :text => 'Too many preferences', :status => :request_entity_too_large
88 # kill the existing ones
89 UserPreference.delete_all(['user_id = ?', @user.id])
95 render :nothing => true
97 rescue Exception => ex
98 render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error