Skip to content

Commit

Permalink
✨ Modifie la tâche rspec pour attribuer les assets aux questions sous…
Browse files Browse the repository at this point in the history
… consignes
  • Loading branch information
marouria committed Nov 13, 2024
1 parent 80304b7 commit 718aa8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
62 changes: 44 additions & 18 deletions lib/tasks/questions.rake
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,69 @@ NOM_TECHNIQUES_QCM = {
hcvf_3: 'rubrique_environnement',
hcvf_4: 'rubrique_environnement'
}.freeze

NOM_TECHNIQUES_CONSIGNES = {
sous_consigne_LOdi_1: 'programme_tele',
sous_consigne_LOdi_2: 'programme_tele_zoom',
sous_consigne_ALrd_1: 'terrasse_cafe',
sous_consigne_ALrd_2: 'liste_titres_musique',
sous_consigne_ACrd_1: 'magazine_sans_texte',
sous_consigne_ACrd_2: 'magazine_sans_texte',
sous_consigne_APlc_1: 'terrasse_cafe',
sous_consigne_APlc_2: 'telephone_liste_de_courses',
sous_consigne_HPar_1: 'hpar_c1',
sous_consigne_HGac_1: 'graphique_avec_selection',
sous_consigne_HCvf_1: 'rubrique_environnement',
sous_consigne_HPfb_1: 'terrasse_cafe',
sous_consigne_HPfb_2: 'telephone_email'
}.freeze
# rubocop:enable Naming/VariableNumber

DOSSIER_ID = 'DOSSIER_ID'
TYPE_QUESTION = 'TYPE_QUESTION'

namespace :questions do
desc 'Attache les assets de cafe de la place aux instances de Question correpondantes'
task attache_assets: :environment do
dossier_id = 'DOSSIER_ID'
logger = RakeLogger.logger
unless ENV.key?(dossier_id)
logger.error "La variable d'environnement #{dossier_id} est manquante"
unless ENV.key?(DOSSIER_ID)
logger.error "La variable d'environnement #{DOSSIER_ID} est manquante"
logger.info 'Usage : rake questions:attache_assets DOSSIER_ID=<dossier_id>'
next
end

@drive = GoogleDriveStorage.new
begin
@drive.existe_dossier?(ENV.fetch(dossier_id, nil))
@drive.existe_dossier?(ENV.fetch(DOSSIER_ID, nil))
rescue GoogleDriveStorage::Error => e
logger.error e.message
next
end

NOM_TECHNIQUES_QCM.each do |question_nom_technique, nom_illustration|
question = Question.find_by(nom_technique: question_nom_technique)
if question
recupere_fichier(ENV.fetch(dossier_id, nil), question, "#{nom_illustration}.png")
recupere_fichier(ENV.fetch(dossier_id, nil), question, "#{question.nom_technique}.mp3",
question.transcription_intitule)
attach_audio_choix(ENV.fetch(dossier_id, nil), question)
else
puts "Pas de question trouvée pour le nom_technique '#{question_nom_technique}'"
end
case ENV.fetch(TYPE_QUESTION, nil)
when 'QCM'
attache_assets(NOM_TECHNIQUES_QCM)
when 'SOUS_CONSIGNE'
attache_assets(NOM_TECHNIQUES_CONSIGNES)
end
end
end

def attache_assets(nom_techniques)
nom_techniques.each do |question_nom_technique, nom_illustration|
question = Question.find_by(nom_technique: question_nom_technique)
if question
recupere_fichier(question, "#{nom_illustration}.png")
recupere_fichier(question, "#{question.nom_technique}.mp3", question.transcription_intitule)
attach_audio_choix(question) if nom_techniques == NOM_TECHNIQUES_QCM
else
puts "Pas de question trouvée pour le nom_technique '#{question_nom_technique}'"
end
end
end

def recupere_fichier(dossier_id, question, fichier_path, model = nil)
file = @drive.recupere_fichier(dossier_id, fichier_path)
def recupere_fichier(question, fichier_path, model = nil)
file = @drive.recupere_fichier(ENV.fetch(DOSSIER_ID, nil), fichier_path)
if file
file_content = file.download_to_string
attach_file_to(model || question, file_content, fichier_path, question.nom_technique)
Expand All @@ -86,9 +112,9 @@ rescue GoogleDriveStorage::Error => e
logger.error e.message
end

def attach_audio_choix(dossier_id, question)
def attach_audio_choix(question)
question.choix.each do |choix|
recupere_fichier(dossier_id, question, "#{choix.nom_technique}.mp3", choix)
recupere_fichier(question, "#{choix.nom_technique}.mp3", choix)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/tasks/questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

before do
ENV['DOSSIER_ID'] = 'fake-dossier-id'
ENV['TYPE_QUESTION'] = 'QCM'
drive = instance_double(GoogleDriveStorage)
allow(GoogleDriveStorage).to receive(:new).and_return(drive)
allow(drive).to receive(:existe_dossier?).with('fake-dossier-id').and_return(true)
Expand Down

0 comments on commit 718aa8e

Please sign in to comment.