Merge pull request #937 from sgmap/upload_file_with_comment

Upload file with comment on new UI
This commit is contained in:
Mathieu Magnin 2017-11-28 15:17:27 +01:00 committed by GitHub
commit 4302997f08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 241 additions and 130 deletions

View file

@ -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'

View file

@ -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

View file

@ -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?

View file

@ -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))
@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

View file

@ -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))
@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,11 +98,11 @@ module NewGestionnaire
private
def dossier
current_gestionnaire.dossiers.find(params[:dossier_id])
@dossier ||= current_gestionnaire.dossiers.find(params[:dossier_id])
end
def commentaire_params
params.require(:commentaire).permit(:body)
params.require(:commentaire).permit(:body, :file)
end
def avis_params

View file

@ -1,9 +1,13 @@
class Commentaire < ActiveRecord::Base
belongs_to :dossier, touch: true
belongs_to :champ
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?
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) }
@ -13,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
@ -43,4 +55,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

View file

@ -0,0 +1,23 @@
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
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

View file

@ -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

View file

@ -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...' }

View file

@ -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) }

View file

@ -6,24 +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')
.rich-text
= sanitize(commentaire.body)
- if file = commentaire.piece_justificative
.attachment-link
= link_to file.content_url, class: "button", target: "_blank", title: "Télécharger" do
.icon.attachment
= file.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
.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) }

View file

@ -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

View file

@ -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…" }

View file

@ -0,0 +1,5 @@
fr:
activerecord:
attributes:
commentaire:
file: fichier

View file

@ -0,0 +1,5 @@
class AddFileToCommentaires < ActiveRecord::Migration[5.0]
def change
add_column :commentaires, :file, :string
end
end

View file

@ -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

View file

@ -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

View file

@ -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
@ -77,26 +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 '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 +81,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

View file

@ -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

View file

@ -111,20 +111,56 @@ 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 }
before do
subject {
post :create_commentaire, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
commentaire: { body: 'body' }
commentaire: {
body: body,
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 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
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') }
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
describe "#create_avis" do

View file

@ -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

View file

@ -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