Commit 9839cbc76f991a316f62605be79083123385045e
1 parent
7df56ce1
Exists in
staging
and in
42 other branches
ActionItem1165: checkpoint
Showing
2 changed files
with
46 additions
and
20 deletions
Show diff stats
script/import-cooperation.net
| ... | ... | @@ -3,6 +3,8 @@ require File.dirname(__FILE__) + '/../config/environment' |
| 3 | 3 | |
| 4 | 4 | include ActionController::UrlWriter |
| 5 | 5 | |
| 6 | +SCRIPT_TYPE = File.basename($PROGRAM_NAME) | |
| 7 | + | |
| 6 | 8 | IMPORT_DIR = ENV['IMPORT_DIR'] || '/home/terceiro/src/cooperation-migration/data/export' |
| 7 | 9 | |
| 8 | 10 | if ARGV.size == 0 |
| ... | ... | @@ -75,32 +77,49 @@ for username in ARGV |
| 75 | 77 | domain_name = username.gsub(/^.*@/, '') |
| 76 | 78 | environment = import_environment(domain_name) |
| 77 | 79 | |
| 78 | - # create user | |
| 79 | 80 | login = username.gsub(/@.*$/, '') |
| 80 | 81 | if FORBIDDEN_LOGINS.include?(login) |
| 81 | 82 | $stderr.puts "E: not importing #{username}, #{login} is not an allowed login" |
| 82 | 83 | next |
| 83 | 84 | end |
| 84 | - user = User.new(:login => login, :email => username, :environment => environment) | |
| 85 | - | |
| 86 | - # import person data | |
| 87 | - imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] | |
| 88 | - user.crypted_password = imported_data.delete(:crypted_password.to_s) | |
| 89 | - user.salt = imported_data.delete(:salt.to_s) | |
| 90 | - user.password_type = imported_data.delete(:password_type.to_s) | |
| 91 | 85 | |
| 92 | - user.person_data = imported_data.merge('preferred_domain_id' => get_domain(domain_name).id) | |
| 93 | - user.save! | |
| 94 | - Progress.say "I: #{username} data imported" | |
| 95 | - | |
| 96 | - person = user.person | |
| 86 | + profile = nil | |
| 87 | + if SCRIPT_TYPE == 'group-import-cooperation.net' | |
| 88 | + # import as a group | |
| 89 | + imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] | |
| 90 | + profile = Community.create!( | |
| 91 | + :identifier => login, | |
| 92 | + :name => login, | |
| 93 | + :contact_email => imported_data['email'], | |
| 94 | + :address => imported_data['address'], | |
| 95 | + :environment => environment, | |
| 96 | + :preferred_domain_id => get_domain(domain_name).id | |
| 97 | + ) | |
| 98 | + else | |
| 99 | + # import as a user | |
| 100 | + | |
| 101 | + # create user | |
| 102 | + user = User.new(:login => login, :email => username, :environment => environment) | |
| 103 | + | |
| 104 | + # import person data | |
| 105 | + imported_data = Hash.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml')))['person'] | |
| 106 | + user.crypted_password = imported_data.delete(:crypted_password.to_s) | |
| 107 | + user.salt = imported_data.delete(:salt.to_s) | |
| 108 | + user.password_type = imported_data.delete(:password_type.to_s) | |
| 109 | + | |
| 110 | + user.person_data = imported_data.merge('preferred_domain_id' => get_domain(domain_name).id) | |
| 111 | + user.save! | |
| 112 | + Progress.say "I: #{username} data imported" | |
| 113 | + | |
| 114 | + profile = user.person | |
| 115 | + end | |
| 97 | 116 | |
| 98 | 117 | # import articles |
| 99 | 118 | Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| |
| 100 | 119 | Progress.say "I: Trying to import #{username}'s article in #{xml} ..." |
| 101 | 120 | article = TinyMceArticle.new |
| 102 | 121 | article.from_xml(File.read(xml)) |
| 103 | - article.profile = person | |
| 122 | + article.profile = profile | |
| 104 | 123 | if article.valid? |
| 105 | 124 | article.save! |
| 106 | 125 | Progress.say "I: #{username}'s article #{article.name.inspect} imported" |
| ... | ... | @@ -110,13 +129,19 @@ for username in ARGV |
| 110 | 129 | end |
| 111 | 130 | |
| 112 | 131 | if article.is_homepage |
| 113 | - person.home_page = article | |
| 114 | - person.save! | |
| 132 | + profile.home_page = article | |
| 133 | + profile.save! | |
| 115 | 134 | Progress.say "I: Article #{article.name.inspect} is #{username}'s homepage!" |
| 116 | 135 | end |
| 117 | 136 | |
| 118 | - File.open("tmp/rewrite.txt", 'w+') do |file| | |
| 119 | - file.puts("#{article.id} #{url_for(article.url)}") | |
| 137 | + orig_article_number = File.basename(xml).sub(/\.xml$/, '') | |
| 138 | + | |
| 139 | + File.open("tmp/rewrite.txt", 'a') do |file| | |
| 140 | + [ '', '.html' ].each do |ext| | |
| 141 | + [ '', '/[a-z.]*'].each do |prefix| | |
| 142 | + file.puts("Redirect #{prefix}/info/#{orig_article_number}#{ext} #{url_for(article.url)}") | |
| 143 | + end | |
| 144 | + end | |
| 120 | 145 | Progress.say "I: Article with id = #{article.id} redirected to #{url_for(article.url)}" |
| 121 | 146 | end |
| 122 | 147 | |
| ... | ... | @@ -132,7 +157,7 @@ for username in ARGV |
| 132 | 157 | Progress.say "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" |
| 133 | 158 | file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) |
| 134 | 159 | file.parent = article |
| 135 | - file.profile = person | |
| 160 | + file.profile = profile | |
| 136 | 161 | Progress.say "I: Trying to save attachment \"#{file.filename}/#{file.slug}\"" |
| 137 | 162 | file.save! |
| 138 | 163 | Progress.say "I: attachment added to article #{article.id}" |
| ... | ... | @@ -158,7 +183,7 @@ for username in ARGV |
| 158 | 183 | data['menu']['items'].each do |item| |
| 159 | 184 | links << { :name => item['title'], :address => item['url'] } |
| 160 | 185 | end |
| 161 | - block = person.blocks.select { |block| block.class == LinkListBlock }[i-1] | |
| 186 | + block = profile.blocks.select { |block| block.class == LinkListBlock }[i-1] | |
| 162 | 187 | block.title = data['menu']['title'] |
| 163 | 188 | block.links = links |
| 164 | 189 | block.save! | ... | ... |