Fix the data config, so that the development database is not the same as the test...
authorShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 8 Oct 2008 15:42:42 +0000 (15:42 +0000)
committerShaun McDonald <shaun@shaunmcdonald.me.uk>
Wed, 8 Oct 2008 15:42:42 +0000 (15:42 +0000)
app/controllers/node_controller.rb
app/controllers/relation_controller.rb
app/models/changeset.rb
app/models/node.rb
app/models/old_relation.rb
app/models/relation.rb
app/models/way.rb
config/database.yml
lib/osm.rb

index bfd1031f04a671c285b3bed33163004eca7b3f36..8e8c8446d1c66ef420a50ac10f028c53da4a17f1 100644 (file)
@@ -36,7 +36,7 @@ class NodeController < ApplicationController
   def read
     begin
       node = Node.find(params[:id])
-      if node.visible
+      if node.visible?
         response.headers['Last-Modified'] = node.timestamp.rfc822
         render :text => node.to_xml.to_s, :content_type => "text/xml"
        else
index c49ecd4d701733b035271a922e3466c40bba0881..b38d90c02b07b569ba0572d92307e2b6eca5f712 100644 (file)
@@ -15,11 +15,11 @@ class RelationController < ApplicationController
         if !relation.preconditions_ok?
           render :text => "", :status => :precondition_failed
         else
-         relation.version = 0
-          relation.user_id = @user.id
+          relation.version = 0
+          #relation.user_id = @user.id
           relation.save_with_history!
 
-         render :text => relation.id.to_s, :content_type => "text/plain"
+          render :text => relation.id.to_s, :content_type => "text/plain"
         end
       else
         render :nothing => true, :status => :bad_request
@@ -51,7 +51,7 @@ class RelationController < ApplicationController
       new_relation = Relation.from_xml(request.raw_post)
 
       if new_relation and new_relation.id == relation.id
-       relation.update_from new_relation, user
+        relation.update_from new_relation, @user
         render :text => relation.version.to_s, :content_type => "text/plain"
       else
         render :nothing => true, :status => :bad_request
index 7e2216f0e501165c6e2f1b2b870f52eb70e6b991..934d75e39de953d51e9bd98221e4b1815056be81 100644 (file)
@@ -13,6 +13,7 @@ class Changeset < ActiveRecord::Base
   has_many :old_relations
   
   validates_presence_of :user_id, :created_at, :open
+  validates_inclusion_of :open, :in => [ true, false ]
   
   # Use a method like this, so that we can easily change how we
   # determine whether a changeset is open, without breaking code in at 
index 2f3001a49a8c627c571f45bd2ed2c68711026a8f..d152be6d302eb881345a37b1962f2e09ced7e6d4 100644 (file)
@@ -4,7 +4,7 @@ class Node < ActiveRecord::Base
   include GeoRecord
 
   set_table_name 'current_nodes'
-  
+
   validates_presence_of :changeset_id, :timestamp
   validates_inclusion_of :visible, :in => [ true, false ]
   validates_numericality_of :latitude, :longitude
@@ -172,7 +172,9 @@ class Node < ActiveRecord::Base
     el1['id'] = self.id.to_s
     el1['lat'] = self.lat.to_s
     el1['lon'] = self.lon.to_s
-
+    el1['version'] = self.version.to_s
+    el1['changeset'] = self.changeset_id.to_s
+    
     user_display_name_cache = {} if user_display_name_cache.nil?
 
     if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
index 9dca97a27ae2aeab20dae1a8263583e280ee91d1..428177755492a6a4981121f3ea8a1274f5945f5d 100644 (file)
@@ -6,7 +6,7 @@ class OldRelation < ActiveRecord::Base
   def self.from_relation(relation)
     old_relation = OldRelation.new
     old_relation.visible = relation.visible
-    old_relation.user_id = relation.user_id
+    old_relation.changeset_id = relation.changeset_id
     old_relation.timestamp = relation.timestamp
     old_relation.id = relation.id
     old_relation.version = relation.version
@@ -93,6 +93,7 @@ class OldRelation < ActiveRecord::Base
     el1['timestamp'] = self.timestamp.xmlschema
     el1['user'] = self.user.display_name if self.user.data_public?
     el1['version'] = self.version.to_s
+    el1['changeset'] = self.changeset_id.to_s
     
     self.old_members.each do |member|
       e = XML::Node.new 'member'
index bb8f2d00342de67fedac105ae3d6136c478b6f25..658cf06f379e72d63e98045131884047ddc0fe1e 100644 (file)
@@ -35,13 +35,14 @@ class Relation < ActiveRecord::Base
     end
 
     relation.version = pt['version']
+    relation.changeset_id = pt['changeset']
 
     if create
       relation.timestamp = Time.now
       relation.visible = true
     else
       if pt['timestamp']
-       relation.timestamp = Time.parse(pt['timestamp'])
+        relation.timestamp = Time.parse(pt['timestamp'])
       end
     end
 
@@ -68,18 +69,19 @@ class Relation < ActiveRecord::Base
     el1['visible'] = self.visible.to_s
     el1['timestamp'] = self.timestamp.xmlschema
     el1['version'] = self.version.to_s
+    el1['changeset'] = self.changeset_id.to_s
 
     user_display_name_cache = {} if user_display_name_cache.nil?
     
-    if user_display_name_cache and user_display_name_cache.key?(self.user_id)
+    if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id)
       # use the cache if available
     elsif self.user.data_public?
-      user_display_name_cache[self.user_id] = self.user.display_name
+      user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name
     else
-      user_display_name_cache[self.user_id] = nil
+      user_display_name_cache[self.changeset.user_id] = nil
     end
 
-    el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
+    el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil?
 
     self.relation_members.each do |member|
       p=0
@@ -218,13 +220,14 @@ class Relation < ActiveRecord::Base
   def delete_with_history(user)
     if self.visible
       if RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='relation' and member_id=?", self.id ])
-       raise OSM::APIPreconditionFailedError.new
+        raise OSM::APIPreconditionFailedError.new
       else
-       self.user_id = user.id
-       self.tags = []
-       self.members = []
-       self.visible = false
-       save_with_history!
+        #self.user_id = user.id
+        # FIXME we need to deal with changeset here, which is probably already dealt with
+        self.tags = []
+        self.members = []
+        self.visible = false
+        save_with_history!
       end
     else
       raise OSM::APIAlreadyDeletedError.new
@@ -236,8 +239,14 @@ class Relation < ActiveRecord::Base
       raise OSM::APIPreconditionFailedError.new
     elsif new_relation.version != version
       raise OSM::APIVersionMismatchError.new(new_relation.version, version)
+    elsif new_relation.changeset.user_id != user.id
+      raise OSM::APIUserChangesetMismatchError.new
+    elsif not new_relation.changeset.open?
+      raise OSM::APIChangesetAlreadyClosedError.new
     else
-      self.user_id = user.id
+      # FIXME need to deal with changeset etc
+      #self.user_id = user.id
+      self.changeset = new_relation.changeset
       self.tags = new_relation.tags
       self.members = new_relation.members
       self.visible = true
index 7c6e9d06b6c1a05d48694464b30424c7f577da69..9ddb603a8720d463b46e183971b68240ed1a27b5 100644 (file)
@@ -85,6 +85,7 @@ class Way < ActiveRecord::Base
     el1['visible'] = self.visible.to_s
     el1['timestamp'] = self.timestamp.xmlschema
     el1['version'] = self.version.to_s
+    el1['changeset'] = self.changeset_id.to_s
 
     user_display_name_cache = {} if user_display_name_cache.nil?
 
index a80e11b70784068b19c360ea1c9218abb538b9f5..fe47e11aa1bbae31073b8cfb8016b7005178d3dc 100644 (file)
 #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
 development:
   adapter: mysql
-  #database: openstreetmap
-  #username: openstreetmap
-  #password: openstreetmap
-  database: osm_test
-  username: osm_test
-  password: osm_test
+  database: openstreetmap
+  username: openstreetmap
+  password: openstreetmap
   host: localhost
 
 # Warning: The database defined as 'test' will be erased and
index a64aa8c4870afc4ee2331ef49769682f318467d3..3ddf10f989c0572a48b3480059b419417d7e5756 100644 (file)
@@ -33,6 +33,20 @@ module OSM
     end
   end
 
+  # Raised when the user logged in isn't the same as the changeset
+  class APIUserChangesetMismatchError < APIError
+    def render_opts
+      { :text => "The user doesn't own that changeset", :status => :conflict }
+    end
+  end
+
+  # Raised when the changeset provided is already closed
+  class APIChangesetAlreadyClosedError < APIError
+    def render_opts
+      { :text => "The supplied changeset has already been closed", :status => :conflict }
+    end
+  end
+
   # Raised when the provided version is not equal to the latest in the db.
   class APIVersionMismatchError < APIError
     def initialize(provided, latest)