demarches-normaliennes/app/models/export.rb

101 lines
2.4 KiB
Ruby
Raw Normal View History

2020-08-06 16:35:45 +02:00
# == Schema Information
#
# Table name: exports
#
# id :bigint not null, primary key
# format :string not null
2021-04-02 18:42:24 +02:00
# key :text not null
2020-08-06 16:35:45 +02:00
# created_at :datetime not null
# updated_at :datetime not null
#
2019-12-03 18:36:50 +01:00
class Export < ApplicationRecord
2021-04-08 12:47:29 +02:00
MAX_DUREE_CONSERVATION_EXPORT = 3.hours
2019-12-03 18:36:50 +01:00
enum format: {
csv: 'csv',
ods: 'ods',
xlsx: 'xlsx'
}
has_and_belongs_to_many :groupe_instructeurs
has_one_attached :file
validates :format, :groupe_instructeurs, :key, presence: true
2019-12-03 18:36:50 +01:00
scope :stale, -> { where('exports.updated_at < ?', (Time.zone.now - MAX_DUREE_CONSERVATION_EXPORT)) }
2019-12-03 18:36:50 +01:00
2020-09-29 14:00:31 +02:00
after_create_commit :compute_async
2019-12-03 18:36:50 +01:00
def compute_async
ExportJob.perform_later(self)
end
def compute
file.attach(
io: io,
filename: filename,
2020-02-24 10:33:31 +01:00
content_type: content_type,
# We generate the exports ourselves, so they are safe
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
2019-12-03 18:36:50 +01:00
)
end
def ready?
file.attached?
end
2021-04-08 12:47:29 +02:00
def old?
updated_at < 20.minutes.ago
end
2019-12-03 18:36:50 +01:00
def self.find_or_create_export(format, groupe_instructeurs)
create_with(groupe_instructeurs: groupe_instructeurs)
.create_or_find_by(format: format, key: generate_cache_key(groupe_instructeurs))
2019-12-03 18:36:50 +01:00
end
def self.find_for_format_and_groupe_instructeurs(format, groupe_instructeurs)
find_by(format: format, key: generate_cache_key(groupe_instructeurs))
end
def self.generate_cache_key(groupe_instructeurs)
groupe_instructeurs.map(&:id).sort.join('-')
2019-12-03 18:36:50 +01:00
end
private
def filename
procedure_identifier = procedure.path || "procedure-#{procedure.id}"
2019-12-03 18:36:50 +01:00
"dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}.#{format}"
end
def io
dossiers = Dossier.where(groupe_instructeur: groupe_instructeurs)
service = ProcedureExportService.new(procedure, dossiers)
case format.to_sym
when :csv
StringIO.new(service.to_csv)
when :xlsx
StringIO.new(service.to_xlsx)
when :ods
StringIO.new(service.to_ods)
end
end
def content_type
case format.to_sym
when :csv
'text/csv'
when :xlsx
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
when :ods
'application/vnd.oasis.opendocument.spreadsheet'
end
end
def procedure
groupe_instructeurs.first.procedure
end
end