FEATURE: Pre-setting user locale via bulk invite (#15195)

This commit is contained in:
Jarek Radosz 2021-12-06 02:08:21 +01:00 committed by GitHub
parent 4bb91754ad
commit 28bf9599f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -111,7 +111,8 @@ module Jobs
email = invite[:email]
groups = get_groups(invite[:groups])
topic = get_topic(invite[:topic_id])
user_fields = get_user_fields(invite.except(:email, :groups, :topic_id))
locale = invite[:locale]
user_fields = get_user_fields(invite.except(:email, :groups, :topic_id, :locale))
begin
if user = Invite.find_user_by_email(email)
@ -133,13 +134,26 @@ module Jobs
end
user.save_custom_fields
end
if locale.present?
user.locale = locale
user.save!
end
else
if user_fields.present?
if user_fields.present? || locale.present?
user = User.where(staged: true).find_by_email(email)
user ||= User.new(username: UserNameSuggester.suggest(email), email: email, staged: true)
user_fields.each do |user_field, value|
user.set_user_field(user_field, value)
if user_fields.present?
user_fields.each do |user_field, value|
user.set_user_field(user_field, value)
end
end
if locale.present?
user.locale = locale
end
user.save!
end

View File

@ -0,0 +1,4 @@
email,locale,team
test@example.com,de,red
test2@example.com,pl,blue
test3@example.com,,red
1 email locale team
2 test@example.com de red
3 test2@example.com pl blue
4 test3@example.com red

View File

@ -896,6 +896,8 @@ describe InvitesController do
let(:csv_file_with_headers) { File.new("#{Rails.root}/spec/fixtures/csv/discourse_headers.csv") }
let(:file_with_headers) { Rack::Test::UploadedFile.new(File.open(csv_file_with_headers)) }
let(:csv_file_with_locales) { File.new("#{Rails.root}/spec/fixtures/csv/invites_with_locales.csv") }
let(:file_with_locales) { Rack::Test::UploadedFile.new(File.open(csv_file_with_locales)) }
it 'fails if you cannot bulk invite to the forum' do
sign_in(Fabricate(:user))
@ -947,6 +949,20 @@ describe InvitesController do
user2 = User.where(staged: true).find_by_email('test2@example.com')
expect(user2.user_fields[user_field.id.to_s]).to eq('europe')
end
it 'can pre-set user locales' do
Jobs.run_immediately!
sign_in(admin)
post '/invites/upload_csv.json', params: { file: file_with_locales, name: 'discourse_headers.csv' }
expect(response.status).to eq(200)
user = User.where(staged: true).find_by_email('test@example.com')
expect(user.locale).to eq('de')
user2 = User.where(staged: true).find_by_email('test2@example.com')
expect(user2.locale).to eq('pl')
end
end
end
end