From ff44fbe523efc1763f73f78cd71bb7b216f92478 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 31 Oct 2017 15:16:55 +0100 Subject: [PATCH 01/15] Add carrierwave i18n gem --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index bab9650cb..12b99fc68 100644 --- a/Gemfile +++ b/Gemfile @@ -50,6 +50,7 @@ gem 'rest-client' gem 'clamav-client', require: 'clamav/client' gem 'carrierwave' +gem 'carrierwave-i18n' gem 'copy_carrierwave_file' gem 'fog' gem 'fog-openstack' diff --git a/Gemfile.lock b/Gemfile.lock index 85c42902f..9f8963016 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -96,6 +96,7 @@ GEM json (>= 1.7) mime-types (>= 1.16) mimemagic (>= 0.3.0) + carrierwave-i18n (0.2.0) chartkick (2.2.1) chunky_png (1.3.8) clamav-client (3.1.0) @@ -683,6 +684,7 @@ DEPENDENCIES byebug capybara carrierwave + carrierwave-i18n chartkick chunky_png clamav-client From 57759018689dce21e38c116d5d7e667910a532ad Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Oct 2017 16:07:02 +0100 Subject: [PATCH 02/15] Add commentaire file uploader --- app/models/commentaire.rb | 3 ++- app/uploaders/commentaire_file_uploader.rb | 15 +++++++++++++++ .../20171030150323_add_file_to_commentaires.rb | 5 +++++ db/schema.rb | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/uploaders/commentaire_file_uploader.rb create mode 100644 db/migrate/20171030150323_add_file_to_commentaires.rb diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 5fd425029..4af0211dc 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -1,9 +1,10 @@ class Commentaire < ActiveRecord::Base belongs_to :dossier, touch: true belongs_to :champ - belongs_to :piece_justificative + mount_uploader :file, CommentaireFileUploader + default_scope { order(created_at: :asc) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } diff --git a/app/uploaders/commentaire_file_uploader.rb b/app/uploaders/commentaire_file_uploader.rb new file mode 100644 index 000000000..35dfedfbd --- /dev/null +++ b/app/uploaders/commentaire_file_uploader.rb @@ -0,0 +1,15 @@ +class CommentaireFileUploader < BaseUploader + def root + File.join(Rails.root, 'public') + end + + if Features.remote_storage + storage :fog + else + storage :file + end + + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end +end diff --git a/db/migrate/20171030150323_add_file_to_commentaires.rb b/db/migrate/20171030150323_add_file_to_commentaires.rb new file mode 100644 index 000000000..7e803453e --- /dev/null +++ b/db/migrate/20171030150323_add_file_to_commentaires.rb @@ -0,0 +1,5 @@ +class AddFileToCommentaires < ActiveRecord::Migration[5.0] + def change + add_column :commentaires, :file, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e69a4eae7..5a81e1b0f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -163,6 +163,7 @@ ActiveRecord::Schema.define(version: 20171117165748) do t.datetime "updated_at", null: false t.integer "piece_justificative_id" t.integer "champ_id" + t.string "file" t.index ["champ_id"], name: "index_commentaires_on_champ_id", using: :btree t.index ["dossier_id"], name: "index_commentaires_on_dossier_id", using: :btree end From ae5e6653cb1e591dd90a75aeb393b9be0ca40205 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Oct 2017 16:16:20 +0100 Subject: [PATCH 03/15] [Fix #888] New UI : Add upload on commentaire form --- .../new_gestionnaire/dossiers_controller.rb | 2 +- .../dossiers/messagerie.html.haml | 16 +++++++++++----- .../new_gestionnaire/dossiers_controller_spec.rb | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 5adafcdf3..cdd786a1f 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -99,7 +99,7 @@ module NewGestionnaire end def commentaire_params - params.require(:commentaire).permit(:body) + params.require(:commentaire).permit(:body, :file) end def avis_params diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index 09e456ed3..ee5b92180 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -15,15 +15,21 @@ - if ![current_gestionnaire.email, @dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) %span.guest Invité %span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y') - .rich-text - = sanitize(commentaire.body) - - if file = commentaire.piece_justificative + %p.rich-text= sanitize(commentaire.body) + + - if commentaire.file.present? .attachment-link - = link_to file.content_url, class: "button", target: "_blank", title: "Télécharger" do + = link_to commentaire.file.url, class: "button", target: "_blank", title: "Télécharger" do .icon.attachment - = file.original_filename + = commentaire.file_identifier + - elsif commentaire.piece_justificative + .attachment-link + = link_to commentaire.piece_justificative.content_url, class: "button", target: "_blank", title: "Télécharger" do + .icon.attachment + = commentaire.piece_justificative.original_filename = form_for(Commentaire.new, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true + = f.file_field :file .send-wrapper = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi..." } diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index a3b7416d1..5783dd869 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -111,12 +111,18 @@ describe NewGestionnaire::DossiersController, type: :controller do describe "#create_commentaire" do let(:saved_commentaire) { dossier.commentaires.first } + let(:file) { nil } before do + allow(ClamavService).to receive(:safe_file?).and_return(true) + post :create_commentaire, params: { procedure_id: procedure.id, dossier_id: dossier.id, - commentaire: { body: 'body' } + commentaire: { + body: 'body', + file: file + } } end @@ -125,6 +131,13 @@ describe NewGestionnaire::DossiersController, type: :controller do it { expect(saved_commentaire.dossier).to eq(dossier) } it { expect(response).to redirect_to(messagerie_dossier_path(dossier.procedure, dossier)) } it { expect(gestionnaire.followed_dossiers).to include(dossier) } + it { expect(saved_commentaire.file.present?).to eq(false) } + + context "with a file" do + let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } + + it { expect(saved_commentaire.file.present?).to eq(true) } + end end describe "#create_avis" do From bdadf12581313de4a2588bfb5fc5fca93967d820 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Oct 2017 18:29:14 +0100 Subject: [PATCH 04/15] Rake task : Migrate old piece justificative in commentaire to file attribute --- ...mmentaire_piece_justificative_to_file.rake | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lib/tasks/2017_10_30_copy_commentaire_piece_justificative_to_file.rake diff --git a/lib/tasks/2017_10_30_copy_commentaire_piece_justificative_to_file.rake b/lib/tasks/2017_10_30_copy_commentaire_piece_justificative_to_file.rake new file mode 100644 index 000000000..7b39d13bc --- /dev/null +++ b/lib/tasks/2017_10_30_copy_commentaire_piece_justificative_to_file.rake @@ -0,0 +1,26 @@ +namespace :'2017_10_30_copy_commentaire_piece_justificative_to_file' do + task set: :environment do + commentaires_to_process = Commentaire.where(file:nil).where.not(piece_justificative_id: nil).reorder(id: :desc) + + puts "#{commentaires_to_process.count} commentaires to process..." + + commentaires_to_process.each do |c| + process_commentaire(c) + end + end + + def process_commentaire commentaire + puts "Processing commentaire #{commentaire.id}" + # https://github.com/carrierwaveuploader/carrierwave#uploading-files-from-a-remote-location + commentaire.remote_file_url = commentaire.piece_justificative.content_url + + if commentaire.piece_justificative.original_filename.present? + commentaire.file.define_singleton_method(:filename) { commentaire.piece_justificative.original_filename } + end + + commentaire.save + if !commentaire.file.present? + puts "Failed to save file for commentaire #{commentaire.id}" + end + end +end From 158adc2472f79fbc8ac49be29062fe6961b6c561 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 30 Oct 2017 17:21:05 +0100 Subject: [PATCH 05/15] New UI : Check if commentaire is correctly saved --- .../new_gestionnaire/dossiers_controller.rb | 21 +++++++++++-------- .../dossiers/messagerie.html.haml | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index cdd786a1f..7dfa157ed 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -5,25 +5,22 @@ module NewGestionnaire end def show - @dossier = dossier dossier.notifications.demande.mark_as_read current_gestionnaire.mark_tab_as_seen(dossier, :demande) end def messagerie - @dossier = dossier dossier.notifications.messagerie.mark_as_read current_gestionnaire.mark_tab_as_seen(dossier, :messagerie) + @commentaire = Commentaire.new end def annotations_privees - @dossier = dossier dossier.notifications.annotations_privees.mark_as_read current_gestionnaire.mark_tab_as_seen(dossier, :annotations_privees) end def avis - @dossier = dossier dossier.notifications.avis.mark_as_read current_gestionnaire.mark_tab_as_seen(dossier, :avis) end @@ -54,10 +51,16 @@ module NewGestionnaire end def create_commentaire - Commentaire.create(commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier)) - current_gestionnaire.follow(dossier) - flash.notice = "Message envoyé" - redirect_to messagerie_dossier_path(dossier.procedure, dossier) + @commentaire = Commentaire.new(commentaire_params.merge(email: current_gestionnaire.email, dossier: dossier)) + + if @commentaire.save + current_gestionnaire.follow(dossier) + flash.notice = "Message envoyé" + redirect_to messagerie_dossier_path(dossier.procedure, dossier) + else + flash.alert = @commentaire.errors.full_messages + render :messagerie + end end def position @@ -95,7 +98,7 @@ module NewGestionnaire private def dossier - current_gestionnaire.dossiers.find(params[:dossier_id]) + @dossier ||= current_gestionnaire.dossiers.find(params[:dossier_id]) end def commentaire_params diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index ee5b92180..b89b49fab 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -28,7 +28,7 @@ .icon.attachment = commentaire.piece_justificative.original_filename - = form_for(Commentaire.new, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| + = form_for(@commentaire, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true = f.file_field :file .send-wrapper From 76cb54988dfe3da9081cf5b9efa9508833237172 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 8 Nov 2017 16:37:04 +0100 Subject: [PATCH 06/15] Validate virus free with Clamav --- app/models/commentaire.rb | 7 ++++ .../dossiers_controller_spec.rb | 34 +++++++++++++------ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 4af0211dc..34005efb1 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,6 +4,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative mount_uploader :file, CommentaireFileUploader + validate :is_virus_free? default_scope { order(created_at: :asc) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } @@ -44,4 +45,10 @@ class Commentaire < ActiveRecord::Base def notify_user NotificationMailer.new_answer(dossier).deliver_now! end + + def is_virus_free? + if file.present? && file_changed? && !ClamavService.safe_file?(file.path) + errors.add(:file, "Virus détecté dans le fichier joint, merci de changer de fichier") + end + end end diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 5783dd869..1c8347c65 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -112,10 +112,9 @@ describe NewGestionnaire::DossiersController, type: :controller do describe "#create_commentaire" do let(:saved_commentaire) { dossier.commentaires.first } let(:file) { nil } + let(:scan_result) { true } - before do - allow(ClamavService).to receive(:safe_file?).and_return(true) - + subject { post :create_commentaire, params: { procedure_id: procedure.id, dossier_id: dossier.id, @@ -124,19 +123,34 @@ describe NewGestionnaire::DossiersController, type: :controller do file: file } } + } + + before do + allow(ClamavService).to receive(:safe_file?).and_return(scan_result) end - it { expect(saved_commentaire.body).to eq('body') } - it { expect(saved_commentaire.email).to eq(gestionnaire.email) } - it { expect(saved_commentaire.dossier).to eq(dossier) } - it { expect(response).to redirect_to(messagerie_dossier_path(dossier.procedure, dossier)) } - it { expect(gestionnaire.followed_dossiers).to include(dossier) } - it { expect(saved_commentaire.file.present?).to eq(false) } + it do + subject + + expect(saved_commentaire.body).to eq('body') + expect(saved_commentaire.email).to eq(gestionnaire.email) + expect(saved_commentaire.dossier).to eq(dossier) + expect(response).to redirect_to(messagerie_dossier_path(dossier.procedure, dossier)) + expect(gestionnaire.followed_dossiers).to include(dossier) + expect(saved_commentaire.file.present?).to eq(false) + end context "with a file" do let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } - it { expect(saved_commentaire.file.present?).to eq(true) } + it { subject; expect(saved_commentaire.file.present?).to eq(true) } + it { expect { subject }.to change(Commentaire, :count).by(1) } + + context "and a virus" do + let(:scan_result) { false } + + it { expect { subject }.not_to change(Commentaire, :count) } + end end end From 6c39aba9789becb67d4ecbf1e016e9fe76cd4052 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 8 Nov 2017 16:38:07 +0100 Subject: [PATCH 07/15] Validates commentaire file size --- app/models/commentaire.rb | 1 + config/locales/models/commentaire/fr.yml | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 config/locales/models/commentaire/fr.yml diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 34005efb1..814be3efa 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -4,6 +4,7 @@ class Commentaire < ActiveRecord::Base belongs_to :piece_justificative mount_uploader :file, CommentaireFileUploader + validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" } validate :is_virus_free? default_scope { order(created_at: :asc) } diff --git a/config/locales/models/commentaire/fr.yml b/config/locales/models/commentaire/fr.yml new file mode 100644 index 000000000..16b77c012 --- /dev/null +++ b/config/locales/models/commentaire/fr.yml @@ -0,0 +1,5 @@ +fr: + activerecord: + attributes: + commentaire: + file: fichier From 4d12aa222c1f5414eed64e3b22516bcfeb1fc362 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Nov 2017 20:40:00 +0100 Subject: [PATCH 08/15] Validate body is present --- app/models/commentaire.rb | 1 + .../new_gestionnaire/dossiers_controller_spec.rb | 11 ++++++++++- spec/models/commentaire_spec.rb | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 814be3efa..1492c4bcb 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -6,6 +6,7 @@ class Commentaire < ActiveRecord::Base mount_uploader :file, CommentaireFileUploader validates :file, file_size: { maximum: 20.megabytes, message: "La taille du fichier doit être inférieure à 20 Mo" } validate :is_virus_free? + validates_presence_of :body, message: "Votre message ne peut être vide" default_scope { order(created_at: :asc) } scope :updated_since?, -> (date) { where('commentaires.updated_at > ?', date) } diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 1c8347c65..c9147b1f3 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -111,6 +111,7 @@ describe NewGestionnaire::DossiersController, type: :controller do describe "#create_commentaire" do let(:saved_commentaire) { dossier.commentaires.first } + let(:body) { "body" } let(:file) { nil } let(:scan_result) { true } @@ -119,7 +120,7 @@ describe NewGestionnaire::DossiersController, type: :controller do procedure_id: procedure.id, dossier_id: dossier.id, commentaire: { - body: 'body', + body: body, file: file } } @@ -140,6 +141,14 @@ describe NewGestionnaire::DossiersController, type: :controller do expect(saved_commentaire.file.present?).to eq(false) end + it { expect { subject }.to change(Commentaire, :count).by(1) } + + context "without a body" do + let(:body) { nil } + + it { expect { subject }.not_to change(Commentaire, :count) } + end + context "with a file" do let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb index 8decaf4ae..802f9ae85 100644 --- a/spec/models/commentaire_spec.rb +++ b/spec/models/commentaire_spec.rb @@ -15,7 +15,7 @@ describe Commentaire do let(:assign_to) { create(:assign_to, gestionnaire: gestionnaire, procedure: procedure) } let(:user) { create(:user) } let(:dossier) { create(:dossier, procedure: procedure, user: user) } - let(:commentaire) { Commentaire.new(dossier: dossier) } + let(:commentaire) { Commentaire.new(dossier: dossier, body: "Mon commentaire") } context "with a commentaire created by a user" do it "calls notify_gestionnaires" do From 7f4652fd1ec0a2382578158c648a7ce7ff51ff67 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 31 Oct 2017 16:21:26 +0100 Subject: [PATCH 09/15] New UI : Accept only few extensions --- app/uploaders/commentaire_file_uploader.rb | 8 ++++++++ app/views/new_gestionnaire/dossiers/messagerie.html.haml | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/uploaders/commentaire_file_uploader.rb b/app/uploaders/commentaire_file_uploader.rb index 35dfedfbd..6b5d95fcb 100644 --- a/app/uploaders/commentaire_file_uploader.rb +++ b/app/uploaders/commentaire_file_uploader.rb @@ -12,4 +12,12 @@ class CommentaireFileUploader < BaseUploader def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end + + def extension_white_list + %w(pdf doc docx xls xlsx ppt pptx odt ods odp jpg jpeg png zip txt) + end + + def accept_extension_list + extension_white_list.map{ |e| ".#{e}" }.join(",") + end end diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index b89b49fab..e289cfdbc 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -30,6 +30,6 @@ = form_for(@commentaire, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true - = f.file_field :file + = f.file_field :file, accept: @commentaire.accept_extension_list .send-wrapper = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi..." } From 7ea7eea9520616dc4ee0ae9b0bec31a1aee0ec07 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 31 Oct 2017 16:22:27 +0100 Subject: [PATCH 10/15] New UI : Style file uploader --- .../new_gestionnaire/dossiers/messagerie.html.haml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index e289cfdbc..691549ecb 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -30,6 +30,12 @@ = form_for(@commentaire, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true - = f.file_field :file, accept: @commentaire.accept_extension_list - .send-wrapper - = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi..." } + .flex.justify-between + %div + = f.file_field :file, id: :file, accept: @commentaire.file.accept_extension_list + %label{ for: :file } + .notice + (taille max : 20 Mo) + + .send-wrapper + = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi…" } From 150ff5e7ca3c83595070f0cb5384e3e175433617 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Nov 2017 17:15:05 +0100 Subject: [PATCH 11/15] New UI : Use RemoteDownloader class to hide ovh url --- app/models/commentaire.rb | 8 ++++++++ app/views/new_gestionnaire/dossiers/messagerie.html.haml | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/commentaire.rb b/app/models/commentaire.rb index 1492c4bcb..7b3c651d9 100644 --- a/app/models/commentaire.rb +++ b/app/models/commentaire.rb @@ -17,6 +17,14 @@ class Commentaire < ActiveRecord::Base "#{email}, " + I18n.l(created_at.localtime, format: '%d %b %Y %H:%M') end + def file_url + if Features.remote_storage + RemoteDownloader.new(file.path).url + else + file.url + end + end + private def notify diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index 691549ecb..f1443fe56 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -19,7 +19,7 @@ - if commentaire.file.present? .attachment-link - = link_to commentaire.file.url, class: "button", target: "_blank", title: "Télécharger" do + = link_to commentaire.file_url, class: "button", target: "_blank", title: "Télécharger" do .icon.attachment = commentaire.file_identifier - elsif commentaire.piece_justificative From 886d278d77be6dee879c5bcec97d23bc49027e6a Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 28 Nov 2017 11:37:42 +0100 Subject: [PATCH 12/15] New UI : Move messagerie template in order to share them later --- .../dossiers/messagerie.html.haml | 34 ++----------------- .../commentaires/_commentaire.html.haml | 21 ++++++++++++ .../commentaires}/_commentaire_icon.html.haml | 0 .../_commentaire_issuer.html.haml | 0 .../shared/commentaires/_form.html.haml | 11 ++++++ 5 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml rename app/views/new_gestionnaire/{dossiers => shared/commentaires}/_commentaire_icon.html.haml (100%) rename app/views/new_gestionnaire/{dossiers => shared/commentaires}/_commentaire_issuer.html.haml (100%) create mode 100644 app/views/new_gestionnaire/shared/commentaires/_form.html.haml diff --git a/app/views/new_gestionnaire/dossiers/messagerie.html.haml b/app/views/new_gestionnaire/dossiers/messagerie.html.haml index f1443fe56..b7423effc 100644 --- a/app/views/new_gestionnaire/dossiers/messagerie.html.haml +++ b/app/views/new_gestionnaire/dossiers/messagerie.html.haml @@ -6,36 +6,6 @@ %ul.messages-list - @dossier.commentaires.each do |commentaire| %li - = render partial: 'commentaire_icon', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } + = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire } - .width-100 - %h2 - %span.mail - = render partial: 'commentaire_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } - - if ![current_gestionnaire.email, @dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) - %span.guest Invité - %span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y') - %p.rich-text= sanitize(commentaire.body) - - - if commentaire.file.present? - .attachment-link - = link_to commentaire.file_url, class: "button", target: "_blank", title: "Télécharger" do - .icon.attachment - = commentaire.file_identifier - - elsif commentaire.piece_justificative - .attachment-link - = link_to commentaire.piece_justificative.content_url, class: "button", target: "_blank", title: "Télécharger" do - .icon.attachment - = commentaire.piece_justificative.original_filename - - = form_for(@commentaire, url: commentaire_dossier_path(@dossier.procedure, @dossier), html: { class: 'form' }) do |f| - = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true - .flex.justify-between - %div - = f.file_field :file, id: :file, accept: @commentaire.file.accept_extension_list - %label{ for: :file } - .notice - (taille max : 20 Mo) - - .send-wrapper - = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi…" } + = render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_dossier_path(@dossier.procedure, @dossier) } diff --git a/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml b/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml new file mode 100644 index 000000000..6d8df6367 --- /dev/null +++ b/app/views/new_gestionnaire/shared/commentaires/_commentaire.html.haml @@ -0,0 +1,21 @@ += render partial: 'new_gestionnaire/shared/commentaires/commentaire_icon', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } + +.width-100 + %h2 + %span.mail + = render partial: 'new_gestionnaire/shared/commentaires/commentaire_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } + - if ![current_gestionnaire.email, @dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) + %span.guest Invité + %span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y') + %p.rich-text= sanitize(commentaire.body) + + - if commentaire.file.present? + .attachment-link + = link_to commentaire.file_url, class: "button", target: "_blank", title: "Télécharger" do + .icon.attachment + = commentaire.file_identifier + - elsif commentaire.piece_justificative + .attachment-link + = link_to commentaire.piece_justificative.content_url, class: "button", target: "_blank", title: "Télécharger" do + .icon.attachment + = commentaire.piece_justificative.original_filename diff --git a/app/views/new_gestionnaire/dossiers/_commentaire_icon.html.haml b/app/views/new_gestionnaire/shared/commentaires/_commentaire_icon.html.haml similarity index 100% rename from app/views/new_gestionnaire/dossiers/_commentaire_icon.html.haml rename to app/views/new_gestionnaire/shared/commentaires/_commentaire_icon.html.haml diff --git a/app/views/new_gestionnaire/dossiers/_commentaire_issuer.html.haml b/app/views/new_gestionnaire/shared/commentaires/_commentaire_issuer.html.haml similarity index 100% rename from app/views/new_gestionnaire/dossiers/_commentaire_issuer.html.haml rename to app/views/new_gestionnaire/shared/commentaires/_commentaire_issuer.html.haml diff --git a/app/views/new_gestionnaire/shared/commentaires/_form.html.haml b/app/views/new_gestionnaire/shared/commentaires/_form.html.haml new file mode 100644 index 000000000..977aca4c7 --- /dev/null +++ b/app/views/new_gestionnaire/shared/commentaires/_form.html.haml @@ -0,0 +1,11 @@ += form_for(commentaire, url: form_url, html: { class: 'form' }) do |f| + = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true + .flex.justify-between + %div + = f.file_field :file, id: :file, accept: commentaire.file.accept_extension_list + %label{ for: :file } + .notice + (taille max : 20 Mo) + + .send-wrapper + = f.submit 'Envoyer', class: 'button send', data: { disable_with: "Envoi…" } From eae16f7746603e03d12c2fe9e803be17bc6575f4 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 7 Nov 2017 17:53:11 +0100 Subject: [PATCH 13/15] New UI : Regular messagerie and avis messagerie should use same partials --- .../new_gestionnaire/avis_controller.rb | 27 ++++++++++------ .../avis/messagerie.html.haml | 18 ++--------- .../new_gestionnaire/avis_controller_spec.rb | 32 +++++++++++++++++-- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/app/controllers/new_gestionnaire/avis_controller.rb b/app/controllers/new_gestionnaire/avis_controller.rb index 03dc94fec..108c5630e 100644 --- a/app/controllers/new_gestionnaire/avis_controller.rb +++ b/app/controllers/new_gestionnaire/avis_controller.rb @@ -2,6 +2,8 @@ module NewGestionnaire class AvisController < ApplicationController layout 'new_application' + before_action :set_avis_and_dossier, only: [:show, :instruction, :messagerie, :create_commentaire] + A_DONNER_STATUS = 'a-donner' DONNES_STATUS = 'donnes' @@ -23,13 +25,9 @@ module NewGestionnaire end def show - @avis = avis - @dossier = avis.dossier end def instruction - @avis = avis - @dossier = avis.dossier end def update @@ -39,13 +37,19 @@ module NewGestionnaire end def messagerie - @avis = avis - @dossier = avis.dossier + @commentaire = Commentaire.new end def create_commentaire - Commentaire.create(commentaire_params.merge(email: current_gestionnaire.email, dossier: avis.dossier)) - redirect_to messagerie_avis_path(avis) + @commentaire = Commentaire.new(commentaire_params.merge(email: current_gestionnaire.email, dossier: avis.dossier)) + + if @commentaire.save + flash.notice = "Message envoyé" + redirect_to messagerie_avis_path(avis) + else + flash.alert = @commentaire.errors.full_messages + render :messagerie + end end def create_avis @@ -56,6 +60,11 @@ module NewGestionnaire private + def set_avis_and_dossier + @avis = avis + @dossier = avis.dossier + end + def avis current_gestionnaire.avis.includes(dossier: [:avis, :commentaires]).find(params[:id]) end @@ -65,7 +74,7 @@ module NewGestionnaire end def commentaire_params - params.require(:commentaire).permit(:body) + params.require(:commentaire).permit(:body, :file) end def create_avis_params diff --git a/app/views/new_gestionnaire/avis/messagerie.html.haml b/app/views/new_gestionnaire/avis/messagerie.html.haml index 56007bf23..cf022f20e 100644 --- a/app/views/new_gestionnaire/avis/messagerie.html.haml +++ b/app/views/new_gestionnaire/avis/messagerie.html.haml @@ -3,21 +3,9 @@ = render partial: 'header', locals: { avis: @avis, dossier: @dossier } .messagerie.container - %ul + %ul.messages-list - @dossier.commentaires.each do |commentaire| %li - = render partial: 'new_gestionnaire/dossiers/commentaire_icon', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } + = render partial: "new_gestionnaire/shared/commentaires/commentaire", locals: { commentaire: commentaire } - .width-100 - %h2 - %span.mail - = render partial: 'new_gestionnaire/dossiers/commentaire_issuer', locals: { commentaire: commentaire, current_gestionnaire: current_gestionnaire } - - if ![current_gestionnaire.email, @dossier.user.email, 'contact@tps.apientreprise.fr'].include?(commentaire.email) - %span.guest Invité - %span.date= I18n.l(commentaire.created_at.localtime, format: '%H:%M le %d/%m/%Y') - %p= sanitize(commentaire.body) - - = form_for(Commentaire.new, url: commentaire_avis_path(@avis), html: { class: 'form' }) do |f| - = f.text_area :body, rows: 5, placeholder: 'Répondre ici', required: true - .send-wrapper - = f.submit 'Envoyer', class: 'button send' + = render partial: "new_gestionnaire/shared/commentaires/form", locals: { commentaire: @commentaire, form_url: commentaire_avis_path(@avis) } diff --git a/spec/controllers/new_gestionnaire/avis_controller_spec.rb b/spec/controllers/new_gestionnaire/avis_controller_spec.rb index 1a8b27a6a..71fc195e8 100644 --- a/spec/controllers/new_gestionnaire/avis_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/avis_controller_spec.rb @@ -63,12 +63,38 @@ describe NewGestionnaire::AvisController, type: :controller do end describe '#create_commentaire' do + let(:file) { nil } + let(:scan_result) { true } + + subject { post :create_commentaire, { id: avis_without_answer.id, commentaire: { body: 'commentaire body', file: file } } } + before do - post :create_commentaire, { id: avis_without_answer.id, commentaire: { body: 'commentaire body' } } + allow(ClamavService).to receive(:safe_file?).and_return(scan_result) end - it { expect(response).to redirect_to(messagerie_avis_path(avis_without_answer)) } - it { expect(dossier.commentaires.map(&:body)).to match(['commentaire body']) } + it do + subject + + expect(response).to redirect_to(messagerie_avis_path(avis_without_answer)) + expect(dossier.commentaires.map(&:body)).to match(['commentaire body']) + end + + context "with a file" do + let(:file) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } + + it do + subject + expect(Commentaire.last.file.path).to include("piece_justificative_0.pdf") + end + + it { expect { subject }.to change(Commentaire, :count).by(1) } + + context "and a virus" do + let(:scan_result) { false } + + it { expect { subject }.not_to change(Commentaire, :count) } + end + end end describe '#create_avis' do From fa95c312ff26ba360a39b222463c30c1fb36e934 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 31 Oct 2017 17:45:04 +0100 Subject: [PATCH 14/15] Old UI : Plug messagerie on new commentaire file system --- app/controllers/commentaires_controller.rb | 16 +++--------- .../commentaires/_commentaire.html.haml | 8 +++++- .../dossiers/commentaires/_form.html.haml | 2 +- .../commentaires_controller_spec.rb | 26 +++---------------- .../users/commentaires_controller_spec.rb | 26 +++---------------- 5 files changed, 18 insertions(+), 60 deletions(-) diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index fbb9532d3..75a11fa05 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -26,21 +26,13 @@ class CommentairesController < ApplicationController @commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email end - unless params[:piece_justificative].nil? - pj = PiecesJustificativesService.upload_one! @commentaire.dossier, current_user, params - - if pj.errors.empty? - @commentaire.piece_justificative = pj - else - flash.alert = pj.errors.full_messages - end - end + @commentaire.file = params["file"] @commentaire.body = params['texte_commentaire'] - unless @commentaire.body.blank? && @commentaire.piece_justificative.nil? - @commentaire.save unless flash.alert + if @commentaire.save + flash.notice = "Votre message a été envoyé" else - flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe." + flash.alert = "Veuillez rédiger un message ou ajouter une pièce jointe (maximum 20 Mo)" end if is_gestionnaire? diff --git a/app/views/dossiers/commentaires/_commentaire.html.haml b/app/views/dossiers/commentaires/_commentaire.html.haml index 7a94c0db6..3edff6be5 100644 --- a/app/views/dossiers/commentaires/_commentaire.html.haml +++ b/app/views/dossiers/commentaires/_commentaire.html.haml @@ -3,7 +3,13 @@ = commentaire.header .content = sanitize(commentaire.body) - - if file = commentaire.piece_justificative + - if commentaire.file.present? + .file + = link_to commentaire.file_url, class: 'link', target: '_blank' do + %span.fa.fa-file + %div + = commentaire.file_identifier + - elsif file = commentaire.piece_justificative .file = link_to file.content_url, class: 'link', target: '_blank' do %span.fa.fa-file diff --git a/app/views/dossiers/commentaires/_form.html.haml b/app/views/dossiers/commentaires/_form.html.haml index 8360e1ba1..f4011836a 100644 --- a/app/views/dossiers/commentaires/_form.html.haml +++ b/app/views/dossiers/commentaires/_form.html.haml @@ -5,6 +5,6 @@ .row .col-md-6 %h4.text-primary{ style: 'margin-top: 0px;' } Ajouter un fichier - = file_field_tag "piece_justificative[content]", accept: PieceJustificative.accept_format, style: 'float: left; margin-left: 20px;' + = file_field_tag "file", accept: Commentaire.new.file.accept_extension_list, style: 'float: left; margin-left: 20px;' .col-md-6.text-right = submit_tag 'Envoyer', id: 'save-message', class: 'form-control btn btn-danger', data: { disable_with: 'Envoi...' } diff --git a/spec/controllers/backoffice/commentaires_controller_spec.rb b/spec/controllers/backoffice/commentaires_controller_spec.rb index 27e3d5486..a556168eb 100644 --- a/spec/controllers/backoffice/commentaires_controller_spec.rb +++ b/spec/controllers/backoffice/commentaires_controller_spec.rb @@ -65,11 +65,7 @@ describe Backoffice::CommentairesController, type: :controller do let(:document_upload) { Rack::Test::UploadedFile.new("./spec/support/files/piece_justificative_0.pdf", 'application/pdf') } subject do - post :create, params: {dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire, piece_justificative: {content: document_upload}} - end - - it 'create a new piece justificative' do - expect { subject }.to change(PieceJustificative, :count).by(1) + post :create, params: { dossier_id: dossier_id, email_commentaire: email_commentaire, texte_commentaire: texte_commentaire, file: document_upload } end it 'clamav check the pj' do @@ -81,22 +77,6 @@ describe Backoffice::CommentairesController, type: :controller do expect { subject }.to change(Notification, :count).by (1) end - describe 'piece justificative created' do - let(:pj) { PieceJustificative.last } - - before do - subject - end - - it 'not have a type de pj' do - expect(pj.type_de_piece_justificative).to be_nil - end - - it 'content not be nil' do - expect(pj.content).not_to be_nil - end - end - describe 'commentaire created' do let(:commentaire) { Commentaire.last } @@ -105,8 +85,8 @@ describe Backoffice::CommentairesController, type: :controller do end it 'have a piece justificative reference' do - expect(commentaire.piece_justificative).not_to be_nil - expect(commentaire.piece_justificative).to eq PieceJustificative.last + expect(commentaire.file.present?).to eq true + expect(commentaire.file.class).to eq(CommentaireFileUploader) end end end diff --git a/spec/controllers/users/commentaires_controller_spec.rb b/spec/controllers/users/commentaires_controller_spec.rb index 1b05ff5ac..e096bb547 100644 --- a/spec/controllers/users/commentaires_controller_spec.rb +++ b/spec/controllers/users/commentaires_controller_spec.rb @@ -61,11 +61,7 @@ describe Users::CommentairesController, type: :controller do subject do sign_in dossier.user - post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire, piece_justificative: { content: document_upload } } - end - - it 'create a new piece justificative' do - expect { subject }.to change(PieceJustificative, :count).by(1) + post :create, params: { dossier_id: dossier_id, texte_commentaire: texte_commentaire, file: document_upload } end it 'clamav check the pj' do @@ -73,22 +69,6 @@ describe Users::CommentairesController, type: :controller do subject end - describe 'piece justificative created' do - let(:pj) { PieceJustificative.last } - - before do - subject - end - - it 'not have a type de pj' do - expect(pj.type_de_piece_justificative).to be_nil - end - - it 'content not be nil' do - expect(pj.content).not_to be_nil - end - end - describe 'commentaire created' do let(:commentaire) { Commentaire.last } @@ -97,8 +77,8 @@ describe Users::CommentairesController, type: :controller do end it 'have a piece justificative reference' do - expect(commentaire.piece_justificative).not_to be_nil - expect(commentaire.piece_justificative).to eq PieceJustificative.last + expect(commentaire.file.present?).to be true + expect(commentaire.file.class).to eq CommentaireFileUploader end end end From 2db1d93a84b1ab95cbfc446d743eabbb24037e3d Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Wed, 8 Nov 2017 18:44:18 +0100 Subject: [PATCH 15/15] Remove this test because no Notification should be created when gestionnaire is sending a file on messagerie --- spec/controllers/backoffice/commentaires_controller_spec.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spec/controllers/backoffice/commentaires_controller_spec.rb b/spec/controllers/backoffice/commentaires_controller_spec.rb index a556168eb..284e8c2e9 100644 --- a/spec/controllers/backoffice/commentaires_controller_spec.rb +++ b/spec/controllers/backoffice/commentaires_controller_spec.rb @@ -73,10 +73,6 @@ describe Backoffice::CommentairesController, type: :controller do subject end - it 'Internal notification is created' do - expect { subject }.to change(Notification, :count).by (1) - end - describe 'commentaire created' do let(:commentaire) { Commentaire.last }