Merge pull request #1427 from betagouv/pj

PJ
This commit is contained in:
gregoirenovel 2018-02-15 12:13:01 +01:00 committed by GitHub
commit 88e951f5fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 380 additions and 10 deletions

View file

@ -1,4 +1,7 @@
exclude: 'app/assets/stylesheets/new_design/reset.scss'
exclude:
- 'app/assets/stylesheets/new_design/reset.scss'
- 'app/assets/stylesheets/direct_uploads.scss'
- 'app/assets/stylesheets/new_design/direct_uploads.scss'
linters:
BangFormat:

View file

@ -10,6 +10,7 @@
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require activestorage
//= require jquery
//= require jquery_ujs
//= require turbolinks

View file

@ -11,6 +11,7 @@
// about supported directives.
//
//= require ./init
//= require activestorage
//= require jquery
//= require jquery_ujs
//= require turbolinks

View file

@ -0,0 +1,45 @@
addEventListener("direct-upload:initialize", function (event) {
var target = event.target,
detail = event.detail,
id = detail.id,
file = detail.file;
target.insertAdjacentHTML("beforebegin", "\n<div id=\"direct-upload-" +
id +
"\" class=\"direct-upload direct-upload--pending\">\n<div id=\"direct-upload-progress-" +
id + "\" class=\"direct-upload__progress\" style=\"width: 0%\"></div>\n<span class=\"direct-upload__filename\">" +
file.name +
"</span>\n</div>\n");
});
addEventListener("direct-upload:start", function (event) {
var id = event.detail.id,
element = document.getElementById("direct-upload-" + id);
element.classList.remove("direct-upload--pending");
});
addEventListener("direct-upload:progress", function (event) {
var id = event.detail.id,
progress = event.detail.progress,
progressElement = document.getElementById("direct-upload-progress-" + id);
progressElement.style.width = progress + "%";
});
addEventListener("direct-upload:error", function (event) {
event.preventDefault();
var id = event.detail.id,
error = event.detail.error,
element = document.getElementById("direct-upload-" + id);
element.classList.add("direct-upload--error");
element.setAttribute("title", error);
});
addEventListener("direct-upload:end", function (event) {
var id = event.detail.id,
element = document.getElementById("direct-upload-" + id);
element.classList.add("direct-upload--complete");
});

View file

@ -0,0 +1,45 @@
addEventListener("direct-upload:initialize", function (event) {
var target = event.target,
detail = event.detail,
id = detail.id,
file = detail.file;
target.insertAdjacentHTML("beforebegin", "\n<div id=\"direct-upload-" +
id +
"\" class=\"direct-upload direct-upload--pending\">\n<div id=\"direct-upload-progress-" +
id + "\" class=\"direct-upload__progress\" style=\"width: 0%\"></div>\n<span class=\"direct-upload__filename\">" +
file.name +
"</span>\n</div>\n");
});
addEventListener("direct-upload:start", function (event) {
var id = event.detail.id,
element = document.getElementById("direct-upload-" + id);
element.classList.remove("direct-upload--pending");
});
addEventListener("direct-upload:progress", function (event) {
var id = event.detail.id,
progress = event.detail.progress,
progressElement = document.getElementById("direct-upload-progress-" + id);
progressElement.style.width = progress + "%";
});
addEventListener("direct-upload:error", function (event) {
event.preventDefault();
var id = event.detail.id,
error = event.detail.error,
element = document.getElementById("direct-upload-" + id);
element.classList.add("direct-upload--error");
element.setAttribute("title", error);
});
addEventListener("direct-upload:end", function (event) {
var id = event.detail.id,
element = document.getElementById("direct-upload-" + id);
element.classList.add("direct-upload--complete");
});

View file

@ -21,6 +21,7 @@
// = require custom_mails
// = require default_data_block
// = require description
// = require direct_uploads
// = require dossier_show
// = require dossiers
// = require etapes

View file

@ -0,0 +1,37 @@
.direct-upload {
display: inline-block;
position: relative;
padding: 2px 4px;
margin: 0 3px 3px 0;
border: 1px solid rgba(0, 0, 0, 0.3);
border-radius: 3px;
font-size: 11px;
line-height: 13px;
}
.direct-upload--pending {
opacity: 0.6;
}
.direct-upload__progress {
position: absolute;
top: 0;
left: 0;
bottom: 0;
opacity: 0.2;
background: #0076ff;
transition: width 120ms ease-out, opacity 60ms 60ms ease-in;
transform: translate3d(0, 0, 0);
}
.direct-upload--complete .direct-upload__progress {
opacity: 0.4;
}
.direct-upload--error {
border-color: red;
}
input[type=file][data-direct-upload-url][disabled] {
display: none;
}

View file

@ -0,0 +1,37 @@
.direct-upload {
display: inline-block;
position: relative;
padding: 2px 4px;
margin: 0 3px 3px 0;
border: 1px solid rgba(0, 0, 0, 0.3);
border-radius: 3px;
font-size: 11px;
line-height: 13px;
}
.direct-upload--pending {
opacity: 0.6;
}
.direct-upload__progress {
position: absolute;
top: 0;
left: 0;
bottom: 0;
opacity: 0.2;
background: #0076ff;
transition: width 120ms ease-out, opacity 60ms 60ms ease-in;
transform: translate3d(0, 0, 0);
}
.direct-upload--complete .direct-upload__progress {
opacity: 0.4;
}
.direct-upload--error {
border-color: red;
}
input[type=file][data-direct-upload-url][disabled] {
display: none;
}

View file

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

View file

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

View file

@ -0,0 +1,11 @@
module TypeDeChampHelper
def tdc_options(current_administrateur)
tdcs = TypeDeChamp.type_de_champs_list_fr
if !current_administrateur.id.in?(Features.champ_pj_allowed_for_admin_ids)
tdcs.reject! { |tdc| tdc.last == "piece_justificative" }
end
tdcs
end
end

View file

@ -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,8 +50,12 @@ class Champ < ActiveRecord::Base
end
def mandatory_and_blank?
if type_champ == 'piece_justificative'
mandatory? && !piece_justificative_file.attached?
else
mandatory? && value.blank?
end
end
def same_date? num, compare
if type_champ == 'datetime' && value.present?
@ -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

View file

@ -21,7 +21,8 @@ class TypeDeChamp < ActiveRecord::Base
engagement: 'engagement',
header_section: 'header_section',
explication: 'explication',
dossier_link: 'dossier_link'
dossier_link: 'dossier_link',
piece_justificative: 'piece_justificative'
}
belongs_to :procedure

View file

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

View file

@ -8,7 +8,8 @@
.form-group.type
%h4 Type
= ff.select :type_champ, TypeDeChamp.type_de_champs_list_fr, {}, { class: 'form-control type-champ' }
- tdc_options = tdc_options(current_administrateur)
= ff.select :type_champ, tdc_options, {}, { class: 'form-control type-champ' }
.form-group.description
%h4 Description

View file

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

View file

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

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'
= 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,

View file

@ -56,6 +56,7 @@ set :shared_paths, [
'config/database.yml',
"config/skylight.yml",
"config/fog_credentials.yml",
'config/storage.yml',
'config/initializers/secret_token.rb',
'config/initializers/features.yml',
"config/environments/#{rails_env}.rb",

View file

@ -19,6 +19,8 @@ Rails.application.configure do
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.active_storage.service = :local
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

View file

@ -68,6 +68,8 @@ Rails.application.configure do
# the I18n.default_locale when a translation cannot be found).
config.i18n.fallbacks = true
config.active_storage.service = :clever_cloud
# Send deprecation notices to registered listeners.
config.active_support.deprecation = :notify

View file

@ -11,9 +11,9 @@ class Features
if File.exist?("#{File.dirname(__FILE__)}/features.yml")
features_map = YAML.load_file("#{File.dirname(__FILE__)}/features.yml")
if features_map
features_map.each do |feature, is_active|
features_map.each do |feature, value|
define_method("#{feature}") do
is_active
value
end
end
end

View file

@ -1,2 +1,4 @@
remote_storage: false
weekly_overview: false
champ_pj_allowed_for_admin_ids:
- 0

View file

@ -25,3 +25,4 @@ fr:
explication: 'Explication'
multiple_drop_down_list: 'Menu déroulant à choix multiples'
dossier_link: 'Lien vers un autre dossier'
piece_justificative: 'Pièce justificative'

3
config/storage.yml Normal file
View file

@ -0,0 +1,3 @@
local:
service: Disk
root: <%= Rails.root.join("storage") %>

View file

@ -0,0 +1,26 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false
t.index [ :key ], unique: true
end
create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false
t.datetime :created_at, null: false
t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end
end
end

View file

@ -16,6 +16,27 @@ ActiveRecord::Schema.define(version: 2018_02_09_133452) do
enable_extension "plpgsql"
enable_extension "unaccent"
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table "administrateurs", id: :serial, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false

View file

@ -0,0 +1,22 @@
require 'rails_helper'
RSpec.describe TypeDeChampHelper, type: :helper do
describe ".tdc_options" do
let(:current_administrateur) { create(:administrateur) }
let(:pj_option) { ["Pièce justificative", "piece_justificative"] }
subject { tdc_options(current_administrateur) }
context "when the champ_pj_allowed_for_admin_id matches the current_administrateur's id" do
before { allow(Features).to receive(:champ_pj_allowed_for_admin_ids).and_return([current_administrateur.id]) }
it { is_expected.to include(pj_option) }
end
context "when the champ_pj_allowed_for_admin_id does not match the current_administrateur's id" do
before { allow(Features).to receive(:champ_pj_allowed_for_admin_ids).and_return([1000]) }
it { is_expected.not_to include(pj_option) }
end
end
end

View file

@ -3,6 +3,9 @@ require 'spec_helper'
describe 'admin/types_de_champ/show.html.haml', type: :view do
let(:procedure) { create(:procedure) }
# FIXME: delete this when support for pj champ is generalized
before { allow(view).to receive(:current_administrateur).and_return(create(:administrateur)) }
describe 'fields sorted' do
let(:first_libelle) { 'salut la compagnie' }
let(:last_libelle) { 'je suis bien sur la page' }

View file

@ -3,6 +3,9 @@ require 'spec_helper'
describe 'admin/types_de_champ/show.html.haml', type: :view do
let(:procedure) { create(:procedure) }
# FIXME: delete this when support for pj champ is generalized
before { allow(view).to receive(:current_administrateur).and_return(create(:administrateur)) }
describe 'fields sorted' do
let(:first_libelle) { 'salut la compagnie' }
let(:last_libelle) { 'je suis bien sur la page' }