Add a piece_justificative_file attribute to Champ

This commit is contained in:
gregoirenovel 2018-01-30 19:17:16 +01:00
parent 7097cad51a
commit ee145180a3
9 changed files with 106 additions and 5 deletions

View file

@ -165,6 +165,7 @@ module NewGestionnaire
def update_annotations def update_annotations
dossier = current_gestionnaire.dossiers.includes(champs_private: :type_de_champ).find(params[:dossier_id]) 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) dossier.update_attributes(champs_private_params)
redirect_to annotations_privees_dossier_path(procedure, dossier) redirect_to annotations_privees_dossier_path(procedure, dossier)
end end
@ -189,7 +190,7 @@ module NewGestionnaire
end end
def champs_private_params 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 end
def check_attestation_emailable def check_attestation_emailable

View file

@ -36,7 +36,7 @@ class Users::DescriptionController < UsersController
return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) if !cerfa.save return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) if !cerfa.save
end 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? return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any?
if params[:champs] && !(brouillon_submission? || brouillon_then_dashboard_submission?) if params[:champs] && !(brouillon_submission? || brouillon_then_dashboard_submission?)

View file

@ -4,6 +4,7 @@ class Champ < ActiveRecord::Base
belongs_to :dossier, touch: true belongs_to :dossier, touch: true
belongs_to :type_de_champ, inverse_of: :champ belongs_to :type_de_champ, inverse_of: :champ
has_many :commentaires has_many :commentaires
has_one_attached :piece_justificative_file
delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, to: :type_de_champ 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 :public_only, -> { where(type: 'ChampPublic').or(where(private: false)) }
scope :private_only, -> { where(type: 'ChampPrivate').or(where(private: true)) } 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? def public?
!private? !private?
end end
@ -32,7 +50,11 @@ class Champ < ActiveRecord::Base
end end
def mandatory_and_blank? def mandatory_and_blank?
mandatory? && value.blank? if type_champ == 'piece_justificative'
mandatory? && !piece_justificative_file.attached?
else
mandatory? && value.blank?
end
end end
def same_date? num, compare def same_date? num, compare
@ -88,6 +110,28 @@ class Champ < ActiveRecord::Base
end end
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 private
def format_date_to_iso def format_date_to_iso

View file

@ -11,12 +11,25 @@ class ChampsService
.map { |c| "Le champ #{c.libelle.truncate(200)} doit être rempli." } .map { |c| "Le champ #{c.libelle.truncate(200)} doit être rempli." }
end 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 private
def fill_champs(champs, h) def fill_champs(champs, h)
datetimes, not_datetimes = champs.partition { |c| c.type_champ == 'datetime' } 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) } datetimes.each { |c| c.value = parse_datetime(c.id, h) }
end end

View file

@ -37,7 +37,7 @@
.col-xs-1.comments-off .col-xs-1.comments-off
= "-" = "-"
.col-xs-5.depositaire-info{ id: "champ-#{champ.id}-value" } .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' - if champ.type_champ == 'dossier_link'
- dossier = Dossier.includes(:procedure).find_by(id: champ.decorate.value) - dossier = Dossier.includes(:procedure).find_by(id: champ.decorate.value)
- if dossier - if dossier
@ -46,6 +46,10 @@
= sanitize(dossier.text_summary) = sanitize(dossier.text_summary)
- else - else
Pas de dossier associé 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 - else
= sanitize(champ.decorate.value) = sanitize(champ.decorate.value)

View file

@ -30,6 +30,13 @@
= sanitize(dossier.text_summary) = sanitize(dossier.text_summary)
- else - else
Pas de dossier associé 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 - else
%th.libelle %th.libelle
= "#{c.libelle} :" = "#{c.libelle} :"

View file

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

View file

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

View file

@ -50,6 +50,9 @@
- when 'date' - when 'date'
= render partial: 'users/description/champs/date', locals: { champ: champ } = render partial: 'users/description/champs/date', locals: { champ: champ }
- when 'piece_justificative'
= render partial: 'users/description/champs/piece_justificative', locals: { champ: champ }
- else - else
%input.form-control{ name: "champs['#{champ.id}']", %input.form-control{ name: "champs['#{champ.id}']",
placeholder: champ.libelle, placeholder: champ.libelle,