Commit 095879b582a77d9a510e3eb0465a9ce5966912ef

Authored by Leandro Santos
1 parent abd9f8d5
Exists in master

generating initial report

controllers/myprofile/juventude_plugin_myprofile_controller.rb 0 → 100644
... ... @@ -0,0 +1,21 @@
  1 +# encoding: UTF-8
  2 +class JuventudePluginMyprofileController < 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 + JuventudePlugin::ReportJob.create_report_path(profile, report_path)
  9 + Delayed::Job.enqueue(JuventudePlugin::ReportJob.new(profile.id, report_path))
  10 + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/%s") % report_path
  11 + redirect_to :back
  12 + end
  13 +
  14 + protected
  15 +
  16 + def is_admin
  17 + render_access_denied unless current_person.is_admin?
  18 + end
  19 +
  20 +
  21 +end
... ...
lib/juventude_plugin/report_job.rb 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +require 'csv'
  2 +class JuventudePlugin::ReportJob < 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 "/tmp/#{report_path}"
  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 = JuventudePlugin::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']
  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 + csv << info
  60 + end
  61 + end
  62 + upload_file(filepath, profile, report_folder)
  63 + end
  64 +
  65 +end
... ...