Merge pull request #4441 from betagouv/fix/impossible-to-dl-stale
#4434 - Rend possible le fait de télécharger 2x un export
This commit is contained in:
commit
0b9650db78
3 changed files with 36 additions and 5 deletions
|
@ -207,16 +207,19 @@ module Instructeurs
|
||||||
|
|
||||||
def download_export
|
def download_export
|
||||||
export_format = params[:export_format]
|
export_format = params[:export_format]
|
||||||
|
notice_message = "Nous générons cet export. Lorsque celui-ci sera disponible, vous recevrez une notification par email accompagnée d'un lien de téléchargement."
|
||||||
if procedure.should_generate_export?(export_format)
|
if procedure.should_generate_export?(export_format)
|
||||||
procedure.queue_export(current_instructeur, export_format)
|
procedure.queue_export(current_instructeur, export_format)
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js do
|
format.js do
|
||||||
flash.notice = "Nous générons cet export. Lorsque celui-ci sera disponible, vous recevrez une notification par email accompagnée d'un lien de téléchargement."
|
flash.notice = notice_message
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elsif procedure.export_queued?(export_format)
|
||||||
|
flash.notice = notice_message
|
||||||
|
redirect_to procedure
|
||||||
else
|
else
|
||||||
redirect_to url_for(procedure.export_file(export_format))
|
redirect_to url_for(procedure.export_file(export_format))
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,9 +2,25 @@ class CleanupStaleExportsJob < ApplicationJob
|
||||||
queue_as :cron
|
queue_as :cron
|
||||||
|
|
||||||
def perform(*args)
|
def perform(*args)
|
||||||
ActiveStorage::Attachment.where(
|
attachments = ActiveStorage::Attachment.where(
|
||||||
"name in ('csv_export_file', 'ods_export_file', 'xlsx_export_file') and created_at < ?",
|
"name in ('csv_export_file', 'ods_export_file', 'xlsx_export_file') and created_at < ?",
|
||||||
Procedure::MAX_DUREE_CONSERVATION_EXPORT.ago
|
Procedure::MAX_DUREE_CONSERVATION_EXPORT.ago
|
||||||
).find_each(&:purge_later)
|
)
|
||||||
|
attachments.each do |attachment|
|
||||||
|
procedure = Procedure.find(attachment.record_id)
|
||||||
|
# export can't be queued if it's already attached
|
||||||
|
# so we clean the flag up just in case it was not removed during
|
||||||
|
# the asynchronous generation
|
||||||
|
case attachment.name
|
||||||
|
when 'csv_export_file'
|
||||||
|
procedure.update(csv_export_queued: false)
|
||||||
|
when 'ods_export_file'
|
||||||
|
procedure.update(ods_export_queued: false)
|
||||||
|
when 'xlsx_export_file'
|
||||||
|
procedure.update(xlsx_export_queued: false)
|
||||||
|
end
|
||||||
|
# and we remove the stale attachment
|
||||||
|
attachment.purge_later
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -145,6 +145,18 @@ class Procedure < ApplicationRecord
|
||||||
!ods_export_file.attached? || ods_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
!ods_export_file.attached? || ods_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def export_queued?(format)
|
||||||
|
case format.to_sym
|
||||||
|
when :csv
|
||||||
|
return csv_export_queued?
|
||||||
|
when :xlsx
|
||||||
|
return xlsx_export_queued?
|
||||||
|
when :ods
|
||||||
|
return ods_export_queued?
|
||||||
|
end
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
def should_generate_export?(format)
|
def should_generate_export?(format)
|
||||||
case format.to_sym
|
case format.to_sym
|
||||||
when :csv
|
when :csv
|
||||||
|
@ -169,7 +181,6 @@ class Procedure < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def queue_export(instructeur, export_format)
|
def queue_export(instructeur, export_format)
|
||||||
ExportProcedureJob.perform_now(self, instructeur, export_format)
|
|
||||||
case export_format.to_sym
|
case export_format.to_sym
|
||||||
when :csv
|
when :csv
|
||||||
update(csv_export_queued: true)
|
update(csv_export_queued: true)
|
||||||
|
@ -178,6 +189,7 @@ class Procedure < ApplicationRecord
|
||||||
when :ods
|
when :ods
|
||||||
update(ods_export_queued: true)
|
update(ods_export_queued: true)
|
||||||
end
|
end
|
||||||
|
ExportProcedureJob.perform_later(self, instructeur, export_format)
|
||||||
end
|
end
|
||||||
|
|
||||||
def prepare_export_download(format)
|
def prepare_export_download(format)
|
||||||
|
|
Loading…
Add table
Reference in a new issue