Add CSV output for Dossier API

This commit is contained in:
Guillaume Lazzara 2016-02-19 16:59:18 +01:00
parent b58f88d9bf
commit bd5a39ff17
6 changed files with 59 additions and 2 deletions

View file

@ -80,6 +80,8 @@ gem 'bootstrap-wysihtml5-rails', '~> 0.3.3.8'
gem 'swagger-docs' gem 'swagger-docs'
gem 'grape-swagger-rails' gem 'grape-swagger-rails'
gem 'as_csv'
group :test do group :test do
gem 'capybara' gem 'capybara'
gem 'factory_girl' gem 'factory_girl'

View file

@ -48,6 +48,10 @@ GEM
tzinfo (~> 1.1) tzinfo (~> 1.1)
addressable (2.3.8) addressable (2.3.8)
arel (6.0.2) arel (6.0.2)
as_csv (2.0.2)
actionpack (>= 3.0)
activemodel (>= 3.0)
responders
ast (2.0.0) ast (2.0.0)
astrolabe (1.3.0) astrolabe (1.3.0)
parser (>= 2.2.0.pre.3, < 3.0) parser (>= 2.2.0.pre.3, < 3.0)
@ -491,6 +495,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
active_model_serializers active_model_serializers
as_csv
bootstrap-datepicker-rails bootstrap-datepicker-rails
bootstrap-sass (~> 3.3.5) bootstrap-sass (~> 3.3.5)
bootstrap-wysihtml5-rails (~> 0.3.3.8) bootstrap-wysihtml5-rails (~> 0.3.3.8)

View file

@ -1,5 +1,4 @@
class API::V1::DossiersController < APIController class API::V1::DossiersController < APIController
swagger_controller :dossiers, "Dossiers" swagger_controller :dossiers, "Dossiers"
swagger_api :index do swagger_api :index do
@ -32,7 +31,10 @@ class API::V1::DossiersController < APIController
def show def show
procedure = current_administrateur.procedures.find(params[:procedure_id]) procedure = current_administrateur.procedures.find(params[:procedure_id])
dossier = procedure.dossiers.find(params[:id]) dossier = procedure.dossiers.find(params[:id])
render json: dossier, status: 200 respond_to do |format|
format.json { render json: dossier, status: 200 }
format.csv { render csv: dossier, status: 200 }
end
rescue ActiveRecord::RecordNotFound => e rescue ActiveRecord::RecordNotFound => e
render json: {}, status: 404 render json: {}, status: 404
end end

View file

@ -1,5 +1,6 @@
class APIController < ApplicationController class APIController < ApplicationController
before_action :authenticate_user before_action :authenticate_user
before_filter :default_format_json
def authenticate_user def authenticate_user
render json: {}, status: 401 unless valid_token? render json: {}, status: 401 unless valid_token?
@ -14,4 +15,8 @@ class APIController < ApplicationController
def current_administrateur def current_administrateur
@administrateur ||= Administrateur.find_by_api_token(params[:token]) @administrateur ||= Administrateur.find_by_api_token(params[:token])
end end
def default_format_json
request.format = "json" unless request.accept
end
end end

View file

@ -1,4 +1,5 @@
class Dossier < ActiveRecord::Base class Dossier < ActiveRecord::Base
enum state: {draft: 'draft', enum state: {draft: 'draft',
initiated: 'initiated', initiated: 'initiated',
replied: 'replied', replied: 'replied',
@ -192,6 +193,13 @@ class Dossier < ActiveRecord::Base
procedure.cerfa_flag? && !cerfa.empty? procedure.cerfa_flag? && !cerfa.empty?
end end
def as_csv(options={})
dossier_attr = DossierSerializer.new(self).attributes
etablissement_attr = EtablissementSerializer.new(self.etablissement).attributes.map {|k, v| ["etablissement.#{k}", v] }.to_h
entreprise_attr = EntrepriseSerializer.new(self.entreprise).attributes.map {|k, v| ["entreprise.#{k}", v] }.to_h
dossier_attr.merge(etablissement_attr).merge(entreprise_attr)
end
private private
def build_default_cerfa def build_default_cerfa

View file

@ -543,4 +543,39 @@ describe Dossier do
it { expect(dossier.cerfa_available?).to be_falsey } it { expect(dossier.cerfa_available?).to be_falsey }
end end
end end
describe '#as_csv?' do
let(:procedure) { create(:procedure) }
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) }
subject { dossier.as_csv }
it { expect(subject[:nom_projet]).to eq("Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger") }
it { expect(subject[:description]).to eq("Ma super description") }
it { expect(subject[:archived]).to be_falsey }
it { expect(subject['etablissement.siret']).to eq('44011762001530') }
it { expect(subject['etablissement.siege_social']).to be_truthy }
it { expect(subject['etablissement.naf']).to eq('4950Z') }
it { expect(subject['etablissement.libelle_naf']).to eq('Transports par conduites') }
it { expect(subject['etablissement.adresse']).to eq("GRTGAZ\r IMMEUBLE BORA\r 6 RUE RAOUL NORDLING\r 92270 BOIS COLOMBES\r") }
it { expect(subject['etablissement.numero_voie']).to eq('6') }
it { expect(subject['etablissement.type_voie']).to eq('RUE') }
it { expect(subject['etablissement.nom_voie']).to eq('RAOUL NORDLING') }
it { expect(subject['etablissement.complement_adresse']).to eq('IMMEUBLE BORA') }
it { expect(subject['etablissement.code_postal']).to eq('92270') }
it { expect(subject['etablissement.localite']).to eq('BOIS COLOMBES') }
it { expect(subject['etablissement.code_insee_localite']).to eq('92009') }
it { expect(subject['entreprise.siren']).to eq('440117620') }
it { expect(subject['entreprise.capital_social']).to eq(537100000) }
it { expect(subject['entreprise.numero_tva_intracommunautaire']).to eq('FR27440117620') }
it { expect(subject['entreprise.forme_juridique']).to eq("SA à conseil d'administration (s.a.i.)") }
it { expect(subject['entreprise.forme_juridique_code']).to eq('5599') }
it { expect(subject['entreprise.nom_commercial']).to eq('GRTGAZ') }
it { expect(subject['entreprise.raison_sociale']).to eq('GRTGAZ') }
it { expect(subject['entreprise.siret_siege_social']).to eq('44011762001530') }
it { expect(subject['entreprise.code_effectif_entreprise']).to eq('51') }
it { expect(subject['entreprise.date_creation']).to eq('Thu, 28 Jan 2016 10:16:29 UTC +00:0') }
it { expect(subject['entreprise.nom']).to be_nil }
it { expect(subject['entreprise.prenom']).to be_nil }
end
end end