Commit b76d09266b45c7c1ebd3c810d29e41c8149c84ef
1 parent
5a1392b5
Exists in
master
and in
22 other branches
ActionItem305: importing script
Showing
1 changed file
with
84 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,84 @@ | @@ -0,0 +1,84 @@ | ||
1 | +#!/usr/bin/ruby | ||
2 | +require File.dirname(__FILE__) + '/../config/environment' | ||
3 | + | ||
4 | +IMPORT_DIR = '/home/terceiro/src/cooperation-migration/data/export' | ||
5 | + | ||
6 | +if ARGV.size == 0 | ||
7 | + puts "usage: %s <username> [ <username> [ <username> ... ] ]" % $PROGRAM_NAME | ||
8 | + exit(1) | ||
9 | +end | ||
10 | + | ||
11 | +def password(username) | ||
12 | + # FIXME | ||
13 | + '123456' | ||
14 | +end | ||
15 | + | ||
16 | +def domainname(envname) | ||
17 | + # FIXME remove the suffix | ||
18 | + envname + '.local' | ||
19 | +end | ||
20 | + | ||
21 | +TinyMceArticle # forces loading the Noosfero class before adding stuff to it | ||
22 | +class TinyMceArticle | ||
23 | + attr_accessor :is_homepage | ||
24 | +end | ||
25 | + | ||
26 | + | ||
27 | +def import_environment(name) | ||
28 | + environment = Environment.find_by_name(name) | ||
29 | + if environment | ||
30 | + puts "I: environment <#{name}> already exists, loading it." | ||
31 | + else | ||
32 | + environment = Environment.create!(:name => name) | ||
33 | + Domain.create!(:name => domainname(name), :owner => environment) | ||
34 | + puts "I: environment <#{name}> created." | ||
35 | + end | ||
36 | + environment | ||
37 | +end | ||
38 | + | ||
39 | +for username in ARGV | ||
40 | + begin | ||
41 | + User.transaction do | ||
42 | + # guess environment | ||
43 | + envname = username.gsub(/^.*@/, '') | ||
44 | + environment = import_environment(envname) | ||
45 | + | ||
46 | + # create user | ||
47 | + login = username.gsub(/@.*$/, '') | ||
48 | + user = User.create!(:login => login, :email => username, :environment => environment, :password => password(username), :password_confirmation => password(username)) | ||
49 | + puts "I: user account for #{username} created" | ||
50 | + | ||
51 | + # import person data | ||
52 | + person = user.person | ||
53 | + person.from_xml(File.read(File.join(IMPORT_DIR, username + '.xml'))) | ||
54 | + puts "I: #{username} data imported" | ||
55 | + | ||
56 | + # import articles | ||
57 | + Dir.glob(File.join(IMPORT_DIR, username, 'articles', '*.xml')) do |xml| | ||
58 | + puts "I: Trying to import #{username}'s article in #{xml} ..." | ||
59 | + article = TinyMceArticle.new | ||
60 | + article.from_xml(File.read(xml)) | ||
61 | + article.profile = person | ||
62 | + if article.valid? | ||
63 | + article.save! | ||
64 | + puts "I: #{username}'s article #{article.name.inspect} imported" | ||
65 | + else | ||
66 | + $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" | ||
67 | + end | ||
68 | + | ||
69 | + if article.is_homepage | ||
70 | + person.home_page = article | ||
71 | + person.save! | ||
72 | + puts "I: Article #{article.name.inspect} is #{username}'s homepage!" | ||
73 | + end | ||
74 | + | ||
75 | + end | ||
76 | + end | ||
77 | + rescue Exception => e | ||
78 | + $stderr.puts "==================================" | ||
79 | + $stderr.puts "E: importing <#{username}> failed." | ||
80 | + $stderr.puts e | ||
81 | + $stderr.puts "==================================" | ||
82 | + $stderr.puts "E: Note that ALL operations above were CANCELLED due to these errors." | ||
83 | + end | ||
84 | +end |