Commit 83e469707f4092af91be19e928e59e9f9a5299e6
1 parent
3bdce461
Exists in
export-comment-paragraph
Added export comments to CSV
Showing
3 changed files
with
86 additions
and
3 deletions
Show diff stats
plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb
1 | +require 'csv' | ||
1 | class CommentParagraphPluginProfileController < ProfileController | 2 | class CommentParagraphPluginProfileController < ProfileController |
2 | append_view_path File.join(File.dirname(__FILE__) + '/../../views') | 3 | append_view_path File.join(File.dirname(__FILE__) + '/../../views') |
3 | 4 | ||
@@ -11,4 +12,68 @@ class CommentParagraphPluginProfileController < ProfileController | @@ -11,4 +12,68 @@ class CommentParagraphPluginProfileController < ProfileController | ||
11 | render :partial => 'comment/comment.html.erb', :collection => @comments | 12 | render :partial => 'comment/comment.html.erb', :collection => @comments |
12 | end | 13 | end |
13 | 14 | ||
15 | + def export_comments | ||
16 | + article_id = params[:id] | ||
17 | + article = profile.articles.find(article_id) | ||
18 | + doc = Nokogiri::HTML(article.body) | ||
19 | + comments = article.comments | ||
20 | + commentsMap = comments.group_by { |cmt| cmt.paragraph_uuid } | ||
21 | + @export = [] | ||
22 | + doc.css("[data-macro-paragraph_uuid]").map do |paragraph| | ||
23 | + uuid = paragraph.attributes['data-macro-paragraph_uuid'].value | ||
24 | + comments_for_paragraph = commentsMap[uuid] | ||
25 | + if comments_for_paragraph | ||
26 | + # Put comments for the paragraph | ||
27 | + comments_for_paragraph.each do | cmt | | ||
28 | + element = Hash.new | ||
29 | + paragraph_text = paragraph.present? ? paragraph.text : nil | ||
30 | + element["paragraph_text"] = paragraph_text | ||
31 | + element["comment_id"] = cmt.id | ||
32 | + element["comment_content"] = cmt.body | ||
33 | + element["comment_author_name"] = cmt.author_name | ||
34 | + element["comment_author_email"] = cmt.author_email | ||
35 | + @export << element | ||
36 | + end | ||
37 | + else | ||
38 | + # There are no comments for this paragraph | ||
39 | + element = Hash.new | ||
40 | + paragraph_text = paragraph.present? ? paragraph.text : nil | ||
41 | + element["paragraph_text"] = paragraph_text | ||
42 | + @export << element | ||
43 | + end | ||
44 | + end | ||
45 | + # Now we need to put all other comments that are not attached to a paragraph | ||
46 | + comments_without_paragrah = commentsMap[nil] | ||
47 | + if (comments_without_paragrah) | ||
48 | + comments_without_paragrah.each do | cmt | | ||
49 | + element = Hash.new | ||
50 | + element["paragraph_text"] = "" | ||
51 | + element["comment_id"] = cmt.id | ||
52 | + element["comment_content"] = cmt.body | ||
53 | + element["comment_author_name"] = cmt.author_name | ||
54 | + element["comment_author_email"] = cmt.author_email | ||
55 | + @export << element | ||
56 | + end | ||
57 | + end | ||
58 | + if (@export.first) | ||
59 | + column_names = @export.first.keys | ||
60 | + header = "Comments for article[#{article_id}]: #{article.path}\n\n" | ||
61 | + s=CSV.generate do |csv| | ||
62 | + csv << column_names | ||
63 | + @export.each do |x| | ||
64 | + csv << x.values | ||
65 | + end | ||
66 | + end | ||
67 | + result = header + s | ||
68 | + else | ||
69 | + result = "No comments for article[#{article_id}]: #{article.path}\n\n" | ||
70 | + end | ||
71 | + fname = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv" | ||
72 | + send_data result, | ||
73 | + :type => 'text/csv; charset=UTF-8; header=present', | ||
74 | + :disposition => "attachment; filename=#{fname}" | ||
75 | + end | ||
76 | + | ||
77 | + | ||
78 | + | ||
14 | end | 79 | end |
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
@@ -47,12 +47,18 @@ class CommentParagraphPlugin < Noosfero::Plugin | @@ -47,12 +47,18 @@ class CommentParagraphPlugin < Noosfero::Plugin | ||
47 | def article_extra_toolbar_buttons(article) | 47 | def article_extra_toolbar_buttons(article) |
48 | user = context.send :user | 48 | user = context.send :user |
49 | return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(user) | 49 | return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(user) |
50 | - { | 50 | + [ |
51 | + { | ||
51 | :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'), | 52 | :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'), |
52 | :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id}, | 53 | :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id}, |
53 | :icon => :toggle_comment_paragraph | 54 | :icon => :toggle_comment_paragraph |
54 | - } | ||
55 | - | 55 | + }, |
56 | + { | ||
57 | + :title => _('Export Comments'), | ||
58 | + :url => {:controller => 'comment_paragraph_plugin_profile', :profile => article.profile.identifier, :action => 'export_comments', :id => article.id}, | ||
59 | + :icon => :toggle_comment_paragraph | ||
60 | + } | ||
61 | + ] | ||
56 | end | 62 | end |
57 | 63 | ||
58 | end | 64 | end |
plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb
@@ -39,4 +39,16 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase | @@ -39,4 +39,16 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase | ||
39 | assert_match /d comment/, @response.body | 39 | assert_match /d comment/, @response.body |
40 | end | 40 | end |
41 | 41 | ||
42 | + should 'export comments as CSV' do | ||
43 | + fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'a comment', :paragraph_uuid => nil) | ||
44 | + fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'b comment', :body => 'b comment', :paragraph_uuid => nil) | ||
45 | + xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id | ||
46 | + assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type | ||
47 | + assert_equal "Comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] | ||
48 | + end | ||
49 | + | ||
50 | + should 'not export any comments as CSV' do | ||
51 | + xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id | ||
52 | + assert_equal "No comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] | ||
53 | + end | ||
42 | end | 54 | end |