Commit db16ebfe36bb1ad94d25eec6f8193c7fa4906912

Authored by Leandro Santos
1 parent 095879b5
Exists in master

adding script to load data

controllers/myprofile/juventude_plugin_myprofile_controller.rb
... ... @@ -7,6 +7,8 @@ class JuventudePluginMyprofileController < MyProfileController
7 7 report_path = Time.zone.now.strftime('%Y-%m-%d-%H-%M-%S')
8 8 JuventudePlugin::ReportJob.create_report_path(profile, report_path)
9 9 Delayed::Job.enqueue(JuventudePlugin::ReportJob.new(profile.id, report_path))
  10 + Delayed::Job.enqueue(JuventudePlugin::PeopleJob.new(profile.id, report_path))
  11 +
10 12 session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/%s") % report_path
11 13 redirect_to :back
12 14 end
... ...
lib/juventude_plugin/people_job.rb 0 → 100644
... ... @@ -0,0 +1,39 @@
  1 +# encoding: UTF-8
  2 +class JuventudePlugin::PeopleJob < JuventudePlugin::ReportJob
  3 +
  4 + ETNIA = { 1 => 'Pardo', 2 => 'Preto', 3 => 'Branco', 4 => 'Indígena', 5 => 'Amarelo'}
  5 + ORIENTACAO_SEXUAL = { 1 => 'Homosexual', 2 => 'Heterosexual', 3 => 'Bisexual', 4 => 'Assexual'}
  6 + GENERO = { 1 => 'Masculino', 2 => 'Feminino' }
  7 +
  8 + def perform
  9 + profile = Profile.find(profile_id)
  10 + report_folder = JuventudePlugin::ReportJob.create_report_path(profile, report_path)
  11 + create_people_report(profile, report_folder)
  12 + end
  13 +
  14 + def create_people_report(profile, report_folder)
  15 + people = Person.all
  16 + filepath = "/tmp/#{report_path}/people.csv"
  17 + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv|
  18 + csv << ['Identificador', 'Nome', 'Email', 'Orientação Sexual', 'Identidade de Gênero', 'Transgenero', 'Raça', 'Estado', 'Cidade']
  19 + count = 0
  20 + people.map do |person|
  21 + count += 1
  22 + puts "%s de %s: adicionando pessoa: %s" % [count, people.count, person.id ]
  23 + info = []
  24 + info.push(person.identifier)
  25 + info.push(person.name)
  26 + info.push(person.email)
  27 + info.push(ORIENTACAO_SEXUAL[person.orientacao_sexual.to_i])
  28 + info.push(GENERO[person.identidade_genero.to_i])
  29 + info.push(person.transgenero)
  30 + info.push(ETNIA[person.etnia.to_i])
  31 + info.push(person.city ? person.city.name : '')
  32 + info.push(person.state ? person.state.name : '')
  33 + csv << info
  34 + end
  35 + end
  36 + upload_file(compress_files('people', 'people.csv'), profile, report_folder)
  37 + end
  38 +
  39 +end
... ...
lib/juventude_plugin/report_job.rb
... ... @@ -31,31 +31,21 @@ class JuventudePlugin::ReportJob &lt; Struct.new(:profile_id, :report_path)
31 31 filepath = "/tmp/#{report_path}/propostas.csv"
32 32  
33 33 CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv|
34   - tasks = ProposalsDiscussionPlugin::ProposalTask.all
  34 + proposals = ProposalsDiscussionPlugin::Proposal.all
35 35 count = 0
36   - csv << ['Origem', 'Status', 'Criada em', 'Moderado por', 'Data de Moderado', 'Validado por', 'Data de Validado', 'Autor', 'Proposta', 'Categorias', 'Tema']
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|
  36 + csv << ['Identificador','Criada em', 'Autor', 'Titulo', 'Proposta', 'Comentarios', 'Seguidores', 'Votos']
  37 + proposals.map do |proposal|
45 38 count += 1
46   - puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ]
  39 + puts "%s de %s: adicionando proposta: %s" % [count, proposals.count, proposal.id ]
47 40 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(' '))
  41 + info.push(proposal.id)
  42 + info.push(proposal.created_at.strftime("%d/%m/%y %H:%M"))
  43 + info.push(proposal.author ? proposal.author.identifier : '')
  44 + info.push(proposal.title)
  45 + info.push(proposal.abstract.present? ? proposal.abstract.gsub(/\s+/, ' ').strip : '')
  46 + info.push(proposal.comments_count)
  47 + info.push(proposal.followers.count)
  48 + info.push(proposal.votes_for)
59 49 csv << info
60 50 end
61 51 end
... ...