Merge pull request #3270 from okriuchykhin/ok_SCI_5636

Add full-page display to PDF Report of all pages of the attached Word and PDF files in the Results [SCI-5636]
This commit is contained in:
Miha Mencin 2021-05-03 14:52:03 +02:00 committed by GitHub
commit ad678a7452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 12 deletions

View file

@ -25,7 +25,7 @@ class PdfPreviewJob < ApplicationJob
ActiveRecord::Base.transaction do
success = system(
"#{libreoffice_path} --headless --invisible --convert-to pdf --outdir #{work_dir} #{input.path}"
libreoffice_path, '--headless', '--invisible', '--convert-to', 'pdf', '--outdir', work_dir, input.path
)
unless success && File.file?(preview_file)
raise StandardError, "There was an error generating PDF preview, blob id: #{blob.id}"

View file

@ -6,6 +6,14 @@ module Reports
queue_as :reports
discard_on StandardError do |job, error|
report = Report.find_by(id: job.arguments.first)
ActiveRecord::Base.no_touching do
report&.update(docx_file_processing: false)
end
Rails.logger.error("Couldn't generate DOCX for Report with id: #{job.arguments.first}. Error:\n #{error}")
end
def perform(report, user, team, root_url)
file = Tempfile.new(['report', '.docx'])
begin

View file

@ -7,6 +7,16 @@ module Reports
queue_as :reports
discard_on StandardError do |job, error|
report = Report.find_by(id: job.arguments.first)
ActiveRecord::Base.no_touching do
report&.update(pdf_file_processing: false)
end
Rails.logger.error("Couldn't generate PDF for Report with id: #{job.arguments.first}. Error:\n #{error}")
end
PREVIEW_EXTENSIONS = %w(docx pdf).freeze
def perform(report, template, user)
file = Tempfile.new(['report', '.pdf'], binmode: true)
begin
@ -36,6 +46,7 @@ module Reports
)
file.rewind
file = append_result_asset_previews(report, file) if report.settings.dig(:task, :file_results_previews)
report.pdf_file.attach(io: file, filename: 'report.pdf')
report.update!(pdf_file_processing: false)
@ -53,5 +64,42 @@ module Reports
file.unlink
end
end
private
def append_result_asset_previews(report, report_file)
Dir.mktmpdir do |tmp_dir|
report.report_elements.my_module.each do |my_module_element|
my_module_element.my_module.results.each do |result|
next unless result.is_asset && PREVIEW_EXTENSIONS.include?(result.asset.file.blob.filename.extension)
asset = result.asset
unless asset.file_pdf_preview.attached?
PdfPreviewJob.perform_now(asset.id)
asset.reload
end
asset.file_pdf_preview.open(tmpdir: tmp_dir) do |file|
report_file = merge_pdf_files(file, report_file)
end
end
end
end
report_file
end
def merge_pdf_files(file, report_file)
merged_file = Tempfile.new(['report', '.pdf'], binmode: true)
success = system(
'pdfunite', report_file.path, file.path, merged_file.path
)
unless success && File.file?(merged_file)
raise StandardError, 'There was an error merging report and PDF file preview'
end
report_file.close
report_file.unlink
merged_file
end
end
end

View file

@ -29,9 +29,11 @@ module ReportActions
# Delete existing template values
@report.report_template_values.destroy_all
formatted_template_values = @template_values.as_json.map { |k, v| v['name'] = k; v }
# Save new template values
@report.report_template_values.create!(formatted_template_values)
if @template_values.present?
formatted_template_values = @template_values.as_json.map { |k, v| v['name'] = k; v }
# Save new template values
@report.report_template_values.create!(formatted_template_values)
end
end
@report

View file

@ -37,6 +37,9 @@
</div>
<div class="pull-left user-time">
<%= t("projects.reports.elements.result_asset.user_time", user: result.user.full_name, timestamp: l(timestamp, format: :full)) %>
<% if report.settings.dig(:task, :file_results_previews) %>
<%= t('projects.reports.elements.result_asset.full_preview_attached') %>
<% end %>
</div>
<div class="pull-right controls">
<%= render partial: "reports/elements/element_controls.html.erb" %>

View file

@ -728,6 +728,7 @@ en:
result_asset:
file_name: "[ %{file} ]"
user_time: "Uploaded by %{user} on %{timestamp}."
full_preview_attached: "[full document attached to report]"
result_table:
table_name: "[ %{name} ]"
user_time: "Created by %{user} on %{timestamp}."

View file

@ -9,9 +9,10 @@ describe ReportsController, type: :controller do
let!(:team) { create :team, created_by: user }
let!(:user_team) { create :user_team, team: team, user: user }
let(:user_project) { create :user_project, :owner, user: user }
let(:project) do
create :project, team: team, user_projects: [user_project]
end
let(:project) { create :project, team: team, user_projects: [user_project] }
let(:experiment) { create :experiment, project: project }
let(:my_module1) { create :my_module, experiment: experiment }
let(:my_module2) { create :my_module, experiment: experiment }
let(:report) do
create :report, user: user, project: project, team: team,
name: 'test repot A1', description: 'test description A1'
@ -23,9 +24,10 @@ describe ReportsController, type: :controller do
let(:params) do
{ project_id: project.id,
report: { name: 'test report created',
description: 'test description created' },
report_contents: '[{"type_of":"project_header","id":{"project_id":' +
project.id.to_s + '},"sort_order":null,"children":[]}]' }
description: 'test description created',
settings: Report::DEFAULT_SETTINGS },
project_content: { experiments: { experiment.id => [my_module1.id] } },
template_values: [] }
end
it 'calls create activity service' do
@ -49,8 +51,8 @@ describe ReportsController, type: :controller do
id: report.id,
report: { name: 'test report update',
description: 'test description update' },
report_contents: '[{"type_of":"project_header","id":{"project_id":' +
project.id.to_s + '},"sort_order":null,"children":[]}]' }
project_content: { experiments: { experiment.id => [my_module2.id] } },
template_values: [] }
end
it 'calls create activity service' do
expect(Activities::CreateActivityService).to receive(:call)