diff options
Diffstat (limited to 'app')
8 files changed, 71 insertions, 33 deletions
diff --git a/app/controllers/compliance_check_results_controller.rb b/app/controllers/compliance_check_results_controller.rb index 85ba23e38..45136938a 100644 --- a/app/controllers/compliance_check_results_controller.rb +++ b/app/controllers/compliance_check_results_controller.rb @@ -2,24 +2,24 @@ class ComplianceCheckResultsController < ChouetteController respond_to :json respond_to :js, :only => :index - respond_to :csv, :only => :index + #respond_to :csv, :only => :index belongs_to :compliance_check_task def index index! do |format| format.html { render :layout => false } - format.csv { send_file collection[0].compliance_check_task.file, :type => "text/html; charset=utf-8" } + #format.csv { send_file collection[0].compliance_check_task.file, :type => "text/html; charset=utf-8" } end end - + def collection wheres = [:status, :severity].map{|key| params.has_key?(key) ? {key => params[key]} : {} }\ .inject({}){|hash, injected| hash.merge!(injected)} @compliance_check_results ||= end_of_association_chain.where(wheres).order(:rule_code) end - + def rule_parameter_set @rule_parameter_set = RuleParameterSet.new.tap { |rps| rps.parameters = resource.parameter_set } end - + end diff --git a/app/controllers/compliance_check_tasks_controller.rb b/app/controllers/compliance_check_tasks_controller.rb index d28ca34ae..9964407ce 100644 --- a/app/controllers/compliance_check_tasks_controller.rb +++ b/app/controllers/compliance_check_tasks_controller.rb @@ -2,6 +2,7 @@ class ComplianceCheckTasksController < ChouetteController defaults :resource_class => ComplianceCheckTask respond_to :html, :js + respond_to :zip, :only => :export belongs_to :referential @@ -25,6 +26,12 @@ class ComplianceCheckTasksController < ChouetteController success.html { flash[:notice] = I18n.t('compliance_check_tasks.new.flash'); redirect_to referential_compliance_check_tasks_path(@referential) } end end + + def export + respond_to do |format| + format.zip { send_file ComplianceCheckTaskExport.new(compliance_check_task).export, :type => :zip } + end + end protected diff --git a/app/models/compliance_check_task.rb b/app/models/compliance_check_task.rb index 9240639a5..1808a792c 100644 --- a/app/models/compliance_check_task.rb +++ b/app/models/compliance_check_task.rb @@ -1,9 +1,6 @@ -require 'tempfile' - class ComplianceCheckTask < ActiveRecord::Base - include ERB::Util - attr_accessor :rule_parameter_set_id, :template + attr_accessor :rule_parameter_set_id belongs_to :referential belongs_to :import_task @@ -13,7 +10,7 @@ class ComplianceCheckTask < ActiveRecord::Base validates_presence_of :user_name validates_inclusion_of :status, :in => %w{ pending processing completed failed } - has_many :compliance_check_results, :order => :status + has_many :compliance_check_results, :order => [ :severity , :status ] serialize :parameter_set, JSON @@ -104,23 +101,4 @@ class ComplianceCheckTask < ActiveRecord::Base update_attribute :status, "failed" end end - - after_destroy :destroy_file - def destroy_file - file.close if File.exists? file - file.unlink if File.exists? file - FileUtils.rm file if File.exists? file - end - - def file - @template = File.open('app/views/compliance_check_results/index.csv.erb' ){ |f| f.read } - file = Tempfile.new('compliance_check_results.csv') - file.write(render) - file.flush - return file - end - - def render() - ERB.new(@template).result(binding) - end end diff --git a/app/models/compliance_check_task_export.rb b/app/models/compliance_check_task_export.rb new file mode 100644 index 000000000..cce26c5e8 --- /dev/null +++ b/app/models/compliance_check_task_export.rb @@ -0,0 +1,51 @@ +require 'tempfile' + +class ComplianceCheckTaskExport + include ERB::Util + + require 'zip' + + attr_accessor :template, :detailed_errors_template + attr_reader :compliance_check_task + + def initialize(compliance_check_task) + @compliance_check_task = compliance_check_task + @template = File.open('app/views/compliance_check_tasks/summary_errors_index.csv.erb' ) { |f| f.read } + @detailed_errors_template = File.open('app/views/compliance_check_tasks/detailed_errors_index.csv.erb' ) { |f| f.read } + end + + def export + begin + Dir.mktmpdir("ComplianceCheckResults_#{@compliance_check_task.referential_id}_#{@compliance_check_task.id}_", Dir.tmpdir) { |temp_dir| + + File.open(temp_dir + "/summary_errors_index.csv" , "a") do |f| + f.write(render) + f.flush + end + + File.open(temp_dir + "/detailed_errors_report.csv" , "a") do |f| + f.write(detailed_errors_render) + f.flush + end + + zip_file = Tempfile.new(["ComplianceCheckResults_#{@compliance_check_task.referential_id}_#{@compliance_check_task.id}_", ".zip"]) + + ::Zip::File.open(zip_file.path, ::Zip::File::CREATE) do |zipfile| + Dir[File.join(temp_dir, '*.csv')].each do |f| + zipfile.add(File.basename(f), f) + end + end + return zip_file + } + end + end + + def render() + ERB.new(@template).result(binding) + end + + def detailed_errors_render() + ERB.new(@detailed_errors_template).result(binding) + end + +end diff --git a/app/views/compliance_check_results/index.csv.erb b/app/views/compliance_check_results/index.csv.erb deleted file mode 100644 index e042d8007..000000000 --- a/app/views/compliance_check_results/index.csv.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= ComplianceCheckResult.human_attribute_name(:severity) %>;<%= ComplianceCheckResult.human_attribute_name(:status) %>;<%= ComplianceCheckResult.human_attribute_name(:rule_code) %>;<%= ComplianceCheckResult.human_attribute_name(:detail) %>;<%= ComplianceCheckResult.human_attribute_name(:url) %>;<%= ComplianceCheckResult.human_attribute_name(:violation_count) %>;<%= ComplianceCheckResult.human_attribute_name(:one) %>;<%= ComplianceCheckResult.human_attribute_name(:two) %>;<%= ComplianceCheckResult.human_attribute_name(:three) %>;<%= ComplianceCheckResult.human_attribute_name(:four) %>;<%= ComplianceCheckResult.human_attribute_name(:five) %>;<%= ComplianceCheckResult.human_attribute_name(:six) %>;<%= ComplianceCheckResult.human_attribute_name(:seven) %>;<%= ComplianceCheckResult.human_attribute_name(:eight) %>;<%= ComplianceCheckResult.human_attribute_name(:nine) %>;<%= ComplianceCheckResult.human_attribute_name(:ten) %> -<% compliance_check_results.each do |r| %><%= r.severity %>;<%= r.status %>;<%= r.rule_code %>;<%= ComplianceCheckResult.human_attribute_name(r.rule_code) %>;<%= Rails.application.config.validation_spec + I18n.locale.to_s + "/" + r.rule_code + ".html" %>;<%= r.violation_count %><% if r.violation_count > 0 %><% for i in 0..10 %><% if (i < r.violation_count) %>;<%= r.detail["detail"][i]["location"]["url"] %><% else %><% break %><% end %><% end %><% end %> -<% end %>
\ No newline at end of file diff --git a/app/views/compliance_check_tasks/detailed_errors_index.csv.erb b/app/views/compliance_check_tasks/detailed_errors_index.csv.erb new file mode 100644 index 000000000..527e35532 --- /dev/null +++ b/app/views/compliance_check_tasks/detailed_errors_index.csv.erb @@ -0,0 +1,2 @@ +<%= ComplianceCheckResult.human_attribute_name(:severity) %>;<%= ComplianceCheckResult.human_attribute_name(:status) %>;<%= ComplianceCheckResult.human_attribute_name(:rule_code) %>;<%= ComplianceCheckResult.human_attribute_name(:detail) %>;<%= ComplianceCheckResult.human_attribute_name(:url) %>;<%= ComplianceCheckResult.human_attribute_name(:violation_count) %>;<%= ComplianceCheckResult.human_attribute_name(:first_violations) %> +<% @compliance_check_task.compliance_check_results.each do |r| %><% if r.detail.present? %><% r.detail["detail"].first(10).each do |error| %><% case r.severity %><% when "warning" %><%= I18n.t "compliance_check_result.severities.warning" %><% when "error" %><%= I18n.t "compliance_check_result.severities.error" %><% end %>;<%= r.status %>;<%= r.rule_code %>;<%= ComplianceCheckResult.human_attribute_name(r.rule_code) %>;<%= Rails.application.config.validation_spec + I18n.locale.to_s + "/" + r.rule_code + ".html" %>;<%= r.violation_count %>;<% if error["messageArgs"] %><%= ComplianceCheckResult.human_attribute_name(r.rule_code) + I18n.t("compliance_check_result.details." + error["messageKey"], error["messageArgs"].symbolize_keys ) %><% else %><%= ComplianceCheckResult.human_attribute_name(r.rule_code) + I18n.t("compliance_check_result.details." + error["messageKey"] ) %><% end %><%= "\n" %><% end %><% end %><% end %>
\ No newline at end of file diff --git a/app/views/compliance_check_tasks/show.html.erb b/app/views/compliance_check_tasks/show.html.erb index 5bb270ffb..2fa39ff6f 100644 --- a/app/views/compliance_check_tasks/show.html.erb +++ b/app/views/compliance_check_tasks/show.html.erb @@ -31,7 +31,7 @@ </div> <% content_for :sidebar do %> <ul class="actions"> - <li><%= link_to t('compliance_check_tasks.actions.download'), referential_compliance_check_task_compliance_check_results_path(@referential, @compliance_check_task)+".csv" %></il> + <li><%= link_to t('compliance_check_tasks.actions.download'), export_referential_compliance_check_task_path(@referential, @compliance_check_task) %></il> <li><%= link_to t('compliance_check_tasks.actions.destroy'), referential_compliance_check_task_path(@referential, @compliance_check_task), :method => :delete, :data => {:confirm => t('compliance_check_tasks.actions.destroy_confirm')}, :class => "remove" %></li> </ul> diff --git a/app/views/compliance_check_tasks/summary_errors_index.csv.erb b/app/views/compliance_check_tasks/summary_errors_index.csv.erb new file mode 100644 index 000000000..516cb1b93 --- /dev/null +++ b/app/views/compliance_check_tasks/summary_errors_index.csv.erb @@ -0,0 +1,3 @@ +<%= ComplianceCheckResult.human_attribute_name(:severity) %>;<%= ComplianceCheckResult.human_attribute_name(:status) %>;<%= ComplianceCheckResult.human_attribute_name(:rule_code) %>;<%= ComplianceCheckResult.human_attribute_name(:detail) %>;<%= ComplianceCheckResult.human_attribute_name(:url) %>;<%= ComplianceCheckResult.human_attribute_name(:violation_count) %> +<% @compliance_check_task.compliance_check_results.each do |r| %><% case r.severity %><% when "warning" %><%= I18n.t "compliance_check_result.severities.warning" %><% when "error" %><%= I18n.t "compliance_check_result.severities.error" %><% end %>;<%= r.status %>;<%= r.rule_code %>;<%= ComplianceCheckResult.human_attribute_name(r.rule_code) %>;<%= Rails.application.config.validation_spec + I18n.locale.to_s + "/" + r.rule_code + ".html" %>;<%= r.violation_count %> +<% end %>
\ No newline at end of file |
