mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-11 01:14:30 +08:00
Move export all report business logic to model [SCI-2641]
This commit is contained in:
parent
5ce94b5ecd
commit
a16d16bc5b
2 changed files with 206 additions and 203 deletions
|
@ -180,9 +180,11 @@ class ReportsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def export_project_pdf(project)
|
def export_whole_project_pdf(project)
|
||||||
@project = project
|
@project = project
|
||||||
@report = generate_export_all_report
|
@report = Report.generate_whole_project_report(
|
||||||
|
@project, current_user, current_team
|
||||||
|
)
|
||||||
|
|
||||||
page_html_string = render_to_string 'reports/new.html.erb', export_all: true
|
page_html_string = render_to_string 'reports/new.html.erb', export_all: true
|
||||||
parsed_page_html = Nokogiri::HTML(page_html_string)
|
parsed_page_html = Nokogiri::HTML(page_html_string)
|
||||||
|
@ -214,207 +216,6 @@ class ReportsController < ApplicationController
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate_export_all_report
|
|
||||||
report_contents = []
|
|
||||||
report_contents << {
|
|
||||||
'type_of' => 'project_header',
|
|
||||||
'id' => {
|
|
||||||
'project_id' => @project.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
|
|
||||||
@project.experiments.each do |exp|
|
|
||||||
modules = []
|
|
||||||
|
|
||||||
exp.my_modules.each do |my_module|
|
|
||||||
module_children = []
|
|
||||||
|
|
||||||
my_module.protocol.steps.each do |step|
|
|
||||||
step_children = []
|
|
||||||
|
|
||||||
step.assets.each do |asset|
|
|
||||||
step_children << {
|
|
||||||
'type_of' => 'step_asset',
|
|
||||||
'id' => {
|
|
||||||
'asset_id' => asset.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
step.tables.each do |table|
|
|
||||||
step_children << {
|
|
||||||
'type_of' => 'step_table',
|
|
||||||
'id' => {
|
|
||||||
'table_id' => table.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
step.checklists.each do |step_checklist|
|
|
||||||
step_children << {
|
|
||||||
'type_of' => 'step_checklist',
|
|
||||||
'id' => {
|
|
||||||
'checklist_id' => step_checklist.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
if step.step_comments.any?
|
|
||||||
step_children << {
|
|
||||||
'type_of' => 'step_comments',
|
|
||||||
'id' => {
|
|
||||||
'step_id' => step.id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'step',
|
|
||||||
'id' => {
|
|
||||||
'step_id' => step.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => step_children
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
my_module.results.each do |result|
|
|
||||||
if result.asset
|
|
||||||
result_comments = []
|
|
||||||
|
|
||||||
if result.result_comments.any?
|
|
||||||
result_comments << {
|
|
||||||
'type_of' => 'result_comments',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'result_asset',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
elsif result.table
|
|
||||||
result_comments = []
|
|
||||||
|
|
||||||
if result.result_comments.any?
|
|
||||||
result_comments << {
|
|
||||||
'type_of' => 'result_comments',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'result_table',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => result_comments
|
|
||||||
}
|
|
||||||
elsif result.result_text
|
|
||||||
result_comments = []
|
|
||||||
|
|
||||||
if result.result_comments.any?
|
|
||||||
result_comments << {
|
|
||||||
'type_of' => 'result_comments',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'result_text',
|
|
||||||
'id' => {
|
|
||||||
'result_id' => result.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => result_comments
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if my_module.activities.any?
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'my_module_activity',
|
|
||||||
'id' => {
|
|
||||||
'my_module_id' => my_module.id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
module_repositories_id =
|
|
||||||
my_module.repository_rows.map(&:repository_id).uniq
|
|
||||||
module_repositories_id.each do |repo_id|
|
|
||||||
module_children << {
|
|
||||||
'type_of' => 'my_module_repository',
|
|
||||||
'id' => {
|
|
||||||
'my_module_id' => my_module.id,
|
|
||||||
'repository_id' => repo_id
|
|
||||||
},
|
|
||||||
'sort_order' => 'asc',
|
|
||||||
'children' => []
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
modules << {
|
|
||||||
'type_of' => 'my_module',
|
|
||||||
'id' => {
|
|
||||||
'my_module_id' => my_module.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => module_children
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
report_contents << {
|
|
||||||
'type_of' => 'experiment',
|
|
||||||
'id' => {
|
|
||||||
'experiment_id' => exp.id
|
|
||||||
},
|
|
||||||
'sort_order' => nil,
|
|
||||||
'children' => modules
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
report = Report.new
|
|
||||||
token = loop do
|
|
||||||
token = SecureRandom.hex(10)
|
|
||||||
break token unless Report.where(name: token).exists?
|
|
||||||
end
|
|
||||||
report.name = token
|
|
||||||
report.project = @project
|
|
||||||
report.user = current_user
|
|
||||||
report.team = current_team
|
|
||||||
report.last_modified_by = current_user
|
|
||||||
|
|
||||||
report.save_with_contents(report_contents)
|
|
||||||
report
|
|
||||||
end
|
|
||||||
|
|
||||||
def save_pdf_to_inventory_item
|
def save_pdf_to_inventory_item
|
||||||
save_pdf_to_inventory_item = ReportActions::SavePdfToInventoryItem.new(
|
save_pdf_to_inventory_item = ReportActions::SavePdfToInventoryItem.new(
|
||||||
current_user, current_team, save_PDF_params
|
current_user, current_team, save_PDF_params
|
||||||
|
|
|
@ -93,6 +93,208 @@ class Report < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.generate_whole_project_report(project, current_user, current_team)
|
||||||
|
report_contents = []
|
||||||
|
report_contents << {
|
||||||
|
'type_of' => 'project_header',
|
||||||
|
'id' => {
|
||||||
|
'project_id' => project.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
|
||||||
|
project.experiments.each do |exp|
|
||||||
|
modules = []
|
||||||
|
|
||||||
|
exp.my_modules.each do |my_module|
|
||||||
|
module_children = []
|
||||||
|
|
||||||
|
my_module.protocol.steps.each do |step|
|
||||||
|
step_children = []
|
||||||
|
|
||||||
|
step.assets.each do |asset|
|
||||||
|
step_children << {
|
||||||
|
'type_of' => 'step_asset',
|
||||||
|
'id' => {
|
||||||
|
'asset_id' => asset.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
step.tables.each do |table|
|
||||||
|
step_children << {
|
||||||
|
'type_of' => 'step_table',
|
||||||
|
'id' => {
|
||||||
|
'table_id' => table.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
step.checklists.each do |step_checklist|
|
||||||
|
step_children << {
|
||||||
|
'type_of' => 'step_checklist',
|
||||||
|
'id' => {
|
||||||
|
'checklist_id' => step_checklist.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
if step.step_comments.any?
|
||||||
|
step_children << {
|
||||||
|
'type_of' => 'step_comments',
|
||||||
|
'id' => {
|
||||||
|
'step_id' => step.id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'step',
|
||||||
|
'id' => {
|
||||||
|
'step_id' => step.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => step_children
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
my_module.results.each do |result|
|
||||||
|
if result.asset
|
||||||
|
result_comments = []
|
||||||
|
|
||||||
|
if result.result_comments.any?
|
||||||
|
result_comments << {
|
||||||
|
'type_of' => 'result_comments',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'result_asset',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
elsif result.table
|
||||||
|
result_comments = []
|
||||||
|
|
||||||
|
if result.result_comments.any?
|
||||||
|
result_comments << {
|
||||||
|
'type_of' => 'result_comments',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'result_table',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => result_comments
|
||||||
|
}
|
||||||
|
elsif result.result_text
|
||||||
|
result_comments = []
|
||||||
|
|
||||||
|
if result.result_comments.any?
|
||||||
|
result_comments << {
|
||||||
|
'type_of' => 'result_comments',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'result_text',
|
||||||
|
'id' => {
|
||||||
|
'result_id' => result.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => result_comments
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if my_module.activities.any?
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'my_module_activity',
|
||||||
|
'id' => {
|
||||||
|
'my_module_id' => my_module.id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
module_repositories_id =
|
||||||
|
my_module.repository_rows.map(&:repository_id).uniq
|
||||||
|
module_repositories_id.each do |repo_id|
|
||||||
|
module_children << {
|
||||||
|
'type_of' => 'my_module_repository',
|
||||||
|
'id' => {
|
||||||
|
'my_module_id' => my_module.id,
|
||||||
|
'repository_id' => repo_id
|
||||||
|
},
|
||||||
|
'sort_order' => 'asc',
|
||||||
|
'children' => []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
modules << {
|
||||||
|
'type_of' => 'my_module',
|
||||||
|
'id' => {
|
||||||
|
'my_module_id' => my_module.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => module_children
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
report_contents << {
|
||||||
|
'type_of' => 'experiment',
|
||||||
|
'id' => {
|
||||||
|
'experiment_id' => exp.id
|
||||||
|
},
|
||||||
|
'sort_order' => nil,
|
||||||
|
'children' => modules
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
dummy_name = loop do
|
||||||
|
dummy_name = SecureRandom.hex(10)
|
||||||
|
break dummy_name unless Report.where(name: dummy_name).exists?
|
||||||
|
end
|
||||||
|
|
||||||
|
report = Report.new
|
||||||
|
report.name = dummy_name
|
||||||
|
report.project = project
|
||||||
|
report.user = current_user
|
||||||
|
report.team = current_team
|
||||||
|
report.last_modified_by = current_user
|
||||||
|
|
||||||
|
report.save_with_contents(report_contents)
|
||||||
|
report
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Recursively save a single JSON element
|
# Recursively save a single JSON element
|
||||||
|
|
Loading…
Reference in a new issue