77 lines
2.5 KiB
Ruby
77 lines
2.5 KiB
Ruby
ActiveAdmin.register_page "SchoolUserUploads" do
|
|
|
|
menu :label => 'School User Upload', :parent => 'Misc'
|
|
|
|
page_action :upload_schooluseruploads, :method => :post do
|
|
User.transaction do
|
|
|
|
puts params
|
|
|
|
file = params[:jam_ruby_user][:csv]
|
|
|
|
|
|
created = 0
|
|
already_existing = 0
|
|
array_of_arrays = CSV.read(file.tempfile.path, headers:true, encoding: 'bom|utf-8')
|
|
array_of_arrays.each do |row|
|
|
school_name = row['School Name']
|
|
school_tag = row['School ID']
|
|
school = School.autocreate_find_from_upload(school_name, school_tag)
|
|
first_name = row['First Name']
|
|
last_name = row['Last Name']
|
|
email_address = row['Email Address']
|
|
license_start = parse_date(row['License Start Date'])
|
|
license_end = parse_date(row['License End Date'])
|
|
source = row['Source']
|
|
password = SecureRandom.uuid
|
|
options = {
|
|
first_name: first_name,
|
|
last_name: last_name,
|
|
email: email_address,
|
|
license_start: license_start,
|
|
license_end: license_end,
|
|
import_source: source,
|
|
school_id: school.id,
|
|
terms_of_service: true,
|
|
musician: true,
|
|
skip_recaptcha: true,
|
|
password: password,
|
|
password_confirmation: password
|
|
}
|
|
|
|
parse_user_type(row['User Type'], options)
|
|
|
|
instrument = Instrument.find('electric guitar')
|
|
instruments= [{instrument_id: instrument.id, proficiency_level: 3, priority: 1}]
|
|
options[:instruments] = instruments
|
|
user = User.signup(options)
|
|
|
|
if user.errors.any?
|
|
puts "User #{user.name} #{user.email} had errors"
|
|
puts user.errors.inspect
|
|
already_existing = already_existing + 1
|
|
else
|
|
puts "User #{user.email} created"
|
|
created = created + 1
|
|
end
|
|
end
|
|
|
|
redirect_to admin_schooluseruploads_path, :notice => "Created #{created} school students. Ignored #{already_existing} because already existed."
|
|
end
|
|
end
|
|
|
|
content do
|
|
panel "Help" do
|
|
link_to "Download Sample CSV", asset_path("Sample_School_User_Upload.csv", target: "_blank", download: "Sample_School_User_Upload.csv")
|
|
end
|
|
|
|
active_admin_form_for User.new, :url => admin_schooluseruploads_upload_schooluseruploads_path, :builder => ActiveAdmin::FormBuilder do |f|
|
|
f.inputs "Upload School Users" do
|
|
f.input :csv, as: :file, required: true, :label => "A school user upload"
|
|
end
|
|
f.actions
|
|
end
|
|
end
|
|
end
|
|
|