diff --git a/controllers/myprofile/juventude_plugin_myprofile_controller.rb b/controllers/myprofile/juventude_plugin_myprofile_controller.rb new file mode 100644 index 0000000..4f8d7dd --- /dev/null +++ b/controllers/myprofile/juventude_plugin_myprofile_controller.rb @@ -0,0 +1,21 @@ +# encoding: UTF-8 +class JuventudePluginMyprofileController < MyProfileController + + before_filter :is_admin + + def send_report + 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)) + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/%s") % report_path + redirect_to :back + end + + protected + + def is_admin + render_access_denied unless current_person.is_admin? + end + + +end diff --git a/lib/juventude_plugin/report_job.rb b/lib/juventude_plugin/report_job.rb new file mode 100644 index 0000000..106f47b --- /dev/null +++ b/lib/juventude_plugin/report_job.rb @@ -0,0 +1,65 @@ +require 'csv' +class JuventudePlugin::ReportJob < Struct.new(:profile_id, :report_path) + + include ActionDispatch::TestProcess + + def self.create_report_path(profile, report_path) + root_report_folder = profile.folders.where(:slug => 'relatorios').first + root_report_folder ||= Folder.create!(:profile => profile, :name => 'Relatorios') + FileUtils.mkdir_p "/tmp/#{report_path}" + report_folder = Folder.find_by_slug(report_path) + report_folder ||= Folder.create!(:profile => profile, :name => report_path, :parent => root_report_folder) + end + + def upload_file(filepath, profile, report_folder) + UploadedFile.create!(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) + end + + def compress_files(filename, pattern) + filepath = "/tmp/#{report_path}/#{filename}.zip" + system("cd /tmp/#{report_path} && zip #{filepath} #{pattern}") + filepath + end + + def perform + profile = Profile.find(profile_id) + report_folder = JuventudePlugin::ReportJob.create_report_path(profile, report_path) + create_proposals_report(profile, report_folder) + end + + def create_proposals_report(profile, report_folder) + filepath = "/tmp/#{report_path}/propostas.csv" + + CSV.open(filepath, 'w', {:col_sep => ';', :force_quotes => true} ) do |csv| + tasks = ProposalsDiscussionPlugin::ProposalTask.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| + count += 1 + puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.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(' ')) + csv << info + end + end + upload_file(filepath, profile, report_folder) + end + +end -- libgit2 0.21.2