diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-08-21 10:48:15 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-09-16 12:41:45 +0100 |
commit | 8eab413902f58ca3b812f8ecfb73de731b7f244b (patch) | |
tree | f51a4d3a7ad556124454c965dfaa006eee5cdd76 /app/controllers/request_controller.rb | |
parent | 5f964420b0b66cc2acd93a866e3671c32cc42d91 (diff) |
Extract out code for making a request summary file
Render the show template within the current thread rather than making
another request - we're going to need to use the current session in
order to know what do include in the zip file, now that we have more
fine-grained visibility of messages. Also, this will mean we can use
this functionality in single threaded contexts, and test it more easily.
Don't display profile photos as this would require another process, and
hide other icons so we don't need to include them. Use render_to_string
as a more standard way of rendering templates to a string for further
manipulation.
Diffstat (limited to 'app/controllers/request_controller.rb')
-rw-r--r-- | app/controllers/request_controller.rb | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index 6f1a549c5..9dff180d6 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -888,32 +888,10 @@ class RequestController < ApplicationController if !File.exists?(file_path) FileUtils.mkdir_p(File.dirname(file_path)) Zip::ZipFile.open(file_path, Zip::ZipFile::CREATE) { |zipfile| - convert_command = AlaveteliConfiguration::html_to_pdf_command - done = false - if !convert_command.blank? && File.exists?(convert_command) - url = "http://#{AlaveteliConfiguration::domain}#{request_path(@info_request)}?print_stylesheet=1" - tempfile = Tempfile.new('foihtml2pdf') - output = AlaveteliExternalCommand.run(convert_command, url, tempfile.path) - if !output.nil? - zipfile.get_output_stream("correspondence.pdf") { |f| - f.puts(File.open(tempfile.path).read) - } - done = true - else - logger.error("Could not convert info request #{@info_request.id} to PDF with command '#{convert_command} #{url} #{tempfile.path}'") - end - tempfile.close - else - logger.warn("No HTML -> PDF converter found at #{convert_command}") - end - if !done - @info_request_events = @info_request.info_request_events - template = File.read(File.join(File.dirname(__FILE__), "..", "views", "request", "simple_correspondence.html.erb")) - output = ERB.new(template).result(binding) - zipfile.get_output_stream("correspondence.txt") { |f| - f.puts(output) - } - end + + file_info = make_request_summary_file(@info_request) + zipfile.get_output_stream(file_info[:filename]) { |f| f.puts(file_info[:data]) } + for message in @info_request.incoming_messages attachments = message.get_attachments_for_display for attachment in attachments @@ -963,6 +941,38 @@ class RequestController < ApplicationController @last_response = info_request.get_last_response end + def make_request_summary_file(info_request) + done = false + convert_command = AlaveteliConfiguration::html_to_pdf_command + assign_variables_for_show_template(info_request) + if !convert_command.blank? && File.exists?(convert_command) + @render_to_file = true + html_output = render_to_string(:template => 'request/show') + tmp_input = Tempfile.new(['foihtml2pdf-input', '.html']) + tmp_input.write(html_output) + tmp_input.close + tmp_output = Tempfile.new('foihtml2pdf-output') + output = AlaveteliExternalCommand.run(convert_command, tmp_input.path, tmp_output.path) + if !output.nil? + file_info = { :filename => 'correspondence.pdf', + :data => File.open(tmp_output.path).read } + done = true + else + logger.error("Could not convert info request #{@info_request.id} to PDF with command '#{convert_command} #{tmp_input.path} #{tmp_output.path}'") + end + tmp_output.close + tmp_input.delete + tmp_output.delete + else + logger.warn("No HTML -> PDF converter found at #{convert_command}") + end + if !done + file_info = { :filename => 'correspondence.txt', + :data => render_to_string(:template => 'request/simple_correspondence.html.erb', + :layout => false) } + end + file_info + end end |