]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_preference_controller.rb
Add history for ways and nodes, split 'last edited by' into a seperate template to...
[rails.git] / app / controllers / user_preference_controller.rb
1 class UserPreferenceController < ApplicationController
2   before_filter :authorize
3
4   def read
5
6     doc = OSM::API.new.get_xml_doc
7
8     prefs = @user.preferences
9
10     el1 = XML::Node.new 'preferences'
11
12     prefs.each do |pref|
13       el1 <<  pref.to_xml_node
14     end
15
16     doc.root << el1
17     render :text => doc.to_s, :content_type => "text/xml"
18
19   end
20
21   def update
22     begin
23       p = XML::Parser.new
24       p.string = request.raw_post
25       doc = p.parse
26
27       prefs = []
28
29       keyhash = {}
30
31       doc.find('//preferences/preference').each do |pt|
32         pref = UserPreference.new
33         
34         unless keyhash[pt['k']].nil? # already have that key
35           render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
36           return
37         end
38         
39         keyhash[pt['k']] = 1
40
41         pref.k = pt['k']
42         pref.v = pt['v']
43         pref.user_id = @user.id
44         prefs << pref
45       end
46
47       if prefs.size > 150
48         render :text => 'Too many preferences', :status => :request_entity_too_large
49         return
50       end
51
52       # kill the existing ones
53       UserPreference.delete_all(['user_id = ?', @user.id])
54
55       # save the new ones
56       prefs.each do |pref|
57         pref.save!
58       end
59
60     rescue Exception => ex
61       render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error
62       return
63     end
64
65     render :nothing => true
66   end
67
68 end