From b9007961bf394e46c8a4de26ac7ec3fb022054e4 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Tue, 11 Aug 2009 15:14:09 -0300 Subject: [PATCH] ActionItem1165: fixing import script --- app/models/user.rb | 4 ++-- script/import-cooperation.net | 28 +++++++++++++++------------- test/functional/account_controller_test.rb | 5 +++-- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 0bf1999..95ece25 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,9 +29,9 @@ class User < ActiveRecord::Base after_create do |user| user.person ||= Person.new - user.person.attributes = user.person_data + user.person.attributes = user.person_data.merge(:identifier => user.login, :user_id => user.id, :environment_id => user.environment_id) user.person.name ||= user.login - user.person.update_attributes(:identifier => user.login, :user_id => user.id, :environment_id => user.environment_id) + user.person.save! end attr_writer :person_data diff --git a/script/import-cooperation.net b/script/import-cooperation.net index 6b9f54e..46834ea 100755 --- a/script/import-cooperation.net +++ b/script/import-cooperation.net @@ -30,12 +30,7 @@ class UploadedFile attr_accessor :is_homepage, :filesystem_location end -Person # forces loading Noosfero class before adding stuff to id -class Person - delegate :crypted_password=, :to => :user - delegate :salt=, :to => :user - delegate :password_type=, :to => :user -end +Person class FileData < StringIO attr_reader :original_filename, :content_type @@ -47,7 +42,7 @@ class FileData < StringIO end class Progress - DEBUG = false + DEBUG = true class << self def start(n) @total = n @@ -59,6 +54,9 @@ class Progress def step_done say('=> done', true) end + def step_failed + say('=> FAILED', true) + end def say(msg, force = Progress::DEBUG) puts("[%d/%d] %s" % [@current, @total, msg]) if force end @@ -86,14 +84,17 @@ for username in ARGV user = User.new(:login => login, :email => username, :environment => environment) # import person data - person = Person.new(:identifier => login) - user.person = person - person.user = user - person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) - person.preferred_domain = get_domain(domain_name) + imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] + user.crypted_password = imported_data.delete(:crypted_password.to_s) + user.salt = imported_data.delete(:salt.to_s) + user.password_type = imported_data.delete(:password_type.to_s) + + user.person_data = imported_data.merge('preferred_domain_id' => get_domain(domain_name).id) user.save! Progress.say "I: #{username} data imported" + person = user.person + # import articles Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| Progress.say "I: Trying to import #{username}'s article in #{xml} ..." @@ -165,6 +166,7 @@ for username in ARGV end end + Progress.step_done rescue Exception => e $stderr.puts "==================================" $stderr.puts "E: importing <#{username}> failed." @@ -172,6 +174,6 @@ for username in ARGV $stderr.puts e.backtrace $stderr.puts "==================================" $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." + Progress.step_failed end - Progress.step_done end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 9f62216..2ddef48 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -636,11 +636,12 @@ class AccountControllerTest < Test::Unit::TestCase end should 'signup filling in mandatory person fields' do - Person.any_instance.stubs(:required_fields).returns(['organization_website']) + Person.any_instance.stubs(:required_fields).returns(['organization']) assert_difference User, :count do - post :signup, :user => { :login => 'testuser', :password => '123456', :password_confirmation => '123456', :email => 'testuser@example.com' }, :profile_data => { :organization_website => 'example.com' } + post :signup, :user => { :login => 'testuser', :password => '123456', :password_confirmation => '123456', :email => 'testuser@example.com' }, :profile_data => { :organization => 'example.com' } assert_redirected_to :controller => 'profile_editor', :profile => 'testuser' end + assert_equal 'example.com', Person['testuser'].organization end protected -- libgit2 0.21.2