Merge pull request #8070 from demarches-simplifiees/seed-database-tags
ETQ SuperAdmin, je souhaite associer en masse des tags avec des démarches
This commit is contained in:
commit
5135e28426
4 changed files with 61 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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 %>
|
||||
|
|
7
app/views/manager/procedures/import_data.html.erb
Normal file
7
app/views/manager/procedures/import_data.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
|||
<div style='margin-top: 1rem; margin-left: 1rem;'>
|
||||
<h1>Importer des tags sur des démarches</h1>
|
||||
<%= 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 %>
|
||||
</div>
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue