Merge pull request #4804 from tchak/remove_dead_export_code
Remove old export code
This commit is contained in:
commit
c6bfcd5c2f
9 changed files with 1 additions and 376 deletions
|
@ -1,26 +0,0 @@
|
||||||
class CleanupStaleExportsJob < ApplicationJob
|
|
||||||
queue_as :cron
|
|
||||||
|
|
||||||
def perform(*args)
|
|
||||||
attachments = ActiveStorage::Attachment.where(
|
|
||||||
"name in ('csv_export_file', 'ods_export_file', 'xlsx_export_file') and created_at < ?",
|
|
||||||
Procedure::MAX_DUREE_CONSERVATION_EXPORT.ago
|
|
||||||
)
|
|
||||||
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
|
|
|
@ -1,6 +0,0 @@
|
||||||
class ExportProcedureJob < ApplicationJob
|
|
||||||
def perform(procedure, instructeur, export_format)
|
|
||||||
procedure.prepare_export_download(export_format)
|
|
||||||
InstructeurMailer.notify_procedure_export_available(instructeur, procedure, export_format).deliver_later
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -42,12 +42,4 @@ class InstructeurMailer < ApplicationMailer
|
||||||
|
|
||||||
mail(to: instructeur.email, subject: subject)
|
mail(to: instructeur.email, subject: subject)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_procedure_export_available(instructeur, procedure, export_format)
|
|
||||||
@procedure = procedure
|
|
||||||
@export_format = export_format
|
|
||||||
subject = "Votre export de la démarche nº #{procedure.id} est disponible"
|
|
||||||
|
|
||||||
mail(to: instructeur.email, subject: subject)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require Rails.root.join('lib', 'percentile')
|
require Rails.root.join('lib', 'percentile')
|
||||||
|
|
||||||
class Procedure < ApplicationRecord
|
class Procedure < ApplicationRecord
|
||||||
self.ignored_columns = ['archived_at']
|
self.ignored_columns = ['archived_at', 'csv_export_queued', 'xlsx_export_queued', 'ods_export_queued']
|
||||||
|
|
||||||
include ProcedureStatsConcern
|
include ProcedureStatsConcern
|
||||||
|
|
||||||
|
@ -41,10 +41,6 @@ class Procedure < ApplicationRecord
|
||||||
has_one_attached :notice
|
has_one_attached :notice
|
||||||
has_one_attached :deliberation
|
has_one_attached :deliberation
|
||||||
|
|
||||||
has_one_attached :csv_export_file
|
|
||||||
has_one_attached :xlsx_export_file
|
|
||||||
has_one_attached :ods_export_file
|
|
||||||
|
|
||||||
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
||||||
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
||||||
|
|
||||||
|
@ -133,100 +129,11 @@ class Procedure < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def csv_export_stale?
|
|
||||||
!csv_export_file.attached? || csv_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
|
||||||
end
|
|
||||||
|
|
||||||
def xlsx_export_stale?
|
|
||||||
!xlsx_export_file.attached? || xlsx_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
|
||||||
end
|
|
||||||
|
|
||||||
def ods_export_stale?
|
|
||||||
!ods_export_file.attached? || ods_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
|
||||||
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)
|
|
||||||
case format.to_sym
|
|
||||||
when :csv
|
|
||||||
return csv_export_stale? && !csv_export_queued?
|
|
||||||
when :xlsx
|
|
||||||
return xlsx_export_stale? && !xlsx_export_queued?
|
|
||||||
when :ods
|
|
||||||
return ods_export_stale? && !ods_export_queued?
|
|
||||||
end
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def export_file(export_format)
|
|
||||||
case export_format.to_sym
|
|
||||||
when :csv
|
|
||||||
csv_export_file
|
|
||||||
when :xlsx
|
|
||||||
xlsx_export_file
|
|
||||||
when :ods
|
|
||||||
ods_export_file
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def queue_export(instructeur, export_format)
|
|
||||||
case export_format.to_sym
|
|
||||||
when :csv
|
|
||||||
update(csv_export_queued: true)
|
|
||||||
when :xlsx
|
|
||||||
update(xlsx_export_queued: true)
|
|
||||||
when :ods
|
|
||||||
update(ods_export_queued: true)
|
|
||||||
end
|
|
||||||
ExportProcedureJob.perform_later(self, instructeur, export_format)
|
|
||||||
end
|
|
||||||
|
|
||||||
def prepare_export_download(format)
|
|
||||||
service = ProcedureExportService.new(self, self.dossiers)
|
|
||||||
filename = export_filename(format)
|
|
||||||
|
|
||||||
case format.to_sym
|
|
||||||
when :csv
|
|
||||||
csv_export_file.attach(
|
|
||||||
io: StringIO.new(service.to_csv),
|
|
||||||
filename: filename,
|
|
||||||
content_type: 'text/csv'
|
|
||||||
)
|
|
||||||
update(csv_export_queued: false)
|
|
||||||
when :xlsx
|
|
||||||
xlsx_export_file.attach(
|
|
||||||
io: StringIO.new(service.to_xlsx),
|
|
||||||
filename: filename,
|
|
||||||
content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
||||||
)
|
|
||||||
update(xlsx_export_queued: false)
|
|
||||||
when :ods
|
|
||||||
ods_export_file.attach(
|
|
||||||
io: StringIO.new(service.to_ods),
|
|
||||||
filename: filename,
|
|
||||||
content_type: 'application/vnd.oasis.opendocument.spreadsheet'
|
|
||||||
)
|
|
||||||
update(ods_export_queued: false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def reset!
|
def reset!
|
||||||
if locked?
|
if locked?
|
||||||
raise "Can not reset a locked procedure."
|
raise "Can not reset a locked procedure."
|
||||||
else
|
else
|
||||||
groupe_instructeurs.each { |gi| gi.dossiers.destroy_all }
|
groupe_instructeurs.each { |gi| gi.dossiers.destroy_all }
|
||||||
purge_export_files
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -268,14 +175,6 @@ class Procedure < ApplicationRecord
|
||||||
procedure.blank? || administrateur.owns?(procedure)
|
procedure.blank? || administrateur.owns?(procedure)
|
||||||
end
|
end
|
||||||
|
|
||||||
def purge_export_files
|
|
||||||
xlsx_export_file.purge_later
|
|
||||||
ods_export_file.purge_later
|
|
||||||
csv_export_file.purge_later
|
|
||||||
|
|
||||||
update(csv_export_queued: false, xlsx_export_queued: false, ods_export_queued: false)
|
|
||||||
end
|
|
||||||
|
|
||||||
def locked?
|
def locked?
|
||||||
publiee? || close? || depubliee?
|
publiee? || close? || depubliee?
|
||||||
end
|
end
|
||||||
|
@ -594,7 +493,6 @@ class Procedure < ApplicationRecord
|
||||||
def hide!
|
def hide!
|
||||||
discard!
|
discard!
|
||||||
dossiers.discard_all
|
dossiers.discard_all
|
||||||
purge_export_files
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -626,7 +524,6 @@ class Procedure < ApplicationRecord
|
||||||
def after_close
|
def after_close
|
||||||
now = Time.zone.now
|
now = Time.zone.now
|
||||||
update!(closed_at: now)
|
update!(closed_at: now)
|
||||||
purge_export_files
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_unpublish
|
def after_unpublish
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
%p
|
|
||||||
Bonjour,
|
|
||||||
|
|
||||||
%p
|
|
||||||
Votre export des dossiers de la démarche nº #{@procedure.id} « #{@procedure.libelle} » au format #{@export_format} est prêt.
|
|
||||||
|
|
||||||
%p
|
|
||||||
Cliquez sur le lien ci-dessous pour le télécharger :
|
|
||||||
= link_to('Télécharger l\'export des dossiers', download_export_instructeur_procedure_url(@procedure, :export_format => @export_format))
|
|
||||||
|
|
||||||
= render partial: "layouts/mailers/signature"
|
|
|
@ -249,47 +249,5 @@ FactoryBot.define do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
trait :with_csv_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.csv_export_file.attach(io: StringIO.new("some csv data"), filename: "export.csv", content_type: "text/plain")
|
|
||||||
procedure.csv_export_file.update(created_at: 5.minutes.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_stale_csv_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.csv_export_file.attach(io: StringIO.new("some csv data"), filename: "export.csv", content_type: "text/plain")
|
|
||||||
procedure.csv_export_file.update(created_at: 4.hours.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_ods_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.ods_export_file.attach(io: StringIO.new("some ods data"), filename: "export.ods", content_type: "text/plain")
|
|
||||||
procedure.ods_export_file.update(created_at: 5.minutes.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_stale_ods_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.ods_export_file.attach(io: StringIO.new("some ods data"), filename: "export.ods", content_type: "text/plain")
|
|
||||||
procedure.ods_export_file.update(created_at: 4.hours.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_xlsx_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.xlsx_export_file.attach(io: StringIO.new("some xlsx data"), filename: "export.xlsx", content_type: "text/plain")
|
|
||||||
procedure.xlsx_export_file.update(created_at: 5.minutes.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
trait :with_stale_xlsx_export_file do
|
|
||||||
after(:create) do |procedure, _evaluator|
|
|
||||||
procedure.xlsx_export_file.attach(io: StringIO.new("some xlsx data"), filename: "export.xlsx", content_type: "text/plain")
|
|
||||||
procedure.xlsx_export_file.update(created_at: 4.hours.ago)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,18 +46,4 @@ RSpec.describe InstructeurMailer, type: :mailer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#notify_procedure_export_available' do
|
|
||||||
let(:instructeur) { create(:instructeur) }
|
|
||||||
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
|
|
||||||
let(:dossier) { create(:dossier, procedure: procedure) }
|
|
||||||
let(:format) { 'xlsx' }
|
|
||||||
|
|
||||||
context 'when the mail is sent' do
|
|
||||||
subject { described_class.notify_procedure_export_available(instructeur, procedure, format) }
|
|
||||||
it 'contains a download link' do
|
|
||||||
expect(subject.body).to include download_export_instructeur_procedure_url(procedure, :export_format => format)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,10 +37,6 @@ class InstructeurMailerPreview < ActionMailer::Preview
|
||||||
InstructeurMailer.send_notifications(instructeur, data)
|
InstructeurMailer.send_notifications(instructeur, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_procedure_export_available
|
|
||||||
InstructeurMailer.notify_procedure_export_available(instructeur, procedure, 'xlsx')
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def instructeur
|
def instructeur
|
||||||
|
|
|
@ -1075,165 +1075,4 @@ describe Procedure do
|
||||||
it { is_expected.to be false }
|
it { is_expected.to be false }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.ods_export_stale?' do
|
|
||||||
subject { procedure.ods_export_stale? }
|
|
||||||
|
|
||||||
context 'with no ods export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent ods export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_ods_export_file) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old ods export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_ods_export_file) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.csv_export_stale?' do
|
|
||||||
subject { procedure.csv_export_stale? }
|
|
||||||
|
|
||||||
context 'with no csv export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent csv export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_csv_export_file) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old csv export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_csv_export_file) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.xlsx_export_stale?' do
|
|
||||||
subject { procedure.xlsx_export_stale? }
|
|
||||||
|
|
||||||
context 'with no xlsx export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent xlsx export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_xlsx_export_file) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old xlsx export' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_xlsx_export_file) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.should_generate_export?' do
|
|
||||||
context 'xlsx' do
|
|
||||||
subject { procedure.should_generate_export?('xlsx') }
|
|
||||||
context 'with no export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_xlsx_export_file, xlsx_export_queued: false) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_xlsx_export_file, xlsx_export_queued: true) }
|
|
||||||
it { expect(procedure.xlsx_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_xlsx_export_file, xlsx_export_queued: false) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_xlsx_export_file, xlsx_export_queued: true) }
|
|
||||||
it { expect(procedure.xlsx_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'csv' do
|
|
||||||
subject { procedure.should_generate_export?('csv') }
|
|
||||||
context 'with no export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_csv_export_file, csv_export_queued: false) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_csv_export_file, csv_export_queued: true) }
|
|
||||||
it { expect(procedure.csv_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_csv_export_file, csv_export_queued: false) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_csv_export_file, csv_export_queued: true) }
|
|
||||||
it { expect(procedure.csv_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'ods' do
|
|
||||||
subject { procedure.should_generate_export?('ods') }
|
|
||||||
context 'with no export' do
|
|
||||||
let(:procedure) { create(:procedure) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with a recent export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_ods_export_file, ods_export_queued: false) }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_ods_export_file, ods_export_queued: true) }
|
|
||||||
it { expect(procedure.ods_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'with an old export' do
|
|
||||||
context 'when its not queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_ods_export_file, ods_export_queued: false) }
|
|
||||||
it { is_expected.to be true }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when its already queued' do
|
|
||||||
let(:procedure) { create(:procedure, :with_stale_ods_export_file, ods_export_queued: true) }
|
|
||||||
it { expect(procedure.ods_export_queued).to be true }
|
|
||||||
it { is_expected.to be false }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue