1 # Update and read user preferences, which are arbitrayr key/val pairs
2 class UserPreferenceController < ApplicationController
3 before_filter :authorize
4 before_filter :require_allow_read_prefs, :only => [:read_one, :read]
5 before_filter :require_allow_write_prefs, :except => [:read_one, :read]
8 pref = UserPreference.find(@user.id, params[:preference_key])
10 render :text => pref.v.to_s
11 rescue ActiveRecord::RecordNotFound => ex
12 render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
17 pref = UserPreference.find(@user.id, params[:preference_key])
18 pref.v = request.raw_post.chomp
20 rescue ActiveRecord::RecordNotFound
21 pref = UserPreference.new
23 pref.k = params[:preference_key]
24 pref.v = request.raw_post.chomp
28 render :nothing => true
32 UserPreference.delete(@user.id, params[:preference_key])
34 render :nothing => true
35 rescue ActiveRecord::RecordNotFound => ex
36 render :text => "param: #{params[:preference_key]} not found", :status => :not_found
39 # print out all the preferences as a big xml block
41 doc = OSM::API.new.get_xml_doc
43 prefs = @user.preferences
45 el1 = XML::Node.new 'preferences'
48 el1 << pref.to_xml_node
52 render :text => doc.to_s, :content_type => "text/xml"
55 # update the entire set of preferences
58 p = XML::Parser.string(request.raw_post)
59 rescue LibXML::XML::Error, ArgumentError => ex
60 raise OSM::APIBadXMLError.new("preferences", xml, ex.message)
68 doc.find('//preferences/preference').each do |pt|
69 pref = UserPreference.new
71 unless keyhash[pt['k']].nil? # already have that key
72 render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
79 pref.user_id = @user.id
84 render :text => 'Too many preferences', :status => :request_entity_too_large
87 # kill the existing ones
88 UserPreference.delete_all(['user_id = ?', @user.id])
94 render :nothing => true
96 rescue Exception => ex
97 render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error