diff --git a/plugins/export_data/controllers/myprofile/export_plugin_myprofile_controller.rb b/plugins/export_data/controllers/myprofile/export_plugin_myprofile_controller.rb new file mode 100644 index 0000000..47835ec --- /dev/null +++ b/plugins/export_data/controllers/myprofile/export_plugin_myprofile_controller.rb @@ -0,0 +1,23 @@ +# encoding: UTF-8 +class ExportPluginMyprofileController < MyProfileController + + before_filter :is_admin + + def send_report + report_path = Time.zone.now.strftime('%Y-%m-%d-%H-%M-%S') + DialogaPlugin::ReportJob.create_report_path(profile, report_path) + Delayed::Job.enqueue(DialogaPlugin::ReportJob.new(profile.id, report_path)) + Delayed::Job.enqueue(DialogaPlugin::RankingJob.new(profile.id, report_path)) + Delayed::Job.enqueue(DialogaPlugin::EventJob.new(profile.id, report_path)) + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/%s") % report_path + redirect_to :back + end + + protected + + def is_admin + render_access_denied unless current_person.is_admin? + end + + +end diff --git a/plugins/export_data/lib/export_data_plugin.rb b/plugins/export_data/lib/export_data_plugin.rb new file mode 100644 index 0000000..00f6601 --- /dev/null +++ b/plugins/export_data/lib/export_data_plugin.rb @@ -0,0 +1,15 @@ +class ExportDataPlugin < Noosfero::Plugin + + def self.plugin_name + _('Export data plugin') + end + + def self.plugin_description + _("Provide a plugin to export all noosfero data.") + end + + def self.api_mount_points + [ExportDataPlugin::API ] + end + +end diff --git a/plugins/export_data/lib/export_data_plugin/api.rb b/plugins/export_data/lib/export_data_plugin/api.rb new file mode 100644 index 0000000..2049155 --- /dev/null +++ b/plugins/export_data/lib/export_data_plugin/api.rb @@ -0,0 +1,9 @@ +class ExportDataPlugin::API < Grape::API + + resource :export_data_plugin do + get 'available' do + present Article.first, :with => API::Entities::Article, :fields => params[:fields] + end + end + +end diff --git a/plugins/export_data/lib/export_data_plugin/article_job.rb b/plugins/export_data/lib/export_data_plugin/article_job.rb new file mode 100644 index 0000000..1b52b37 --- /dev/null +++ b/plugins/export_data/lib/export_data_plugin/article_job.rb @@ -0,0 +1,31 @@ +# encoding: UTF-8 +class ExportDataPlugin::ArticleJob < ExportDataPlugin::ExportJob + + def perform + profile = Profile.find(profile_id) + report_folder = DialogaPlugin::ReportJob.create_report_path(profile, report_path) + create_event_report(profile, report_folder) + end + + def create_event_report(profile, report_folder) + events = Event.where(:profile_id => profile.id) + events.map do |event| + filepath = "/tmp/#{report_path}/evento-#{event.slug}.csv" + count = 0 + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv| + csv << [event.name] + csv << ['Nome', 'Email'] + event.person_followers.map do |person| + count += 1 + puts "%s de %s: adicionando evento: %s" % [count, event.person_followers.count, event.id ] + info = [] + info.push(person.name) + info.push(person.email) + csv << info + end + end + end + upload_file(compress_files('eventos', 'evento-*'), profile, report_folder) + end + +end diff --git a/plugins/export_data/lib/export_data_plugin/community_job.rb b/plugins/export_data/lib/export_data_plugin/community_job.rb new file mode 100644 index 0000000..3802181 --- /dev/null +++ b/plugins/export_data/lib/export_data_plugin/community_job.rb @@ -0,0 +1,29 @@ +# encoding: UTF-8 +require 'csv' +class ExportDataPlugin::RankingJob < ExportDataPlugin::ExportJob + + def perform + profile = Profile.find(profile_id) + report_folder = DialogaPlugin::ReportJob.create_report_path(profile, report_path) + create_ranking_report(profile, report_folder) + end + + def create_ranking_report(profile, report_folder) + ProposalsDiscussionPlugin::Discussion.where(:profile_id => profile.id).map do |discussion| + articles = discussion.topics + articles.each do |article| + puts "#{article.slug}" + ranking = article.ranking + next if ranking.empty? + + filepath = "/tmp/#{report_path}/ranking-#{discussion.slug}_#{article.slug}.csv" + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true}) do |csv| + csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor', 'Autor', 'Email do Autor'] + ranking.each_with_index {|r, i| csv << [r.position, r.id, r.abstract, r.votes_for, r.votes_against, r.hits, r.effective_support, r.proposal.author.present? ? r.proposal.author.name : r.proposal.author_name, r.proposal.author.present? ? r.proposal.author.email : ''].flatten} + end + end + end + upload_file(compress_files('rankings', 'ranking-*'), profile, report_folder) + end + +end diff --git a/plugins/export_data/lib/export_data_plugin/export_job.rb b/plugins/export_data/lib/export_data_plugin/export_job.rb new file mode 100644 index 0000000..2213761 --- /dev/null +++ b/plugins/export_data/lib/export_data_plugin/export_job.rb @@ -0,0 +1,66 @@ +require 'csv' +class ExportDataPlugin::ExportJob < Struct.new(:profile_id, :report_path) + + include ActionDispatch::TestProcess + + def self.create_report_path(profile, report_path) +# root_report_folder = profile.folders.where(:slug => 'relatorios').first +# root_report_folder ||= Folder.create!(:profile => profile, :name => 'Relatorios') + FileUtils.mkdir_p(File.join(Rails.root,'public',profile.environment.identifier,profile.identifier)) +# report_folder = Folder.find_by_slug(report_path) +# report_folder ||= Folder.create!(:profile => profile, :name => report_path, :parent => root_report_folder) + end + + def upload_file(filepath, profile, report_folder) + UploadedFile.create!(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) + end + + def compress_files(filename, pattern) + filepath = "/tmp/#{report_path}/#{filename}.zip" + system("cd /tmp/#{report_path} && zip #{filepath} #{pattern}") + filepath + end + + def perform + profile = Profile.find(profile_id) + report_folder = DialogaPlugin::ReportJob.create_report_path(profile, report_path) + create_proposals_report(profile, report_folder) + end + + def create_proposals_report(profile, report_folder) + filepath = "/tmp/#{report_path}/propostas.csv" + + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv| + tasks = ProposalsDiscussionPlugin::ProposalTask.all + count = 0 + csv << ['Origem', 'Status', 'Criada em', 'Moderado por', 'Data de Moderado', 'Validado por', 'Data de Validado', 'Autor', 'Proposta', 'Categorias', 'Tema', 'Email do Autor'] + status_translation = { + 1 => 'Pendente de Moderacao', + 2 => 'Rejeitada', + 3 => 'Aprovada', + 5 => 'Pre Aprovada', + 6 => 'Pre Rejeitada', + } + tasks.map do |task| + count += 1 + puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ] + info = [] + info.push(task.proposal_source) + info.push(status_translation[task.status]) + info.push(task.created_at.strftime("%d/%m/%y %H:%M")) + info.push(task.proposal_evaluation.present? && task.proposal_evaluation.evaluated_by.present? ? task.proposal_evaluation.evaluated_by.name : '') + info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.created_at.strftime("%d/%m/%y %H:%M") : '') + info.push(task.closed_by.present? ? task.closed_by.name : '') + info.push(task.closed_by.present? ? task.end_date.strftime("%d/%m/%y %H:%M") : '') + info.push(task.requestor.present? ? task.requestor.name : '') + info.push(task.abstract.present? ? task.abstract.gsub(/\s+/, ' ').strip : '') + info.push(task.categories.map {|c| c.name}.join(' ')) + info.push(task.article_parent.nil? ? '' : task.article_parent.categories.map(&:name).join(' ')) + info.push(task.requestor.present? ? task.requestor.email : '') + csv << info + end + end + upload_file(filepath, profile, report_folder) + end + +end diff --git a/plugins/export_data/po/dialoga.pot b/plugins/export_data/po/dialoga.pot new file mode 100644 index 0000000..41b6809 --- /dev/null +++ b/plugins/export_data/po/dialoga.pot @@ -0,0 +1,30 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 1.2~rc1-2843-g999a037\n" +"POT-Creation-Date: 2015-08-06 08:53-0300\n" +"PO-Revision-Date: 2015-08-06 08:41-0300\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: plugins/dialoga/controllers/myprofile/dialoga_plugin_myprofile_controller.rb:12 +msgid "\"The report wil be sent to email #{current_person.email}\"" +msgstr "" + +#: plugins/dialoga/lib/dialoga_plugin.rb:4 +msgid "Dialoga custom plugin" +msgstr "" + +#: plugins/dialoga/lib/dialoga_plugin.rb:8 +msgid "Provide a plugin to dialoga environment." +msgstr "" diff --git a/plugins/export_data/po/pt/dialoga.po b/plugins/export_data/po/pt/dialoga.po new file mode 100644 index 0000000..af60abe --- /dev/null +++ b/plugins/export_data/po/pt/dialoga.po @@ -0,0 +1,30 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: 1.2~rc1-2843-g999a037\n" +"POT-Creation-Date: 2015-08-06 08:53-0300\n" +"PO-Revision-Date: 2015-08-06 08:41-0300\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: plugins/dialoga/controllers/myprofile/dialoga_plugin_myprofile_controller.rb:12 +msgid "\"The report wil be sent to email #{current_person.email}\"" +msgstr "O relatório será enviado para o email #{current_person.email}" + +#: plugins/dialoga/lib/dialoga_plugin.rb:4 +msgid "Dialoga custom plugin" +msgstr "Plugin com customizações para o Dialoga" + +#: plugins/dialoga/lib/dialoga_plugin.rb:8 +msgid "Provide a plugin to dialoga environment." +msgstr "Provê um plugin para o ambiente do dialoga" diff --git a/plugins/export_data/test/test_helper.rb b/plugins/export_data/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/export_data/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/export_data/test/unit/api_test.rb b/plugins/export_data/test/unit/api_test.rb new file mode 100644 index 0000000..3707afc --- /dev/null +++ b/plugins/export_data/test/unit/api_test.rb @@ -0,0 +1,13 @@ +require_relative '../test_helper' +require_relative '../../../../test/unit/api/test_helper' + +class APITest < ActiveSupport::TestCase + + def setup + login_api + end + + should 'return something' do + end + +end diff --git a/plugins/export_data/test/unit/proposal_test.rb b/plugins/export_data/test/unit/proposal_test.rb new file mode 100644 index 0000000..d5af957 --- /dev/null +++ b/plugins/export_data/test/unit/proposal_test.rb @@ -0,0 +1,38 @@ +require_relative '../test_helper' + +class ProposalTest < ActiveSupport::TestCase + + def setup + @profile = fast_create(Community) + @person = fast_create(Person) + @discussion = ProposalsDiscussionPlugin::Discussion.create!(:name => 'discussion', :profile => person, :allow_topics => false) + @proposal = ProposalsDiscussionPlugin::Proposal.new(:name => 'test', :abstract => 'abstract', :profile => @profile, :parent => @discussion) + @proposal.created_by = @person + @environment = Environment.default + environment.enable_plugin(DialogaPlugin) + end + + attr_reader :profile, :proposal, :person, :discussion, :environment + + should 'should not save a proposal with an abstract greater than 200 chars' do + proposal.abstract = 201.times.map{'B'}.join + refute proposal.valid? + refute proposal.save + assert proposal.errors[:abstract].present? + end + + should 'should save a proposal with an abstract greater than 200 chars and dialoga plugin is not enabled' do + environment.disable_plugin(DialogaPlugin) + proposal.abstract = 201.times.map{'B'}.join + assert proposal.valid? + assert proposal.save + end + + should 'should save a proposal with an abstract not greater than 200 chars' do + proposal.abstract = 200.times.map{'B'}.join + assert proposal.valid? + assert proposal.save + end + + +end -- libgit2 0.21.2