]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_preference_controller.rb
Added a bunch more tests on the API 0.6. Fixed node/way/relation from_xml code to...
[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.new
57       p.string = request.raw_post
58       doc = p.parse
59
60       prefs = []
61
62       keyhash = {}
63
64       doc.find('//preferences/preference').each do |pt|
65         pref = UserPreference.new
66
67         unless keyhash[pt['k']].nil? # already have that key
68           render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
69           return
70         end
71
72         keyhash[pt['k']] = 1
73
74         pref.k = pt['k']
75         pref.v = pt['v']
76         pref.user_id = @user.id
77         prefs << pref
78       end
79
80       if prefs.size > 150
81         render :text => 'Too many preferences', :status => :request_entity_too_large
82         return
83       end
84
85       # kill the existing ones
86       UserPreference.delete_all(['user_id = ?', @user.id])
87
88       # save the new ones
89       prefs.each do |pref|
90         pref.save!
91       end
92
93     rescue Exception => ex
94       render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error
95       return
96     end
97
98     render :nothing => true
99   end
100 end