From 2709027882c3f36a1d6a442c2c4b706b54b1edeb Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Thu, 29 Mar 2007 22:14:39 +0000 Subject: [PATCH] API 0.4: User rename, view trace links, tag filter summary / see all link, traces testing --- app/controllers/trace_controller.rb | 13 ++-- app/controllers/user_controller.rb | 17 ++++++ app/models/trace.rb | 84 ++++++++++++++------------ app/models/user.rb | 10 +-- app/views/layouts/site.rhtml | 2 - app/views/trace/_trace.rhtml | 6 +- app/views/trace/_trace_optionals.rhtml | 24 +++++--- app/views/trace/list.rhtml | 13 +++- app/views/trace/mine.rhtml | 14 +++-- app/views/trace/view.rhtml | 14 +++-- config/routes.rb | 6 +- 11 files changed, 129 insertions(+), 74 deletions(-) diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index fbc2cefca..25409cc13 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -6,7 +6,7 @@ class TraceController < ApplicationController # target_user - if set, specifies the user to fetch traces for. if not set will fetch all traces # paging_action - the action that will be linked back to from view def list (target_user = nil, paging_action = 'list') - @traces_per_page = 4 + @traces_per_page = 20 page_index = params[:page] ? params[:page].to_i - 1 : 0 # nice 1-based page -> 0-based page index # from display name, pick up user id if one user's traces only @@ -40,8 +40,9 @@ class TraceController < ApplicationController opt[:order] = 'timestamp DESC' if params[:tag] + @tag = params[:tag] conditions[0] += " AND gpx_file_tags.tag = ?" - conditions << params[:tag]; + conditions << @tag; end opt[:conditions] = conditions @@ -103,11 +104,15 @@ class TraceController < ApplicationController @trace.inserted = false @trace.user_id = @user.id @trace.timestamp = Time.now + saved_filename = "/tmp/#{@trace.id}.gpx" + # *nix - specific `mv #{filename} /tmp/#{@trace.id}.gpx` + File.rename(filename, saved_filename) + @trace.tmpname = saved_filename if @trace.save logger.info("id is #{@trace.id}") - File.rename(filename, "/tmp/#{@trace.id}.gpx") - # *nix - specific `mv #{filename} /tmp/#{@trace.id}.gpx` flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion." + else + #TODO upload failure end redirect_to :action => 'mine' diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index eaac774d2..4a4d4d855 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -2,6 +2,8 @@ class UserController < ApplicationController layout 'site' before_filter :authorize, :only => :preferences + before_filter :authorize_web, :only => :rename + def save @user = User.new(params[:user]) @@ -15,6 +17,21 @@ class UserController < ApplicationController render :action => 'new' end end + + def rename + new_name = params['display_name'] + if @user + @user.display_name = new_name + if @user.save + flash[:notice] = "User display name updated OK." + else + flash[:notice] = "Rename failed: #{ @user.errors.full_messages.join('; ') }." + end + else + flash[:notice] = 'not logged in' + end + redirect_to :back + end def lost_password if params['user']['email'] diff --git a/app/models/trace.rb b/app/models/trace.rb index f74b1d009..aefe78b60 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -1,6 +1,10 @@ class Trace < ActiveRecord::Base set_table_name 'gpx_files' + validates_presence_of :user_id, :name, :public, :description, :tmpname, :timestamp + validates_numericality_of :latitude, :longitude + validates_inclusion_of :inserted, :in => [ true, false] + belongs_to :user has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :destroy @@ -10,44 +14,44 @@ class Trace < ActiveRecord::Base tt.tag = tag tt } - end - - def large_picture= (data) - f = File.new(large_picture_name, "wb") - f.syswrite(data) - f.close - end - - def icon_picture= (data) - f = File.new(icon_picture_name, "wb") - f.syswrite(data) - f.close - end - - def large_picture - f = File.new(large_picture_name, "rb") - logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}" - data = f.sysread(File.size(f.path)) - logger.info "have read data, bytes: '#{data.length}'" - f.close - data - end - - def icon_picture - f = File.new(icon_picture_name, "rb") - logger.info "icon picture file: '#{f.path}'" - data = f.sysread(File.size(f.path)) - f.close - data - end - - # FIXME change to permanent filestore area - def large_picture_name - "/tmp/#{id}.gif" - end - - # FIXME change to permanent filestore area - def icon_picture_name - "/tmp/#{id}_icon.gif" - end + end + + def large_picture= (data) + f = File.new(large_picture_name, "wb") + f.syswrite(data) + f.close + end + + def icon_picture= (data) + f = File.new(icon_picture_name, "wb") + f.syswrite(data) + f.close + end + + def large_picture + f = File.new(large_picture_name, "rb") + logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}" + data = f.sysread(File.size(f.path)) + logger.info "have read data, bytes: '#{data.length}'" + f.close + data + end + + def icon_picture + f = File.new(icon_picture_name, "rb") + logger.info "icon picture file: '#{f.path}'" + data = f.sysread(File.size(f.path)) + f.close + data + end + + # FIXME change to permanent filestore area + def large_picture_name + "/tmp/#{id}.gif" + end + + # FIXME change to permanent filestore area + def icon_picture_name + "/tmp/#{id}_icon.gif" + end end diff --git a/app/models/user.rb b/app/models/user.rb index 4c7a26473..a8bc39615 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,10 +4,10 @@ class User < ActiveRecord::Base has_many :traces validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password' - validates_uniqueness_of :display_name + validates_uniqueness_of :display_name, :allow_nil => true validates_uniqueness_of :email validates_length_of :pass_crypt, :minimum => 8 - validates_length_of :display_name, :minimum => 3 + validates_length_of :display_name, :minimum => 3, :allow_nil => true validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i def set_defaults @@ -18,12 +18,12 @@ class User < ActiveRecord::Base def pass_crypt=(str) write_attribute("pass_crypt", Digest::MD5.hexdigest(str)) - end + end def pass_crypt_confirmation=(str) write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str)) - end - + end + def self.authenticate(email, passwd) find(:first, :conditions => [ "email = ? AND pass_crypt = ?", email, Digest::MD5.hexdigest(passwd)]) end diff --git a/app/views/layouts/site.rhtml b/app/views/layouts/site.rhtml index a0034a5ef..249db6576 100644 --- a/app/views/layouts/site.rhtml +++ b/app/views/layouts/site.rhtml @@ -80,9 +80,7 @@ - <%= yield :optionals %> -