You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# frozen_string_literal: truerequire"ruby-progressbar"namespace:importdodesc"Usage: rake import:user FILE='<filename.csv>' ORG=<organization_id> ADMIN=<admin_id> PROCESS=<process_id> [VERBOSE=true]'"# TODO: Add teststaskuser: :environmentdodefvalidate_inputvalidate_filevalidate_processvalidate_adminvalidate_orgenddefvalidate_orgif@org.class != Integerputs"You must pass an organization id as an integer"exit1endunlesscurrent_organizationputs"Organization does not exist"exit1endenddefvalidate_adminif@admin.class != Integerputs"You must pass an admin id as an integer"exit1endunlesscurrent_userputs"Admin does not exist"exit1endenddefvalidate_processif@process.class != Integerputs"You must pass a process id as an integer"exit1endunlesscurrent_processputs"Process does not exist"exit1endenddefvalidate_fileunlessFile.exist?(@file)puts"File does not exist, be sure to pass a full path."exit1endifFile.extname(@file) != ".csv"puts"You must pass a CSV file"exit1endenddefdisplay_helpputs<<~HEREDOC Help: Usage: rake import:user FILE='<filename.csv>' ORG=<organization_id> ADMIN=<admin_id> PROCESS=<process_id> HEREDOCexit0enddefcheck_csv(file)file.eachdo |row|
# Check if id, first_name, last_name are nilnextunlessrow[0].nil? || row[1].nil? || row[2].nil?puts"Something went wrong, empty field(s) on line #{$INPUT_LINE_NUMBER}"putsrow.inspectexit1endenddefimport_data(id,first_name,last_name,email)# Extends are only loaded at the last timerequire"extends/commands/decidim/admin/create_participatory_space_private_user_extends"require"extends/commands/decidim/admin/impersonate_user_extends"ifemail.nil?import_without_email(id,first_name,last_name)elseimport_with_email(id,first_name,last_name,email)endenddefimport_without_email(id,first_name,last_name)new_user=Decidim::User.new(managed: true,name: set_name(first_name,last_name),organization: current_organization,admin: false,roles: [],tos_agreement: true)form=Decidim::Admin::ImpersonateUserForm.from_params(user: new_user,name: new_user.name,reason: "import",handler_name: "osp_authorization_handler",authorization: Decidim::AuthorizationHandler.handler_for("osp_authorization_handler",{user: new_user,document_number: id})).with_context(current_organization: current_organization,current_user: current_user)privatable_to=current_processDecidim::Admin::ImpersonateUser.call(form)doon(:ok)do |user|
Decidim::ParticipatorySpacePrivateUser.find_or_create_by!(user: user,privatable_to: privatable_to)Rails.logger.debugI18n.t("participatory_space_private_users.create.success",scope: "decidim.admin")Rails.logger.debug{"Registered user with id: #{id}, first_name: #{first_name}, last_name: #{last_name} --> #{user.id}"}endon(:invalid)doRails.logger.debugI18n.t("participatory_space_private_users.create.error",scope: "decidim.admin")Rails.logger.debuguser.errors.full_messagesifuser.invalid?Rails.logger.debugform.errors.full_messagesifform.invalid?Rails.logger.debug{"Failed to register user with id: #{id}, first_name: #{first_name}, last_name: #{last_name} !!"}# exit 1endendenddefimport_with_email(id,first_name,last_name,email)form=Decidim::Admin::ParticipatorySpacePrivateUserForm.from_params({name: set_name(first_name,last_name),email: email},privatable_to: current_process)Decidim::Admin::CreateParticipatorySpacePrivateUser.call(form,current_user,current_process)doon(:ok)do |user|
Decidim::Authorization.create_or_update_from(Decidim::AuthorizationHandler.handler_for("osp_authorization_handler",{user: user,document_number: id}))Rails.logger.debugI18n.t("participatory_space_private_users.create.success",scope: "decidim.admin")Rails.logger.debug{"Registered user with id: #{id}, first_name: #{first_name}, last_name: #{last_name}, email: #{email} --> #{user.id}"}endon(:invalid)doRails.logger.debugI18n.t("participatory_space_private_users.create.error",scope: "decidim.admin")Rails.logger.debugform.errors.full_messagesifform.invalid?Rails.logger.debug{"Failed to register user with id: #{id}, first_name: #{first_name}, last_name: #{last_name}, email: #{email} !!"}# exit 1endendenddefset_name(first_name,last_name)"#{first_name}#{last_name}"enddefcurrent_user@current_user ||= Decidim::User.find(@admin)enddefcurrent_organization@current_organization ||= Decidim::Organization.find(@org)enddefcurrent_process@current_process ||= Decidim::ParticipatoryProcess.find(@process)endRails.application.config.active_job.queue_adapter=:inline@verbose=ENV["VERBOSE"].to_s == "true"Rails.logger=if@verboseLogger.new($stdout)elseLogger.new("log/import-user-#{Time.zone.now.strftime"%Y-%m-%d-%H:%M:%S"}.log")enddisplay_helpunlessENV.fetch("FILE",nil) && ENV.fetch("ORG",nil) && ENV.fetch("ADMIN",nil) && ENV.fetch("PROCESS",nil)@file=ENV.fetch("FILE",nil)@org=ENV["ORG"].to_i@admin=ENV["ADMIN"].to_i@process=ENV["PROCESS"].to_i@auth_handler=ENV.fetch("AUTH_HANDLER",nil)validate_inputcsv=CSV.read(@file,col_sep: ",",headers: true,skip_blanks: true)check_csv(csv)count=CSV.read(@file).countputs"CSV file is #{count} lines long"progressbar=ProgressBar.create(title: "Importing User",total: count,format: "%t%e%B%p%%")unless@verbosecsv.eachdo |row|
progressbar.incrementunless@verbose# Import user with parsed informations id, first_name, last_name, emailimport_data(row[0],row[1],row[2],row[3])endRails.logger.closeendend
The text was updated successfully, but these errors were encountered:
exit 1
exit 1
exit 1
exit 1
decidim-ubx/lib/tasks/import.rake
Line 7 in c2b1f0f
The text was updated successfully, but these errors were encountered: