From db16ebfe36bb1ad94d25eec6f8193c7fa4906912 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Fri, 4 Sep 2015 14:33:24 -0300 Subject: [PATCH] adding script to load data --- controllers/myprofile/juventude_plugin_myprofile_controller.rb | 2 ++ lib/juventude_plugin/people_job.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/juventude_plugin/report_job.rb | 34 ++++++++++++---------------------- 3 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 lib/juventude_plugin/people_job.rb diff --git a/controllers/myprofile/juventude_plugin_myprofile_controller.rb b/controllers/myprofile/juventude_plugin_myprofile_controller.rb index 4f8d7dd..848c6ea 100644 --- a/controllers/myprofile/juventude_plugin_myprofile_controller.rb +++ b/controllers/myprofile/juventude_plugin_myprofile_controller.rb @@ -7,6 +7,8 @@ class JuventudePluginMyprofileController < MyProfileController report_path = Time.zone.now.strftime('%Y-%m-%d-%H-%M-%S') JuventudePlugin::ReportJob.create_report_path(profile, report_path) Delayed::Job.enqueue(JuventudePlugin::ReportJob.new(profile.id, report_path)) + Delayed::Job.enqueue(JuventudePlugin::PeopleJob.new(profile.id, report_path)) + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/%s") % report_path redirect_to :back end diff --git a/lib/juventude_plugin/people_job.rb b/lib/juventude_plugin/people_job.rb new file mode 100644 index 0000000..392b625 --- /dev/null +++ b/lib/juventude_plugin/people_job.rb @@ -0,0 +1,39 @@ +# encoding: UTF-8 +class JuventudePlugin::PeopleJob < JuventudePlugin::ReportJob + + ETNIA = { 1 => 'Pardo', 2 => 'Preto', 3 => 'Branco', 4 => 'Indígena', 5 => 'Amarelo'} + ORIENTACAO_SEXUAL = { 1 => 'Homosexual', 2 => 'Heterosexual', 3 => 'Bisexual', 4 => 'Assexual'} + GENERO = { 1 => 'Masculino', 2 => 'Feminino' } + + def perform + profile = Profile.find(profile_id) + report_folder = JuventudePlugin::ReportJob.create_report_path(profile, report_path) + create_people_report(profile, report_folder) + end + + def create_people_report(profile, report_folder) + people = Person.all + filepath = "/tmp/#{report_path}/people.csv" + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv| + csv << ['Identificador', 'Nome', 'Email', 'Orientação Sexual', 'Identidade de Gênero', 'Transgenero', 'Raça', 'Estado', 'Cidade'] + count = 0 + people.map do |person| + count += 1 + puts "%s de %s: adicionando pessoa: %s" % [count, people.count, person.id ] + info = [] + info.push(person.identifier) + info.push(person.name) + info.push(person.email) + info.push(ORIENTACAO_SEXUAL[person.orientacao_sexual.to_i]) + info.push(GENERO[person.identidade_genero.to_i]) + info.push(person.transgenero) + info.push(ETNIA[person.etnia.to_i]) + info.push(person.city ? person.city.name : '') + info.push(person.state ? person.state.name : '') + csv << info + end + end + upload_file(compress_files('people', 'people.csv'), profile, report_folder) + end + +end diff --git a/lib/juventude_plugin/report_job.rb b/lib/juventude_plugin/report_job.rb index 106f47b..1aaab76 100644 --- a/lib/juventude_plugin/report_job.rb +++ b/lib/juventude_plugin/report_job.rb @@ -31,31 +31,21 @@ class JuventudePlugin::ReportJob < Struct.new(:profile_id, :report_path) filepath = "/tmp/#{report_path}/propostas.csv" CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv| - tasks = ProposalsDiscussionPlugin::ProposalTask.all + proposals = ProposalsDiscussionPlugin::Proposal.all count = 0 - csv << ['Origem', 'Status', 'Criada em', 'Moderado por', 'Data de Moderado', 'Validado por', 'Data de Validado', 'Autor', 'Proposta', 'Categorias', 'Tema'] - status_translation = { - 1 => 'Pendente de Moderacao', - 2 => 'Rejeitada', - 3 => 'Aprovada', - 5 => 'Pre Aprovada', - 6 => 'Pre Rejeitada', - } - tasks.map do |task| + csv << ['Identificador','Criada em', 'Autor', 'Titulo', 'Proposta', 'Comentarios', 'Seguidores', 'Votos'] + proposals.map do |proposal| count += 1 - puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ] + puts "%s de %s: adicionando proposta: %s" % [count, proposals.count, proposal.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(proposal.id) + info.push(proposal.created_at.strftime("%d/%m/%y %H:%M")) + info.push(proposal.author ? proposal.author.identifier : '') + info.push(proposal.title) + info.push(proposal.abstract.present? ? proposal.abstract.gsub(/\s+/, ' ').strip : '') + info.push(proposal.comments_count) + info.push(proposal.followers.count) + info.push(proposal.votes_for) csv << info end end -- libgit2 0.21.2