Move the docx creation to the Delayed job [SCI-5560]

This commit is contained in:
Oleksii Kriuchykhin 2021-03-16 13:47:23 +01:00
parent 2aae7046d0
commit df8354bde3
7 changed files with 49 additions and 17 deletions

View file

@ -62,7 +62,7 @@ gem 'ajax-datatables-rails', '~> 0.3.1'
gem 'aspector' # Aspect-oriented programming for Rails
gem 'auto_strip_attributes', '~> 2.1' # Removes unnecessary whitespaces AR
gem 'bcrypt', '~> 3.1.10'
gem 'caracal-rails' # Build docx report
gem 'caracal' # Build docx report
gem 'deface', '~> 1.0'
gem 'down', '~> 5.0'
gem 'faker' # Generate fake data

View file

@ -198,9 +198,6 @@ GEM
nokogiri (~> 1.6)
rubyzip (~> 1.1)
tilt (>= 1.4)
caracal-rails (1.0.2)
caracal (~> 1.0)
rails (>= 3.2)
case_transform (0.2)
activesupport
childprocess (3.0.0)
@ -647,7 +644,7 @@ DEPENDENCIES
canaid!
capybara
capybara-email
caracal-rails
caracal
cucumber-rails (~> 1.8)
database_cleaner
deface (~> 1.0)

View file

@ -22,7 +22,7 @@ class ReportsController < ApplicationController
result_contents
).freeze
before_action :load_vars, only: %i(edit update document_preview)
before_action :load_vars, only: %i(edit update document_preview generate)
before_action :load_vars_nested, only: BEFORE_ACTION_METHODS
before_action :load_visible_projects, only: %i(new edit)
before_action :load_available_repositories,
@ -167,15 +167,20 @@ class ReportsController < ApplicationController
end
# Generation action
# Currently, only .PDF is supported
def generate
respond_to do |format|
format.docx do
@user = current_user
@team = current_team
@scinote_url = root_url
@data = params[:data]
headers["Content-Disposition"] = 'attachment; filename="scinote_report.docx"'
format.pdf do
render pdf: 'report', header: { html: { template: 'reports/header.pdf.erb' }},
footer: { html: { template: 'reports/footer.pdf.erb',
locals: { current_time: I18n.l(Time.zone.now, format: :full) }}},
locals: { content: content },
template: 'reports/report.pdf.erb',
disable_javascript: true
end
format.json do
@report.update!(docx_file_processing: true)
Reports::DocxJob.perform_now(@report, params[:data], current_user, current_team, root_url)
render json: {}, status: :accepted
end
end
end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
module Reports
class DocxJob < ApplicationJob
queue_as :reports
def perform(report, data, user, team, root_url)
file = Tempfile.new(['report', '.docx'])
begin
docx = Caracal::Document.new(file.path)
Reports::Docx.new(data, docx, user: user, team: team, scinote_url: root_url).draw
docx.save
report.docx_file.attach(io: file, filename: 'report.docx')
report.update!(docx_file_processing: false)
ensure
file.close
file.unlink
end
end
end
end

View file

@ -1 +0,0 @@
Reports::Docx.new(@data,docx,user: @user, team: @team, scinote_url: @scinote_url).draw

View file

@ -26,8 +26,8 @@
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li><%= link_to t("projects.reports.new.nav_pdf"), generate_project_reports_path(@project, format: :pdf), id: "get-report-pdf" %></li>
<li><%= link_to t("projects.reports.new.nav_docx"), generate_project_reports_path(@project, format: :docx), id: "get-report-docx" %></li>
<li><%= link_to t("projects.reports.new.nav_pdf"), generate_project_report_path(@project, @report, format: :pdf), id: "get-report-pdf" %></li>
<li><%= link_to t("projects.reports.new.nav_docx"), generate_project_report_path(@project, @report, format: :json), id: "get-report-docx" %></li>
</ul>
</div>

View file

@ -249,10 +249,20 @@ Rails.application.routes.draw do
resources :reports,
path: '/reports',
only: %i(edit update create) do
member do
post 'generate', to: 'reports#generate', format: %w(pdf json)
end
collection do
# The posts following here should in theory be gets,
# but are posts because of parameters payload
post 'generate', to: 'reports#generate', format: %w(docx pdf)
get 'new/', to: 'reports#new'
get 'new/project_contents_modal',
to: 'reports#project_contents_modal',
as: :project_contents_modal
post 'new/project_contents',
to: 'reports#project_contents',
as: :project_contents
get 'new/experiment_contents_modal',
to: 'reports#experiment_contents_modal',
as: :experiment_contents_modal