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