From b1b8f1b12ec5e6906bd99fbb0e6a3dd7bff4cf4e Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Sun, 18 Dec 2022 11:51:59 +0100 Subject: [PATCH 1/3] routes --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index e762ae61b..64e70c7d4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -78,6 +78,8 @@ Rails.application.routes.draw do match "/delayed_job" => DelayedJobWeb, :anchor => false, :via => [:get, :post] end + get 'import_procedure_tags' => 'procedures#import_data' + post 'import_tags' => 'procedures#import_tags' root to: "administrateurs#index" end From 86e1d1dd9761080d1bdf5d8960a72faf6be24e61 Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Sun, 18 Dec 2022 11:52:18 +0100 Subject: [PATCH 2/3] layout --- app/views/manager/application/_navigation.html.erb | 1 + app/views/manager/procedures/import_data.html.erb | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 app/views/manager/procedures/import_data.html.erb diff --git a/app/views/manager/application/_navigation.html.erb b/app/views/manager/application/_navigation.html.erb index 6ba4253fe..aaff0d2c8 100644 --- a/app/views/manager/application/_navigation.html.erb +++ b/app/views/manager/application/_navigation.html.erb @@ -25,6 +25,7 @@ as defined by the routes in the `admin/` namespace <%= link_to "Delayed Jobs", manager_delayed_job_path, class: "navigation__link" %> <%= link_to "Features", manager_flipper_path, class: "navigation__link" %> + <%= link_to "Import data via CSV", manager_import_procedure_tags_path, class: "navigation__link" %> <% if Rails.application.secrets.sendinblue[:enabled] && ENV["SAML_IDP_ENABLED"] == "enabled" %> <%= link_to "Sendinblue", ENV.fetch("SENDINBLUE_LOGIN_URL"), class: "navigation__link", target: '_blank' %> <% end %> diff --git a/app/views/manager/procedures/import_data.html.erb b/app/views/manager/procedures/import_data.html.erb new file mode 100644 index 000000000..ac24f7eb4 --- /dev/null +++ b/app/views/manager/procedures/import_data.html.erb @@ -0,0 +1,7 @@ +
+

Importer des tags sur des démarches

+ <%= form_tag(manager_import_tags_path, method: :post, multipart: true, style: 'margin-top: 1rem;') do |f| %> + <%= file_field_tag :tags_csv_file, required: true, accept: 'text/csv', size: "1" %> + <%= submit_tag "Importer", data: { disable_with: "Import en cours..." } , style: 'margin-top: 1rem;'%> + <% end %> +
From 7bdc7c2cad35feb894d3f7173f40b1dd4e1d53ec Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Sun, 18 Dec 2022 11:52:38 +0100 Subject: [PATCH 3/3] manager procedure controller --- .../manager/procedures_controller.rb | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/app/controllers/manager/procedures_controller.rb b/app/controllers/manager/procedures_controller.rb index 44337ad38..195b9a038 100644 --- a/app/controllers/manager/procedures_controller.rb +++ b/app/controllers/manager/procedures_controller.rb @@ -1,5 +1,10 @@ module Manager class ProceduresController < Manager::ApplicationController + CSV_MAX_SIZE = 1.megabytes + CSV_ACCEPTED_CONTENT_TYPES = [ + "text/csv", + "application/vnd.ms-excel" + ] # # Administrate overrides # @@ -103,6 +108,44 @@ module Manager redirect_to manager_procedure_path(procedure) end + def import_data + end + + def import_tags + if !CSV_ACCEPTED_CONTENT_TYPES.include?(tags_csv_file.content_type) && !CSV_ACCEPTED_CONTENT_TYPES.include?(marcel_content_type) + flash[:alert] = "Importation impossible : veuillez importer un fichier CSV" + + elsif tags_csv_file.size > CSV_MAX_SIZE + flash[:alert] = "Importation impossible : le poids du fichier est supérieur à #{number_to_human_size(CSV_MAX_SIZE)}" + + else + file = tags_csv_file.read + base_encoding = CharlockHolmes::EncodingDetector.detect(file) + procedure_tags = ACSV::CSV.new_for_ruby3(file.encode("UTF-8", base_encoding[:encoding], invalid: :replace, replace: ""), headers: true, header_converters: :downcase) + .map { |r| r.to_h.slice('demarche', 'tag') } + + invalid_ids = [] + procedure_tags.each do |procedure_tag| + procedure = Procedure.find_by(id: procedure_tag['demarche']) + tags = procedure_tag["tag"].split(',').map(&:strip).map(&:capitalize) + + if procedure.nil? + invalid_ids << procedure_tag['demarche'] + next + end + + tags.each do |tag| + procedure.tags.push(tag) + end + procedure.save + end + end + message = "Import des tags terminé." + message += " Ces démarches n'existent pas : #{invalid_ids.to_sentence}" if invalid_ids.any? + flash.notice = message + redirect_to manager_administrateurs_path + end + private def procedure @@ -121,6 +164,14 @@ module Manager params.require(:procedure).permit(:tags) end + def tags_csv_file + params[:tags_csv_file] + end + + def marcel_content_type + Marcel::MimeType.for(tags_csv_file.read, name: tags_csv_file.original_filename, declared_type: tags_csv_file.content_type) + end + def unfiltered_list? action_name == "index" && !params[:search] end