Skip to content

Commit

Permalink
🎨 Validation du format de fichier XLS lors de l'importation et ajout …
Browse files Browse the repository at this point in the history
…de messages d'erreur appropriés
  • Loading branch information
Guitguitou authored and cprodhomme committed Nov 19, 2024
1 parent 2f7a34e commit 3fa898f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
controller do
def import_xls
return if params[:file_xls].blank?
return invalide_format_xls unless ImportExport::ImportXls.fichier_xls?(params[:file_xls])

initialise_import
flash[:success] = I18n.t('.layouts.succes.import_question')
Expand Down Expand Up @@ -52,5 +53,10 @@ def redirection_apres_import

redirection_paths[params[:type]] || admin_questions_path
end

def invalide_format_xls
flash[:error] = I18n.t('.layouts.erreurs.import_question.format_invalide', format: 'XLS')
redirect_to admin_import_xls_path(type: params[:type])
end
end
end
6 changes: 6 additions & 0 deletions app/models/import_export/import_xls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ def attache_fichier(attachment, url)
def message_erreur_validation(exception, row)
"Erreur ligne #{row}: #{exception.record.errors.full_messages.to_sentence}"
end

XLS_MIME_TYPE = 'application/vnd.ms-excel'

def self.fichier_xls?(file)
file.content_type == XLS_MIME_TYPE && File.extname(file.original_filename) == '.xls'
end
end
end
2 changes: 2 additions & 0 deletions config/locales/views/erreurs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ fr:
message-accueil: Oups…
retour-accueil: Retour à l'accueil
import_question:
format_invalide: |
Le fichier n’est pas au bon format. Il doit être au format .xls
mauvais_format: |
Le fichier n'est pas au bon format. Il doit contenir les colonnes dans l'ordre suivant : %{headers}
succes:
Expand Down

0 comments on commit 3fa898f

Please sign in to comment.