From 83e469707f4092af91be19e928e59e9f9a5299e6 Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Tue, 22 Dec 2015 17:48:26 -0300 Subject: [PATCH] Added export comments to CSV --- plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/comment_paragraph/lib/comment_paragraph_plugin.rb | 12 +++++++++--- plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb | 12 ++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb b/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb index 9743c3e..823a295 100644 --- a/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb +++ b/plugins/comment_paragraph/controllers/profile/comment_paragraph_plugin_profile_controller.rb @@ -1,3 +1,4 @@ +require 'csv' class CommentParagraphPluginProfileController < ProfileController append_view_path File.join(File.dirname(__FILE__) + '/../../views') @@ -11,4 +12,68 @@ class CommentParagraphPluginProfileController < ProfileController render :partial => 'comment/comment.html.erb', :collection => @comments end + def export_comments + article_id = params[:id] + article = profile.articles.find(article_id) + doc = Nokogiri::HTML(article.body) + comments = article.comments + commentsMap = comments.group_by { |cmt| cmt.paragraph_uuid } + @export = [] + doc.css("[data-macro-paragraph_uuid]").map do |paragraph| + uuid = paragraph.attributes['data-macro-paragraph_uuid'].value + comments_for_paragraph = commentsMap[uuid] + if comments_for_paragraph + # Put comments for the paragraph + comments_for_paragraph.each do | cmt | + element = Hash.new + paragraph_text = paragraph.present? ? paragraph.text : nil + element["paragraph_text"] = paragraph_text + element["comment_id"] = cmt.id + element["comment_content"] = cmt.body + element["comment_author_name"] = cmt.author_name + element["comment_author_email"] = cmt.author_email + @export << element + end + else + # There are no comments for this paragraph + element = Hash.new + paragraph_text = paragraph.present? ? paragraph.text : nil + element["paragraph_text"] = paragraph_text + @export << element + end + end + # Now we need to put all other comments that are not attached to a paragraph + comments_without_paragrah = commentsMap[nil] + if (comments_without_paragrah) + comments_without_paragrah.each do | cmt | + element = Hash.new + element["paragraph_text"] = "" + element["comment_id"] = cmt.id + element["comment_content"] = cmt.body + element["comment_author_name"] = cmt.author_name + element["comment_author_email"] = cmt.author_email + @export << element + end + end + if (@export.first) + column_names = @export.first.keys + header = "Comments for article[#{article_id}]: #{article.path}\n\n" + s=CSV.generate do |csv| + csv << column_names + @export.each do |x| + csv << x.values + end + end + result = header + s + else + result = "No comments for article[#{article_id}]: #{article.path}\n\n" + end + fname = "comments_for_article#{article_id}_#{DateTime.now.to_i}.csv" + send_data result, + :type => 'text/csv; charset=UTF-8; header=present', + :disposition => "attachment; filename=#{fname}" + end + + + end diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb index f0809fe..1129dad 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb @@ -47,12 +47,18 @@ class CommentParagraphPlugin < Noosfero::Plugin def article_extra_toolbar_buttons(article) user = context.send :user return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(user) - { + [ + { :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'), :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id}, :icon => :toggle_comment_paragraph - } - + }, + { + :title => _('Export Comments'), + :url => {:controller => 'comment_paragraph_plugin_profile', :profile => article.profile.identifier, :action => 'export_comments', :id => article.id}, + :icon => :toggle_comment_paragraph + } + ] end end diff --git a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb index edaf8aa..f28d9de 100644 --- a/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb +++ b/plugins/comment_paragraph/test/functional/comment_paragraph_plugin_profile_controller_test.rb @@ -39,4 +39,16 @@ class CommentParagraphPluginProfileControllerTest < ActionController::TestCase assert_match /d comment/, @response.body end + should 'export comments as CSV' do + fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :author_id => profile, :title => 'a comment', :body => 'a comment', :paragraph_uuid => nil) + fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :author_id => profile, :title => 'b comment', :body => 'b comment', :paragraph_uuid => nil) + xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id + assert_equal 'text/csv; charset=UTF-8; header=present', @response.content_type + assert_equal "Comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] + end + + should 'not export any comments as CSV' do + xhr :get, :export_comments, :profile => @profile.identifier, :id => article.id + assert_equal "No comments for article[#{article.id}]: #{article.path}", @response.body.split("\n")[0] + end end -- libgit2 0.21.2