Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎨 Validation du format de fichier XLS lors de l'importation et ajout de messages d'erreur appropriés #1749

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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