]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_preference_controller.rb
Fix a few user preferences bugs
[rails.git] / app / controllers / user_preference_controller.rb
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]
7   around_filter :api_call_handle_error
8
9   def read_one
10     pref = UserPreference.find(@user.id, params[:preference_key])
11
12     render :text => pref.v.to_s, :content_type => "text/plain"
13   rescue ActiveRecord::RecordNotFound => ex
14     render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
15   end
16
17   def update_one
18     begin
19       pref = UserPreference.find(@user.id, params[:preference_key])
20       pref.v = request.raw_post.chomp
21       pref.save
22     rescue ActiveRecord::RecordNotFound 
23       pref = UserPreference.new
24       pref.user = @user
25       pref.k = params[:preference_key]
26       pref.v = request.raw_post.chomp
27       pref.save
28     end
29
30     render :nothing => true, :content_type => "text/plain"
31   end
32
33   def delete_one
34     UserPreference.find(@user.id, params[:preference_key]).delete
35
36     render :nothing => true, :content_type => "text/plain"
37   rescue ActiveRecord::RecordNotFound => ex
38     render :text => "param: #{params[:preference_key]} not found", :status => :not_found
39   end
40
41   # print out all the preferences as a big xml block
42   def read
43     doc = OSM::API.new.get_xml_doc
44
45     prefs = @user.preferences
46
47     el1 = XML::Node.new 'preferences'
48
49     prefs.each do |pref|
50       el1 <<  pref.to_xml_node
51     end
52
53     doc.root << el1
54     render :text => doc.to_s, :content_type => "text/xml"
55   end
56
57   # update the entire set of preferences
58   def update
59     doc = XML::Parser.string(request.raw_post).parse
60
61     prefs = []
62
63     keyhash = {}
64
65     doc.find('//preferences/preference').each do |pt|
66       pref = UserPreference.new
67
68       unless keyhash[pt['k']].nil? # already have that key
69         render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable, :content_type => "text/plain"
70         return
71       end
72
73       keyhash[pt['k']] = 1
74
75       pref.k = pt['k']
76       pref.v = pt['v']
77       pref.user_id = @user.id
78       prefs << pref
79     end
80
81     if prefs.size > 150
82       render :text => 'Too many preferences', :status => :request_entity_too_large, :content_type => "text/plain"
83       return
84     end
85
86     # kill the existing ones
87     UserPreference.delete_all(['user_id = ?', @user.id])
88
89     # save the new ones
90     prefs.each do |pref|
91       pref.save!
92     end
93
94     render :nothing => true, :content_type => "text/plain"
95   end
96 end