From ee145180a3863646b3b45ead454104c7212d6824 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Tue, 30 Jan 2018 19:17:16 +0100 Subject: [PATCH] Add a piece_justificative_file attribute to Champ --- .../new_gestionnaire/dossiers_controller.rb | 3 +- .../users/description_controller.rb | 2 +- app/models/champ.rb | 46 ++++++++++++++++++- app/services/champs_service.rb | 15 +++++- app/views/dossiers/_infos_dossier.html.haml | 6 ++- .../dossiers/_champs.html.haml | 7 +++ .../_piece_justificative.html.haml | 14 ++++++ .../champs/_piece_justificative.html.haml | 15 ++++++ .../champs/_render_list_champs.html.haml | 3 ++ 9 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 app/views/new_gestionnaire/dossiers/editable_champs/_piece_justificative.html.haml create mode 100644 app/views/users/description/champs/_piece_justificative.html.haml diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 58eea7a09..d5e9c1106 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -165,6 +165,7 @@ module NewGestionnaire def update_annotations dossier = current_gestionnaire.dossiers.includes(champs_private: :type_de_champ).find(params[:dossier_id]) + # FIXME: add attachements validation, cf. Champ#piece_justificative_file_errors dossier.update_attributes(champs_private_params) redirect_to annotations_privees_dossier_path(procedure, dossier) end @@ -189,7 +190,7 @@ module NewGestionnaire end def champs_private_params - params.require(:dossier).permit(champs_private_attributes: [:id, :value, value: []]) + params.require(:dossier).permit(champs_private_attributes: [:id, :piece_justificative_file, :value, value: []]) end def check_attestation_emailable diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index e991408d7..f51305b31 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -36,7 +36,7 @@ class Users::DescriptionController < UsersController return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) if !cerfa.save end - errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params) + errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params) + ChampsService.check_piece_justificative_files(dossier.champs) return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any? if params[:champs] && !(brouillon_submission? || brouillon_then_dashboard_submission?) diff --git a/app/models/champ.rb b/app/models/champ.rb index 4ff77ccd5..a67c4e581 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -4,6 +4,7 @@ class Champ < ActiveRecord::Base belongs_to :dossier, touch: true belongs_to :type_de_champ, inverse_of: :champ has_many :commentaires + has_one_attached :piece_justificative_file delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, to: :type_de_champ @@ -15,6 +16,23 @@ class Champ < ActiveRecord::Base scope :public_only, -> { where(type: 'ChampPublic').or(where(private: false)) } scope :private_only, -> { where(type: 'ChampPrivate').or(where(private: true)) } + PIECE_JUSTIFICATIVE_FILE_MAX_SIZE = 200.megabytes + + PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS = [ + "application/pdf", + "application/msword", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "application/vnd.ms-excel", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "application/vnd.ms-powerpoint", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "application/vnd.oasis.opendocument.text", + "application/vnd.oasis.opendocument.presentation", + "application/vnd.oasis.opendocument.spreadsheet", + "image/png", + "image/jpeg" + ] + def public? !private? end @@ -32,7 +50,11 @@ class Champ < ActiveRecord::Base end def mandatory_and_blank? - mandatory? && value.blank? + if type_champ == 'piece_justificative' + mandatory? && !piece_justificative_file.attached? + else + mandatory? && value.blank? + end end def same_date? num, compare @@ -88,6 +110,28 @@ class Champ < ActiveRecord::Base end end + def piece_justificative_file_errors + errors = [] + + if piece_justificative_file.attached? && piece_justificative_file.previous_changes.present? + if piece_justificative_file.blob.byte_size > PIECE_JUSTIFICATIVE_FILE_MAX_SIZE + errors << "Le fichier #{piece_justificative_file.filename.to_s} est trop lourd, il doit faire au plus #{PIECE_JUSTIFICATIVE_FILE_MAX_SIZE.to_s(:human_size, precision: 2)}" + end + + if !piece_justificative_file.blob.content_type.in?(PIECE_JUSTIFICATIVE_FILE_ACCEPTED_FORMATS) + errors << "Le fichier #{piece_justificative_file.filename.to_s} est dans un format que nous n'acceptons pas" + end + + # FIXME: add Clamav check + end + + if errors.present? + piece_justificative_file.purge + end + + errors + end + private def format_date_to_iso diff --git a/app/services/champs_service.rb b/app/services/champs_service.rb index 62a1d7342..0374b1b4a 100644 --- a/app/services/champs_service.rb +++ b/app/services/champs_service.rb @@ -11,12 +11,25 @@ class ChampsService .map { |c| "Le champ #{c.libelle.truncate(200)} doit être rempli." } end + def check_piece_justificative_files(champs) + champs.select do |champ| + champ.type_champ == 'piece_justificative' + end.map { |c| c.piece_justificative_file_errors }.flatten + end + private def fill_champs(champs, h) datetimes, not_datetimes = champs.partition { |c| c.type_champ == 'datetime' } - not_datetimes.each { |c| c.value = h[:champs]["'#{c.id}'"] } + not_datetimes.each do |c| + if c.type_champ == 'piece_justificative' && h["champs"]["'#{c.id}'"].present? + c.piece_justificative_file.attach(h["champs"]["'#{c.id}'"]) + else + c.value = h[:champs]["'#{c.id}'"] + end + end + datetimes.each { |c| c.value = parse_datetime(c.id, h) } end diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index f1c09ddf9..de2ee2545 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -37,7 +37,7 @@ .col-xs-1.comments-off = "-" .col-xs-5.depositaire-info{ id: "champ-#{champ.id}-value" } - - if champ.decorate.value.present? + - if champ.decorate.value.present? || champ.piece_justificative_file.attached? - if champ.type_champ == 'dossier_link' - dossier = Dossier.includes(:procedure).find_by(id: champ.decorate.value) - if dossier @@ -46,6 +46,10 @@ = sanitize(dossier.text_summary) - else Pas de dossier associé + - elsif champ.type_champ == 'piece_justificative' + - pj = champ.piece_justificative_file + %a{ href: url_for(pj), target: '_blank' } + = pj.filename.to_s - else = sanitize(champ.decorate.value) diff --git a/app/views/new_gestionnaire/dossiers/_champs.html.haml b/app/views/new_gestionnaire/dossiers/_champs.html.haml index 45609eaa2..9e44d4e7a 100644 --- a/app/views/new_gestionnaire/dossiers/_champs.html.haml +++ b/app/views/new_gestionnaire/dossiers/_champs.html.haml @@ -30,6 +30,13 @@ = sanitize(dossier.text_summary) - else Pas de dossier associé + - when "piece_justificative" + %th.libelle + = "#{c.libelle} :" + %td.rich-text + - pj = c.piece_justificative_file + %a{ href: url_for(pj), target: '_blank' } + = pj.filename.to_s - else %th.libelle = "#{c.libelle} :" diff --git a/app/views/new_gestionnaire/dossiers/editable_champs/_piece_justificative.html.haml b/app/views/new_gestionnaire/dossiers/editable_champs/_piece_justificative.html.haml new file mode 100644 index 000000000..fe59efd3f --- /dev/null +++ b/app/views/new_gestionnaire/dossiers/editable_champs/_piece_justificative.html.haml @@ -0,0 +1,14 @@ +- pj = champ.piece_justificative_file + +- if !pj.attached? + = form.file_field :piece_justificative_file, + id: "champs_#{champ.id}", + direct_upload: true +- else + %a{ href: url_for(pj), target: '_blank' } + = pj.filename.to_s + %br + Modifier : + = form.file_field :piece_justificative_file, + id: "champs_#{champ.id}", + direct_upload: true diff --git a/app/views/users/description/champs/_piece_justificative.html.haml b/app/views/users/description/champs/_piece_justificative.html.haml new file mode 100644 index 000000000..b8c8e872d --- /dev/null +++ b/app/views/users/description/champs/_piece_justificative.html.haml @@ -0,0 +1,15 @@ +- pj = champ.piece_justificative_file + +- if !pj.attached? + = file_field_tag "champs['#{champ.id}']", + id: "champs_#{champ.id}", + direct_upload: true, + mandatory: champ.mandatory +- else + %a{ href: url_for(pj), target: '_blank' } + = pj.filename.to_s + %br + Modifier : + = file_field_tag "champs['#{champ.id}']", + id: "champs_#{champ.id}", + direct_upload: true diff --git a/app/views/users/description/champs/_render_list_champs.html.haml b/app/views/users/description/champs/_render_list_champs.html.haml index 8115c6cac..e7f5dab2a 100644 --- a/app/views/users/description/champs/_render_list_champs.html.haml +++ b/app/views/users/description/champs/_render_list_champs.html.haml @@ -50,6 +50,9 @@ - when 'date' = render partial: 'users/description/champs/date', locals: { champ: champ } + - when 'piece_justificative' + = render partial: 'users/description/champs/piece_justificative', locals: { champ: champ } + - else %input.form-control{ name: "champs['#{champ.id}']", placeholder: champ.libelle,