Commit 2bbc5327c39b368e46429c95bb21a50944e867d0

Authored by Leandro Santos
1 parent df0c5164
Exists in export_data

add export data plugin

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