Skip to content

Commit

Permalink
Merge pull request #3602 from betagouv/lucien/debug/19-09
Browse files Browse the repository at this point in the history
Améliore l'export xls de l'annuaire pour faire apparaitre tout les experts
  • Loading branch information
clairezed authored Sep 27, 2024
2 parents 694c444 + 1a3ec7e commit 901d6bd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/controllers/annuaire/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def index
format.xlsx do
users = retrieve_users
xlsx_filename = "#{(@antenne || @institution).name.parameterize}-#{users.model_name.human.pluralize.parameterize}.xlsx"
result = users.export_xlsx(include_expert: true, institutions_subjects: institutions_subjects_exportable)
result = XlsxExport::AnnuaireUserExporter.new(@grouped_experts, { relation_name: 'User', institutions_subjects: institutions_subjects_exportable }).export
send_data result.xlsx.to_stream.read, type: "application/xlsx", filename: xlsx_filename
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/api_entreprise/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class Request
def initialize(siren_or_siret, options = {})
@siren_or_siret = siren_or_siret
@options = options
@http_response = HTTP.auth("Bearer #{token}").get(url)
begin
@http_response = HTTP.auth("Bearer #{token}").get(url)
@data = @http_response.parse(:json)
rescue StandardError => e
@error = e
Expand Down
2 changes: 1 addition & 1 deletion app/services/api_insee/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class Request
def initialize(siren_or_siret, options = {})
@siren_or_siret = siren_or_siret
@options = options
@http_response = HTTP.auth("Bearer #{token}").get(url)
begin
@http_response = HTTP.auth("Bearer #{token}").get(url)
@data = @http_response.parse(:json)
rescue StandardError => e
@error = e
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,30 @@
module XlsxExport
# UserExporter supports two options.
# Both require the relation to be fetched using User.relevant_for_skills.
# include_expert
# institutions_subjects: the institutions_subjects to add as csv columns for the relevant expert.
class UserExporter < BaseExporter
class AnnuaireUserExporter < BaseExporter
def fields
fields = base_fields
fields.merge!(fields_for_team) if @options[:include_expert]
fields.merge!(fields_for_subjects) if @options[:institutions_subjects]

fields.merge!(fields_for_team)
fields.merge!(fields_for_subjects)
fields
end

private

def base_fields
{
full_name: :full_name,
antenne: -> { antenne.name },
antenne: :name,
email: :email,
phone_number: :phone_number,
job: :job,
}
end

def preloaded_associations
[
:antenne
]
end

def fields_for_team
{
team_full_name: -> { first_expert_with_subject&.full_name if first_expert_with_subject.present? },
team_email: -> { first_expert_with_subject&.email if first_expert_with_subject.present? },
team_phone_number: -> { first_expert_with_subject&.phone_number if first_expert_with_subject.present? },
team_custom_communes: -> { first_expert_with_subject&.communes.pluck(:insee_code).join(', ') if first_expert_with_subject&.custom_communes? },
team_full_name: :full_name,
team_email: :email,
team_phone_number: :phone_number,
team_custom_communes: -> { communes.pluck(:insee_code).join(', ') if custom_communes? },
}
end

Expand All @@ -47,12 +38,9 @@ def fields_for_subjects
# * There can be only one expert_subject for an (expert, institution_subject) pair.
title = institution_subject.unique_name
lambda = -> {
# This block is executed in the context of a User
# (`self` is a User; See `object.instance_exec(&lambda)` in CsvExport::Base.)
return if first_expert_with_subject.blank?
# This block is executed in the context of a Expert

# We’re using `&` instead of .merge to use the preloaded relations instead of doing a new DB query.
experts_subjects = first_expert_with_subject&.experts_subjects & institution_subject.experts_subjects
experts_subjects = self.experts_subjects.merge(institution_subject.experts_subjects)
raise 'There should only be one ExpertSubject' if experts_subjects.present? && experts_subjects.size > 1
expert_subject = experts_subjects.first
expert_subject&.csv_description
Expand All @@ -61,10 +49,6 @@ def fields_for_subjects
end
end

def sort_relation(relation)
relation.preload(*preloaded_associations).sort_by{ |u| [u.antenne.name, u.first_expert_with_subject&.full_name.to_s] }
end

def create_styles(s)
@green_bg = s.add_style bg_color: 'D9EAD3', alignment: { horizontal: :center, vertical: :center, wrap_text: true },
border: { style: :thin, color: '000000' }
Expand All @@ -82,11 +66,11 @@ def create_styles(s)
s
end

def build_headers_rows(sheet, attributes, klass)
def build_headers_rows(sheet, attributes, _)
# first row
sheet.add_row(headers, height: 60)
# second row, columns titles
sheet.add_row(attributes.keys.map{ |attr| klass.human_attribute_name(attr, default: attr) }, height: 60, widths: [:ignore, :auto,80])
sheet.add_row(attributes.keys.map{ |attr| User.human_attribute_name(attr, default: attr) }, height: 60, widths: [:ignore, :auto,80])
# third row, institution subject description
third_row = fields.values
third_row[0] = ''
Expand All @@ -104,18 +88,31 @@ def build_headers_rows(sheet, attributes, klass)
end

def build_rows(sheet, attributes)
# Users lines
row = attributes.values
sort_relation(@relation).each do |object|
sheet.add_row(row.map do |val|
if val.respond_to? :call
lambda = val
object.instance_exec(&lambda)
else
object.send(val)
row = attributes
@relation.each do |antenne, expert_users|
expert_users.each do |expert, users|
users.each do |user|
sheet.add_row(build_row_data(row, antenne, expert, user), height: 30)
end
end, height: 30)
end
end
sheet
end

def build_row_data(row, antenne, expert, user)
row.map do |key, val|
if key == :antenne
antenne.send(val)
elsif base_fields.key? key
user.send(val)
else
next unless expert.persisted?
val.respond_to?(:call) ? expert.instance_exec(&val) : expert.send(val)
end
end
end

def apply_style(sheet, attributes)
sheet.rows.each_index do |i|
case i
when 0
Expand All @@ -132,11 +129,7 @@ def build_rows(sheet, attributes)
sheet.rows[i].cells[8].style = @green_bg
end
end
sheet
end

def apply_style(sheet, attributes)
# Style
sheet.merge_cells('A1:C1')
sheet.merge_cells('F1:I1')
sheet.merge_cells('B3:E3')
Expand Down
10 changes: 8 additions & 2 deletions app/services/xlsx_export/base_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def export
end

def xlsx
klass = @relation.klass
if @options[:relation_name].present?
klass = @options[:relation_name]
klass_human_name = @options[:relation_name]
else
klass_human_name = klass.model_name.human
klass = @relation.klass
end
attributes = fields # implemented by subclasses

p = Axlsx::Package.new
wb = p.workbook

create_styles wb.styles

wb.add_worksheet(name: @relation.klass.model_name.human.pluralize) do |sheet|
wb.add_worksheet(name: klass_human_name) do |sheet|
build_headers_rows sheet, attributes, klass
build_rows sheet, attributes
apply_style sheet, attributes
Expand Down

0 comments on commit 901d6bd

Please sign in to comment.