]> git.openstreetmap.org Git - rails.git/commitdiff
User prference system basically done
authorSteve Coast <steve@asklater.com>
Sat, 23 Feb 2008 13:37:36 +0000 (13:37 +0000)
committerSteve Coast <steve@asklater.com>
Sat, 23 Feb 2008 13:37:36 +0000 (13:37 +0000)
app/controllers/user_preference_controller.rb
app/models/user_preference.rb
config/routes.rb

index 53db769efcd8aec57e904ea3ad635633c4e26a2f..7f841b72b79d244e82c5e2899fccb8e37179d2c0 100644 (file)
@@ -1,2 +1,68 @@
 class UserPreferenceController < ApplicationController
+  before_filter :authorize
+
+  def read
+
+    doc = OSM::API.new.get_xml_doc
+
+    prefs = @user.preferences
+
+    el1 = XML::Node.new 'preferences'
+
+    prefs.each do |pref|
+      el1 <<  pref.to_xml_node
+    end
+
+    doc.root << el1
+    render :text => doc.to_s, :content_type => "text/xml"
+
+  end
+
+  def update
+    begin
+      p = XML::Parser.new
+      p.string = request.raw_post
+      doc = p.parse
+
+      prefs = []
+
+      keyhash = {}
+
+      doc.find('//preferences/preference').each do |pt|
+        pref = UserPreference.new
+        
+        unless keyhash[pt['k']].nil? # already have that key
+          render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => 406
+          return
+        end
+        
+        keyhash[pt['k']] = 1
+
+        pref.k = pt['k']
+        pref.v = pt['v']
+        pref.user_id = @user.id
+        prefs << pref
+      end
+
+      if prefs.size > 150
+        render :text => 'Too many preferences', :status => 413
+        return
+      end
+
+      # kill the existing ones
+      UserPreference.delete_all(['user_id = ?', @user.id])
+
+      # save the new ones
+      prefs.each do |pref|
+        pref.save!
+      end
+
+    rescue Exception => ex
+      render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => 500
+      return
+    end
+
+    render :nothing => true
+  end
+
 end
index 9584d13e43aeb053897eaedf98ed286c7b34965e..2a73f22331d7333f7a262c6d262dad0519a25824 100644 (file)
@@ -1,3 +1,13 @@
 class UserPreference < ActiveRecord::Base
   belongs_to :user
+
+  # Turn this Node in to an XML Node without the <osm> wrapper.
+  def to_xml_node
+    el1 = XML::Node.new 'preference'
+    el1['k'] = self.k
+    el1['v'] = self.v
+    
+    return el1
+  end
+
 end
index 025d5d2b0a7114a86e527e5a7ca8890e49bdbe28..c6a9de38a4c7654c9875aeda9aa907908e82b6b5 100644 (file)
@@ -41,6 +41,8 @@ ActionController::Routing::Routes.draw do |map|
   map.connect "api/#{API_VERSION}/nodes/search", :controller => 'search', :action => 'search_nodes'
   
   map.connect "api/#{API_VERSION}/user/details", :controller => 'user', :action => 'api_details'
+  map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'read', :conditions => { :method => :get }
+  map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'update', :conditions => { :method => :put }
   map.connect "api/#{API_VERSION}/user/gpx_files", :controller => 'user', :action => 'api_gpx_files'
  
   map.connect "api/#{API_VERSION}/gpx/create", :controller => 'trace', :action => 'api_create'