From d14e13230534d91aa5afdf2b92f9cbbf9cc3a9a1 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 8 Apr 2022 17:12:53 +0200 Subject: [PATCH] feat(export): add zip format support --- app/models/export.rb | 17 ++++++++++++++--- app/services/procedure_export_service.rb | 9 +++++++++ .../views/instructeurs/procedures/fr.yml | 1 + spec/models/export_spec.rb | 6 +++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/models/export.rb b/app/models/export.rb index 988e78c1a..e6ceb89d7 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -18,8 +18,9 @@ class Export < ApplicationRecord enum format: { csv: 'csv', ods: 'ods', - xlsx: 'xlsx' - } + xlsx: 'xlsx', + zip: 'zip' + }, _prefix: true enum time_span_type: { everything: 'everything', @@ -52,7 +53,7 @@ class Export < ApplicationRecord { format: format, time_span_type: time_span_type } end end - FORMATS = [:xlsx, :ods, :csv].map do |format| + FORMATS = [:xlsx, :ods, :csv, :zip].map do |format| { format: format } end @@ -98,6 +99,10 @@ class Export < ApplicationRecord format == self.class.formats.fetch(:csv) end + def zip? + format == self.class.formats.fetch(:zip) + end + def self.find_or_create_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation, procedure_presentation_snapshot: procedure_presentation&.snapshot) .includes(:procedure_presentation) @@ -128,6 +133,10 @@ class Export < ApplicationRecord csv: { time_span_type: not_filtered.filter(&:csv?).index_by(&:time_span_type), statut: filtered.filter(&:csv?).index_by(&:statut) + }, + zip: { + time_span_type: {}, + statut: filtered.filter(&:zip?).index_by(&:statut) } } end @@ -181,6 +190,8 @@ class Export < ApplicationRecord service.to_xlsx when :ods service.to_ods + when :zip + service.to_zip end end diff --git a/app/services/procedure_export_service.rb b/app/services/procedure_export_service.rb index a9b3fc280..907426fd6 100644 --- a/app/services/procedure_export_service.rb +++ b/app/services/procedure_export_service.rb @@ -27,6 +27,13 @@ class ProcedureExportService end.bytes) create_blob(io, :ods) end + + def to_zip + attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers, true) + + DownloadableFileService.download_and_zip(procedure, attachments, base_filename) do |zip_filepath| + ArchiveUploader.new(procedure: procedure, filename: filename(:zip), filepath: zip_filepath).blob + end end private @@ -62,6 +69,8 @@ class ProcedureExportService 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' when :ods 'application/vnd.oasis.opendocument.spreadsheet' + when :zip + 'application/zip' end end diff --git a/config/locales/views/instructeurs/procedures/fr.yml b/config/locales/views/instructeurs/procedures/fr.yml index 9cf758507..b4139e687 100644 --- a/config/locales/views/instructeurs/procedures/fr.yml +++ b/config/locales/views/instructeurs/procedures/fr.yml @@ -5,6 +5,7 @@ fr: everything_csv_html: Demander un export au format .csv
(uniquement les dossiers, sans les champs répétables) everything_xlsx_html: Demander un export au format .xlsx everything_ods_html: Demander un export au format .ods + everything_zip_html: Demander un export au format .zip everything_short: Demander un export au format %{export_format} everything_pending_html: Un export au format %{export_format} est en train d’être généré
(demandé il y a %{export_time}) everything_ready_html: Télécharger l’export au format %{export_format}
(généré il y a %{export_time}) diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index 2bfdec18a..3511895e5 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -48,9 +48,9 @@ RSpec.describe Export, type: :model do context 'when an export is made for one groupe instructeur' do let!(:export) { create(:export, groupe_instructeurs: [gi_1, gi_2]) } - it { expect(Export.find_for_groupe_instructeurs([gi_1.id], nil)).to eq({ csv: { statut: {}, time_span_type: {} }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} } }) } - it { expect(Export.find_for_groupe_instructeurs([gi_2.id, gi_1.id], nil)).to eq({ csv: { statut: {}, time_span_type: { 'everything' => export } }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} } }) } - it { expect(Export.find_for_groupe_instructeurs([gi_1.id, gi_2.id, gi_3.id], nil)).to eq({ csv: { statut: {}, time_span_type: {} }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} } }) } + it { expect(Export.find_for_groupe_instructeurs([gi_1.id], nil)).to eq({ csv: { statut: {}, time_span_type: {} }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} }, zip: { statut: {}, time_span_type: {} } }) } + it { expect(Export.find_for_groupe_instructeurs([gi_2.id, gi_1.id], nil)).to eq({ csv: { statut: {}, time_span_type: { 'everything' => export } }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} }, zip: { statut: {}, time_span_type: {} } }) } + it { expect(Export.find_for_groupe_instructeurs([gi_1.id, gi_2.id, gi_3.id], nil)).to eq({ csv: { statut: {}, time_span_type: {} }, xlsx: { statut: {}, time_span_type: {} }, ods: { statut: {}, time_span_type: {} }, zip: { statut: {}, time_span_type: {} } }) } end end end