Merge pull request #6335 from betagouv/fix-import-csv
Administrateur : corrige l'import de fichiers CSV utilisant d'autres encodages que l'UTF-8 (#6335)
This commit is contained in:
commit
674d8f9a9b
5 changed files with 22 additions and 1 deletions
1
Gemfile
1
Gemfile
|
@ -12,6 +12,7 @@ gem 'anchored'
|
|||
gem 'bcrypt'
|
||||
gem 'bootsnap', '>= 1.4.4', require: false # Reduces boot times through caching; required in config/boot.rb
|
||||
gem 'browser'
|
||||
gem 'charlock_holmes'
|
||||
gem 'chartkick'
|
||||
gem 'chunky_png'
|
||||
gem 'clamav-client', require: 'clamav/client'
|
||||
|
|
|
@ -155,6 +155,7 @@ GEM
|
|||
marcel (~> 1.0)
|
||||
nokogiri (~> 1.10, >= 1.10.4)
|
||||
rubyzip (>= 1.3.0, < 3)
|
||||
charlock_holmes (0.7.7)
|
||||
chartkick (3.4.2)
|
||||
childprocess (3.0.0)
|
||||
choice (0.2.0)
|
||||
|
@ -793,6 +794,7 @@ DEPENDENCIES
|
|||
capybara-email
|
||||
capybara-screenshot
|
||||
capybara-selenium
|
||||
charlock_holmes
|
||||
chartkick
|
||||
chunky_png
|
||||
clamav-client
|
||||
|
|
|
@ -170,7 +170,10 @@ module NewAdministrateur
|
|||
redirect_to admin_procedure_groupe_instructeurs_path(procedure)
|
||||
|
||||
else
|
||||
groupes_emails = CSV.new(group_csv_file.read.force_encoding("UTF-8"), headers: true, header_converters: :downcase)
|
||||
file = group_csv_file.read
|
||||
# find the original encoding to avoid encoding errors
|
||||
base_encoding = CharlockHolmes::EncodingDetector.detect(file)
|
||||
groupes_emails = CSV.new(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true, header_converters: :downcase)
|
||||
.map { |r| r.to_h.slice('groupe', 'email') }
|
||||
|
||||
groupes_emails_has_keys = groupes_emails.first.has_key?("groupe") && groupes_emails.first.has_key?("email")
|
||||
|
|
|
@ -365,6 +365,16 @@ describe NewAdministrateur::GroupeInstructeursController, type: :controller do
|
|||
it { expect(flash.alert).to eq("Import terminé. Cependant les emails suivants ne sont pas pris en compte: kara") }
|
||||
end
|
||||
|
||||
context 'when the content of csv contains special characters' do
|
||||
let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe_avec_caracteres_speciaux.csv', 'text/csv') }
|
||||
|
||||
before { subject }
|
||||
|
||||
it { expect(procedure.groupe_instructeurs.pluck(:label)).to eq(["défaut", "Auvergne-Rhône-Alpes", "Vendée"]) }
|
||||
it { expect(flash.notice).to be_present }
|
||||
it { expect(flash.notice).to eq("La liste des instructeurs a été importée avec succès") }
|
||||
end
|
||||
|
||||
context 'when the csv file length is more than 1 mo' do
|
||||
let(:csv_file) { fixture_file_upload('spec/fixtures/files/groupe-instructeur.csv', 'text/csv') }
|
||||
|
||||
|
|
5
spec/fixtures/files/groupe_avec_caracteres_speciaux.csv
vendored
Normal file
5
spec/fixtures/files/groupe_avec_caracteres_speciaux.csv
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
Email,Groupe
|
||||
kara@beta.gouv.fr,Auvergne-Rhône-Alpes
|
||||
camilya@beta.gouv.fr,Vendée
|
||||
philippe@beta.gouv.fr,Auvergne-Rhône-Alpes
|
||||
sim@beta.gouv.fr,Vendée
|
|
Loading…
Reference in a new issue