Commit ea7e701a29d9390deccd4600ff1be6a6e0ef1ec8

Authored by Victor Costa
1 parent 39fe1dc9
Exists in master

Fix report job

Showing 1 changed file with 28 additions and 29 deletions   Show diff stats
lib/dialoga_plugin/report_job.rb
  1 +require 'csv'
1 class DialogaPlugin::ReportJob < Struct.new(:profile_id, :report_path) 2 class DialogaPlugin::ReportJob < Struct.new(:profile_id, :report_path)
2 3
3 include ActionDispatch::TestProcess 4 include ActionDispatch::TestProcess
@@ -28,37 +29,35 @@ class DialogaPlugin::ReportJob &lt; Struct.new(:profile_id, :report_path) @@ -28,37 +29,35 @@ class DialogaPlugin::ReportJob &lt; Struct.new(:profile_id, :report_path)
28 29
29 def create_proposals_report(profile, report_folder) 30 def create_proposals_report(profile, report_folder)
30 filepath = "/tmp/#{report_path}/propostas.csv" 31 filepath = "/tmp/#{report_path}/propostas.csv"
31 - file = File.open(filepath, 'w+')  
32 32
33 - tasks = ProposalsDiscussionPlugin::ProposalTask.all  
34 - count = 0  
35 - header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta';'Categorias'\n"  
36 - file.write(header)  
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.categories.map {|c| c.name}.join(', '))  
57 - file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))  
58 - file.write("\n") 33 + CSV.open(filepath, 'w' ) 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']
  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 + csv << info
  59 + end
59 end 60 end
60 -  
61 - file.close  
62 upload_file(filepath, profile, report_folder) 61 upload_file(filepath, profile, report_folder)
63 end 62 end
64 63