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 | |
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.
-rw-r--r-- | app/controllers/request_controller.rb | 62 | ||||
-rw-r--r-- | app/views/comment/_single_comment.html.erb | 2 | ||||
-rw-r--r-- | app/views/general/_stylesheet_includes.html.erb | 39 | ||||
-rw-r--r-- | app/views/request/show.html.erb | 4 | ||||
-rw-r--r-- | config/general.yml-example | 6 | ||||
-rw-r--r-- | config/test.yml | 2 | ||||
-rw-r--r-- | public/stylesheets/print.css | 4 |
7 files changed, 69 insertions, 50 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 diff --git a/app/views/comment/_single_comment.html.erb b/app/views/comment/_single_comment.html.erb index 421a9d4ba..af1de0649 100644 --- a/app/views/comment/_single_comment.html.erb +++ b/app/views/comment/_single_comment.html.erb @@ -1,5 +1,5 @@ <div class="comment_in_request" id="comment-<%=comment.id.to_s%>"> - <% if comment.user && comment.user.profile_photo %> + <% if comment.user && comment.user.profile_photo && !@render_to_file %> <div class="user_photo_on_comment"> <img src="<%= get_profile_photo_url(:url_name => comment.user.url_name) %>" alt=""> </div> diff --git a/app/views/general/_stylesheet_includes.html.erb b/app/views/general/_stylesheet_includes.html.erb index 9dd1f357d..416ddb825 100644 --- a/app/views/general/_stylesheet_includes.html.erb +++ b/app/views/general/_stylesheet_includes.html.erb @@ -1,16 +1,23 @@ - <%= stylesheet_link_tag 'main', :title => "Main", :rel => "stylesheet", :media => "all" %> - <%= stylesheet_link_tag 'fonts', :rel => "stylesheet", :media => "all" %> - <%= stylesheet_link_tag 'print', :rel => "stylesheet", :media => "print" %> - <% if !params[:print_stylesheet].nil? %> - <%= stylesheet_link_tag 'print', :rel => "stylesheet", :media => "all" %> - <% end %> - <%= stylesheet_link_tag 'admin-theme/jquery-ui-1.8.15.custom.css', :rel => 'stylesheet'%> - <!--[if LT IE 7]> - <style type="text/css">@import url("/stylesheets/ie6.css");</style> - <![endif]--> - <!--[if LT IE 8]> - <style type="text/css">@import url("/stylesheets/ie7.css");</style> - <![endif]--> - <% if AlaveteliConfiguration::force_registration_on_new_request %> - <%= stylesheet_link_tag 'jquery.fancybox-1.3.4', :rel => "stylesheet" %> - <% end %> +<%- if @render_to_file %> + <style> + <%= raw File.read(Rails.root.join('public', 'stylesheets', 'main.css')) %> + <%= raw File.read(Rails.root.join('public', 'stylesheets', 'print.css')) %> + </style> +<%- else %> + <%= stylesheet_link_tag 'main', :title => "Main", :rel => "stylesheet", :media => "all" %> + <%= stylesheet_link_tag 'fonts', :rel => "stylesheet", :media => "all" %> + <%= stylesheet_link_tag 'print', :rel => "stylesheet", :media => "print" %> + <% if !params[:print_stylesheet].nil? %> + <%= stylesheet_link_tag 'print', :rel => "stylesheet", :media => "all" %> + <% end %> + <%= stylesheet_link_tag 'admin-theme/jquery-ui-1.8.15.custom.css', :rel => 'stylesheet'%> + <!--[if LT IE 7]> + <style type="text/css">@import url("/stylesheets/ie6.css");</style> + <![endif]--> + <!--[if LT IE 8]> + <style type="text/css">@import url("/stylesheets/ie7.css");</style> + <![endif]--> + <% if AlaveteliConfiguration::force_registration_on_new_request %> + <%= stylesheet_link_tag 'jquery.fancybox-1.3.4', :rel => "stylesheet" %> + <% end %> +<% end %> diff --git a/app/views/request/show.html.erb b/app/views/request/show.html.erb index 455846aeb..c520ce40c 100644 --- a/app/views/request/show.html.erb +++ b/app/views/request/show.html.erb @@ -25,7 +25,7 @@ <div id="left_column"> <h1><%=h(@info_request.title)%></h1> - <% if !@info_request.is_external? && @info_request.user.profile_photo %> + <% if !@info_request.is_external? && @info_request.user.profile_photo && !@render_to_file %> <p class="user_photo_on_request"> <img src="<%= get_profile_photo_url(:url_name => @info_request.user.url_name) %>" alt=""> </p> @@ -48,7 +48,7 @@ <p id="request_status" class="request_icon_line icon_<%= @info_request.calculate_status %>"> <% if @info_request.awaiting_description %> - <% if @is_owning_user && !@info_request.is_external? %> + <% if @is_owning_user && !@info_request.is_external? && !@render_to_file %> <%= _('Please <strong>answer the question above</strong> so we know whether the ')%> <%= MySociety::Format.fancy_pluralize(@new_responses_count, 'recent response contains', 'recent responses contain') %> <%= _('useful information.') %> <% else %> diff --git a/config/general.yml-example b/config/general.yml-example index 0753af46b..0491ef64d 100644 --- a/config/general.yml-example +++ b/config/general.yml-example @@ -147,9 +147,9 @@ GAZE_URL: http://gaze.mysociety.org # The email address to which non-bounce responses should be forwarded FORWARD_NONBOUNCE_RESPONSES_TO: user-support@localhost -# Path to a program that converts a page at a URL to HTML. It should -# take two arguments: the URL, and a path to an output file. A static -# binary of wkhtmltopdf is recommended: +# Path to a program that converts an HTML page in a file to PDF. It +#should take two arguments: the URL, and a path to an output file. +# A static binary of wkhtmltopdf is recommended: # http://code.google.com/p/wkhtmltopdf/downloads/list # If the command is not present, a text-only version will be rendered # instead. diff --git a/config/test.yml b/config/test.yml index 5c08e928b..b26ca99d4 100644 --- a/config/test.yml +++ b/config/test.yml @@ -108,7 +108,7 @@ ACCELERATOR_PORT: '6081' # mySociety's gazeteer service. Shouldn't change. GAZE_URL: http://gaze.mysociety.org -# Path to a program that converts a page at a URL to HTML. It should +# Path to a program that converts a page in a file to PDF. It should # take two arguments: the URL, and a path to an output file. A static # binary of wkhtmltopdf is recommended: # http://code.google.com/p/wkhtmltopdf/downloads/list diff --git a/public/stylesheets/print.css b/public/stylesheets/print.css index 89be21019..6f29c18da 100644 --- a/public/stylesheets/print.css +++ b/public/stylesheets/print.css @@ -18,9 +18,11 @@ p.event_actions, div#after_actions, #right_column, #banner, +.admin .navbar, #header_right, #describe_state_form_1, -#describe_state_form_2 input[type=submit], +#describe_state_form_2, +.attachment_image, #footer { display: none; } |