scinote-web/app/controllers/reports_controller.rb

424 lines
12 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class ReportsController < ApplicationController
include TeamsHelper
2017-03-08 21:14:03 +08:00
include ReportActions
include ReportsHelper
2021-06-10 16:59:34 +08:00
include StringUtility
2016-02-12 23:52:43 +08:00
BEFORE_ACTION_METHODS = %i(
create
edit
update
generate_pdf
generate_docx
new_template_values
2021-04-06 21:27:12 +08:00
project_contents
).freeze
before_action :load_vars, only: %i(edit update document_preview generate_pdf generate_docx status
save_pdf_to_inventory_modal save_pdf_to_inventory_item)
before_action :load_vars_nested, only: BEFORE_ACTION_METHODS
before_action :load_wizard_vars, only: %i(new edit)
before_action :load_available_repositories, only: %i(index save_pdf_to_inventory_modal available_repositories)
before_action :check_manage_permissions, only: BEFORE_ACTION_METHODS
2020-04-23 22:02:36 +08:00
before_action :switch_team_with_param, only: :index
2016-02-12 23:52:43 +08:00
after_action :generate_pdf_report, only: %i(create update generate_pdf)
2016-07-21 19:11:15 +08:00
# Index showing all reports of a single project
def index; end
def datatable
respond_to do |format|
format.json do
render json: ::ReportDatatable.new(
view_context,
current_user,
Report.viewable_by_user(current_user, current_team)
)
end
end
2016-02-12 23:52:43 +08:00
end
# Report grouped by modules
2016-07-21 19:11:15 +08:00
def new
@templates = Extends::REPORT_TEMPLATES
2021-04-08 23:40:16 +08:00
@report = current_team.reports.new
end
def new_template_values
if Extends::REPORT_TEMPLATES.key?(params[:template]&.to_sym)
template = params[:template]
else
return render_404
end
report = current_team.reports.where(project: @project).find_by(id: params[:report_id])
report ||= current_team.reports.new(project: @project)
respond_to do |format|
format.json do
if lookup_context.template_exists?("reports/templates/#{template}/edit.html.erb")
render json: {
html: render_to_string(
template: "reports/templates/#{template}/edit.html.erb",
layout: 'reports/template_values_editor',
locals: { report: report }
)
}
else
render json: {
html: render_to_string(partial: 'reports/wizard/no_template_values.html.erb')
}
end
end
end
2016-02-12 23:52:43 +08:00
end
# Creating new report from the _save modal of the new page
def create
@report = Report.new(report_params)
@report.project = @project
@report.user = current_user
@report.team = current_team
@report.settings = report_params[:settings]
2016-02-12 23:52:43 +08:00
@report.last_modified_by = current_user
ReportActions::ReportContent.new(
@report,
params[:project_content],
params[:template_values],
current_user
).save_with_content
if @report.errors.blank?
2019-03-14 02:05:29 +08:00
log_activity(:create_report)
flash[:success] = t('projects.reports.index.generation.accepted_message')
2019-03-14 02:05:29 +08:00
redirect_to reports_path
2016-02-12 23:52:43 +08:00
else
render json: @report.errors.full_messages, status: :unprocessable_entity
2016-02-12 23:52:43 +08:00
end
end
def edit
2021-04-13 20:10:52 +08:00
@edit = true
@active_template = @report.settings[:template]
@report.settings = Report::DEFAULT_SETTINGS if @report.settings.blank?
2021-04-13 20:10:52 +08:00
@project_contents = {
experiments: @report.report_elements.order(:position).experiment.pluck(:experiment_id),
my_modules: @report.report_elements.order(:position).my_module.pluck(:my_module_id),
repositories: @report.report_elements.my_module_repository.distinct(:repository_id).pluck(:repository_id)
2021-04-13 20:10:52 +08:00
}
render :new
2016-02-12 23:52:43 +08:00
end
# Updating existing report from the _save modal of the new page
def update
@report.last_modified_by = current_user
@report.assign_attributes(
report_params.merge(project_id: @project.id)
)
2016-02-12 23:52:43 +08:00
ReportActions::ReportContent.new(
@report,
params[:project_content],
params[:template_values],
current_user
).save_with_content
if @report.errors.blank?
2019-03-14 02:05:29 +08:00
log_activity(:edit_report)
flash[:success] = t('projects.reports.index.generation.accepted_message')
2019-03-14 02:05:29 +08:00
redirect_to reports_path
2016-02-12 23:52:43 +08:00
else
render json: @report.errors.full_messages, status: :unprocessable_entity
2016-02-12 23:52:43 +08:00
end
end
# Destroy multiple entries action
def destroy
begin
report_ids = JSON.parse(params[:report_ids])
rescue
render_404
end
report_ids.each do |report_id|
report = Report.find_by_id(report_id)
next unless report.present? && can_manage_reports?(report.project.team)
2017-03-08 21:14:03 +08:00
# record an activity
2019-03-21 04:34:47 +08:00
log_activity(:delete_report, report)
2017-03-08 21:14:03 +08:00
report.destroy
2016-02-12 23:52:43 +08:00
end
redirect_to reports_path
2016-02-12 23:52:43 +08:00
end
def status
docx = @report.docx_file.attached? ? document_preview_report_path(@report, report_type: :docx) : nil
pdf = @report.pdf_file.attached? ? document_preview_report_path(@report, report_type: :pdf) : nil
respond_to do |format|
format.json do
render json: {
docx: {
processing: @report.docx_processing?,
preview_url: docx,
error: @report.docx_error?
},
pdf: {
processing: @report.pdf_processing?,
preview_url: pdf,
error: @report.pdf_error?
}
}
end
end
end
# Generation actions
def generate_pdf
2016-02-12 23:52:43 +08:00
respond_to do |format|
format.json do
render json: {
message: I18n.t('projects.reports.index.generation.accepted_message')
}
end
end
end
def generate_docx
respond_to do |format|
format.json do
@report.docx_processing!
log_activity(:generate_docx_report)
ensure_report_template!
Reports::DocxJob.perform_later(@report.id, current_user, root_url)
render json: {
message: I18n.t('projects.reports.index.generation.accepted_message')
}
end
2016-02-12 23:52:43 +08:00
end
end
def save_pdf_to_inventory_modal
respond_to do |format|
format.json do
render json: {
html: render_to_string(partial: 'reports/save_PDF_to_inventory_modal.html.erb')
}
end
end
end
def save_pdf_to_inventory_item
return render_404 unless @report.pdf_file.attached?
save_pdf_to_inventory_item = ReportActions::SavePdfToInventoryItem.new(
@report, current_user, current_team, save_pdf_params
)
if save_pdf_to_inventory_item.save
render json: {
message: I18n.t(
'projects.reports.new.save_PDF_to_inventory_modal.success_flash'
)
}, status: :ok
else
render json: { message: save_pdf_to_inventory_item.error_messages },
status: :unprocessable_entity
end
rescue ReportActions::RepositoryPermissionError => e
render json: { message: e.message }, status: :forbidden
rescue StandardError => e
render json: { message: e.message }, status: :internal_server_error
end
2016-02-12 23:52:43 +08:00
# Modal for adding contents into module element
def module_contents_modal
my_module = MyModule.find_by_id(params[:my_module_id])
return render_403 unless my_module.experiment.project == @project
2016-02-12 23:52:43 +08:00
respond_to do |format|
if my_module.blank?
format.json do
2016-02-12 23:52:43 +08:00
render json: {}, status: :not_found
end
2016-02-12 23:52:43 +08:00
else
format.json do
2016-02-12 23:52:43 +08:00
render json: {
html: render_to_string(
2017-03-08 21:14:03 +08:00
partial: 'reports/new/modal/module_contents.html.erb',
2016-02-12 23:52:43 +08:00
locals: { project: @project, my_module: my_module }
)
2016-02-12 23:52:43 +08:00
}
end
2016-02-12 23:52:43 +08:00
end
end
end
# Modal for adding contents into step element
def step_contents_modal
step = Step.find_by_id(params[:step_id])
return render_403 unless step.my_module.experiment.project == @project
respond_to do |format|
if step.blank?
format.json do
render json: {}, status: :not_found
end
else
format.json do
render json: {
html: render_to_string(
partial: 'reports/new/modal/step_contents.html.erb',
locals: { project: @project, step: step }
)
}
end
end
end
end
# Modal for adding contents into result element
def result_contents_modal
result = Result.find_by_id(params[:result_id])
return render_403 unless result.experiment.project == @project
respond_to do |format|
if result.blank?
format.json do
render json: {}, status: :not_found
end
else
format.json do
render json: {
html: render_to_string(
partial: 'reports/new/modal/result_contents.html.erb',
locals: { project: @project, result: result }
)
}
end
end
end
end
2016-02-12 23:52:43 +08:00
def project_contents
2021-04-06 21:27:12 +08:00
render json: {
2021-04-08 23:40:16 +08:00
html: render_to_string(
partial: 'reports/wizard/project_contents.html.erb',
2021-04-13 20:10:52 +08:00
locals: { project: @project, report: nil}
2021-04-08 23:40:16 +08:00
)
}
2016-02-12 23:52:43 +08:00
end
def available_repositories
render json: { results: @available_repositories }, status: :ok
end
def document_preview
render json: { html: render_to_string(
partial: 'reports/content_document_preview.html.erb',
locals: {
report: @report,
report_type: params[:report_type]
}
) }
end
2016-02-12 23:52:43 +08:00
private
AvailableRepository = Struct.new(:id, :name)
2016-02-12 23:52:43 +08:00
def load_vars
@report = current_team.reports.find_by(id: params[:id])
2017-03-08 21:14:03 +08:00
render_404 unless @report
render_403 unless can_read_project?(@report.project)
2016-02-12 23:52:43 +08:00
end
def load_vars_nested
@project = current_team.projects.find_by(id: params[:project_id])
2017-03-08 21:14:03 +08:00
render_404 unless @project
render_403 unless can_read_project?(@project)
2016-02-12 23:52:43 +08:00
end
def load_wizard_vars
@templates = Extends::REPORT_TEMPLATES
live_repositories = Repository.accessible_by_teams(current_team)
snapshots_of_deleted = RepositorySnapshot.left_outer_joins(:original_repository)
.where(team: current_team)
.where.not(original_repository: live_repositories)
.select('DISTINCT ON ("repositories"."parent_id") "repositories".*')
@repositories = (live_repositories + snapshots_of_deleted).sort_by { |r| r.name.downcase }
@visible_projects = Project.active
.viewable_by_user(current_user, current_team)
.joins(experiments: :my_modules)
.merge(Experiment.active)
.merge(MyModule.active)
.select(:id, :name)
2016-02-12 23:52:43 +08:00
end
def check_manage_permissions
render_403 unless can_manage_reports?(@project.team)
end
def load_available_repositories
@available_repositories = []
repositories = Repository.active
.accessible_by_teams(current_team)
.name_like(search_params[:q])
.limit(Constants::SEARCH_LIMIT)
.select(:id, :name, :team_id, :permission_level)
repositories.each do |repository|
next unless can_manage_repository_rows?(current_user, repository)
@available_repositories.push(AvailableRepository.new(repository.id,
ellipsize(repository.name, 75, 50)))
end
end
2016-02-12 23:52:43 +08:00
def report_params
2017-03-08 21:14:03 +08:00
params.require(:report)
.permit(:name, :description, :grouped_by, :report_contents, settings: {})
2016-02-12 23:52:43 +08:00
end
def search_params
params.permit(:q)
end
def save_pdf_params
params.permit(:repository_id, :respository_column_id, :repository_item_id)
end
2019-03-14 02:05:29 +08:00
2019-03-21 04:34:47 +08:00
def log_activity(type_of, report = @report)
2019-03-14 02:05:29 +08:00
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
2019-03-21 04:34:47 +08:00
subject: report,
team: report.team,
project: report.project,
message_items: { report: report.id })
2019-03-14 02:05:29 +08:00
end
def generate_pdf_report
return unless @report.persisted?
@report.pdf_processing!
log_activity(:generate_pdf_report)
ensure_report_template!
Reports::PdfJob.perform_later(@report.id, current_user)
end
def ensure_report_template!
return if @report.settings['template'].present?
@report.settings['template'] = 'scinote_template'
@report.save
end
2016-07-21 19:11:15 +08:00
end