Commit 374888e82e32962f25d6223de2a9abc6f931ff52
1 parent
0920b07f
Exists in
master
and in
29 other branches
ActionItem305: importing attachments
Showing
1 changed file
with
37 additions
and
0 deletions
Show diff stats
script/import-cooperation.net
@@ -22,7 +22,19 @@ TinyMceArticle # forces loading the Noosfero class before adding stuff to it | @@ -22,7 +22,19 @@ TinyMceArticle # forces loading the Noosfero class before adding stuff to it | ||
22 | class TinyMceArticle | 22 | class TinyMceArticle |
23 | attr_accessor :is_homepage | 23 | attr_accessor :is_homepage |
24 | end | 24 | end |
25 | +UploadedFile # force loading Noosfero class before addinf stuff to it | ||
26 | +class UploadedFile | ||
27 | + attr_accessor :is_homepage, :filesystem_location | ||
28 | +end | ||
25 | 29 | ||
30 | +class FileData < StringIO | ||
31 | + attr_reader :original_filename, :content_type | ||
32 | + def initialize(actual_filename, name, content_type) | ||
33 | + @original_filename = name | ||
34 | + @content_type = content_type | ||
35 | + super(File.read(actual_filename)) | ||
36 | + end | ||
37 | +end | ||
26 | 38 | ||
27 | def import_environment(name) | 39 | def import_environment(name) |
28 | environment = Environment.find_by_name(name) | 40 | environment = Environment.find_by_name(name) |
@@ -64,6 +76,7 @@ for username in ARGV | @@ -64,6 +76,7 @@ for username in ARGV | ||
64 | puts "I: #{username}'s article #{article.name.inspect} imported" | 76 | puts "I: #{username}'s article #{article.name.inspect} imported" |
65 | else | 77 | else |
66 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" | 78 | $stderr.puts "W: #{username}'s article #{article.name.inspect} cannot be saved. Errors: #{article.errors.full_messages.join(', ')}" |
79 | + next | ||
67 | end | 80 | end |
68 | 81 | ||
69 | if article.is_homepage | 82 | if article.is_homepage |
@@ -72,12 +85,36 @@ for username in ARGV | @@ -72,12 +85,36 @@ for username in ARGV | ||
72 | puts "I: Article #{article.name.inspect} is #{username}'s homepage!" | 85 | puts "I: Article #{article.name.inspect} is #{username}'s homepage!" |
73 | end | 86 | end |
74 | 87 | ||
88 | + # import attachments | ||
89 | + attachments_dir = xml.gsub(/\.xml$/, '') | ||
90 | + Dir.glob(File.join(attachments_dir, 'attachments', '*.xml')).each do |attachment_xml| | ||
91 | + file = UploadedFile.new | ||
92 | + file.from_xml(File.read(attachment_xml)) | ||
93 | + puts "I: about to read data from #{file.filesystem_location} (xml: #{attachment_xml})" | ||
94 | + file.uploaded_data = FileData.new(file.filesystem_location, file.filename, file.content_type) | ||
95 | + file.parent = article | ||
96 | + file.profile = person | ||
97 | + file.save! | ||
98 | + puts "I: attachment added to article #{article.id}" | ||
99 | + | ||
100 | + file.reload | ||
101 | + if file.image? | ||
102 | + file.parent.body = "<img align='left' style='margin-right: 5px; margin-bottom: 5px;' src='/#{login}/#{file.path}'/> " + file.parent.body | ||
103 | + else | ||
104 | + if file.parent.body | ||
105 | + file.parent.body += "<hr/><div><a href='/#{login}/#{file.path}'>#{file.abstract}</a></div>" | ||
106 | + end | ||
107 | + end | ||
108 | + file.parent.save | ||
109 | + | ||
110 | + end | ||
75 | end | 111 | end |
76 | end | 112 | end |
77 | rescue Exception => e | 113 | rescue Exception => e |
78 | $stderr.puts "==================================" | 114 | $stderr.puts "==================================" |
79 | $stderr.puts "E: importing <#{username}> failed." | 115 | $stderr.puts "E: importing <#{username}> failed." |
80 | $stderr.puts e | 116 | $stderr.puts e |
117 | + $stderr.puts e.backtrace | ||
81 | $stderr.puts "==================================" | 118 | $stderr.puts "==================================" |
82 | $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." | 119 | $stderr.puts "E: Note that ALL operations above relative to <#{username}> were CANCELLED due to these errors." |
83 | end | 120 | end |