diff --git a/app/controllers/user_admin_controller.rb b/app/controllers/user_admin_controller.rb --- a/app/controllers/user_admin_controller.rb +++ b/app/controllers/user_admin_controller.rb @@ -88,4 +88,71 @@ @scorearray << ustat end end + + def import + if params[:file]=='' + flash[:notice] = 'Error importing no file' + redirect_to :action => 'list' and return + end + import_from_file(params[:file]) + end + + protected + + def import_from_file(f) + data_hash = YAML.load(f) + @import_log = "" + + country_data = data_hash[:countries] + site_data = data_hash[:sites] + user_data = data_hash[:users] + + # import country + countries = {} + country_data.each_pair do |id,country| + c = Country.find_by_name(country[:name]) + if c!=nil + countries[id] = c + @import_log << "Found #{country[:name]}\n" + else + countries[id] = Country.new(:name => country[:name]) + countries[id].save + @import_log << "Created #{country[:name]}\n" + end + end + + # import sites + sites = {} + site_data.each_pair do |id,site| + s = Site.find_by_name(site[:name]) + if s!=nil + @import_log << "Found #{site[:name]}\n" + else + s = Site.new(:name => site[:name]) + @import_log << "Created #{site[:name]}\n" + end + s.password = site[:password] + s.country = countries[site[:country_id]] + s.save + sites[id] = s + end + + # import users + user_data.each_pair do |id,user| + u = User.find_by_login(user[:login]) + if u!=nil + @import_log << "Found #{user[:login]}\n" + else + u = User.new(:login => user[:login]) + @import_log << "Created #{user[:login]}\n" + end + u.full_name = user[:name] + u.password = user[:password] + u.country = countries[user[:country_id]] + u.site = sites[user[:site_id]] + u.save + end + + end + end diff --git a/app/models/country.rb b/app/models/country.rb new file mode 100644 --- /dev/null +++ b/app/models/country.rb @@ -0,0 +1,5 @@ +class Country < ActiveRecord::Base + + has_many :sites + +end diff --git a/app/models/site.rb b/app/models/site.rb --- a/app/models/site.rb +++ b/app/models/site.rb @@ -1,5 +1,8 @@ class Site < ActiveRecord::Base + belongs_to :country + has_many :users + def clear_start_time_if_not_started if !self.started self.start_time = nil diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,6 +17,7 @@ :order => 'created_at DESC' belongs_to :site + belongs_to :country validates_presence_of :login validates_presence_of :full_name diff --git a/app/views/messages/list.html.haml b/app/views/messages/list.html.haml --- a/app/views/messages/list.html.haml +++ b/app/views/messages/list.html.haml @@ -4,7 +4,7 @@ - form_for 'message', nil, :url => { :action => 'create'} do |f| %p - %b New message + %b New clarification request = submit_tag "Post" %br/ = f.text_area :body, :rows => 5, :cols => 100 diff --git a/app/views/user_admin/import.html.haml b/app/views/user_admin/import.html.haml new file mode 100644 --- /dev/null +++ b/app/views/user_admin/import.html.haml @@ -0,0 +1,6 @@ +return to += link_to '[user list]', :action => 'list' + +%h3 Import log + += simple_format(@import_log) diff --git a/app/views/user_admin/list.rhtml b/app/views/user_admin/list.rhtml --- a/app/views/user_admin/list.rhtml +++ b/app/views/user_admin/list.rhtml @@ -4,29 +4,33 @@

Listing users

-
+
Quick add <% form_tag :action => 'create' do %> - - - - - - - - - - - - - - - -
<%= text_field 'user', 'login', :size => 10 %><%= text_field 'user', 'full_name', :size => 30 %><%= text_field 'user', 'alias', :size => 10 %><%= password_field 'user', 'password', :size => 10 %><%= password_field 'user', 'password_confirmation', :size => 10 %><%= submit_tag "Create" %>
+ + + + + + + + + + + + + + + +
<%= text_field 'user', 'login', :size => 10 %><%= text_field 'user', 'full_name', :size => 30 %><%= text_field 'user', 'alias', :size => 10 %><%= password_field 'user', 'password', :size => 10 %><%= password_field 'user', 'password_confirmation', :size => 10 %><%= submit_tag "Create" %>
+<% end %> +
+Import from site management +<% form_tag({:action => 'import'}, :multipart => true) do %> + File: <%= file_field_tag 'file' %> <%= submit_tag 'Import' %> <% end %>
- <% for column in User.content_columns %> diff --git a/db/migrate/034_create_countries.rb b/db/migrate/034_create_countries.rb new file mode 100644 --- /dev/null +++ b/db/migrate/034_create_countries.rb @@ -0,0 +1,12 @@ +class CreateCountries < ActiveRecord::Migration + def self.up + create_table :countries do |t| + t.column :name, :string + t.timestamps + end + end + + def self.down + drop_table :countries + end +end diff --git a/db/migrate/035_add_country_to_sites_and_users.rb b/db/migrate/035_add_country_to_sites_and_users.rb new file mode 100644 --- /dev/null +++ b/db/migrate/035_add_country_to_sites_and_users.rb @@ -0,0 +1,15 @@ +class AddCountryToSitesAndUsers < ActiveRecord::Migration + def self.up + add_column 'sites', 'country_id', :integer + add_column 'sites', 'password', :string + + add_column 'users', 'country_id', :integer + end + + def self.down + remove_column 'users', 'country_id' + + remove_column 'sites', 'country_id' + remove_column 'sites', 'password' + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 33) do +ActiveRecord::Schema.define(:version => 35) do create_table "announcements", :force => true do |t| t.string "author" @@ -27,6 +27,12 @@ t.datetime "updated_at" end + create_table "countries", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "descriptions", :force => true do |t| t.text "body" t.boolean "markdowned" @@ -114,6 +120,8 @@ t.datetime "start_time" t.datetime "created_at" t.datetime "updated_at" + t.integer "country_id" + t.string "password" end create_table "submissions", :force => true do |t| @@ -172,6 +180,7 @@ t.string "alias" t.string "email" t.integer "site_id" + t.integer "country_id" end add_index "users", ["login"], :name => "index_users_on_login", :unique => true diff --git a/test/fixtures/countries.yml b/test/fixtures/countries.yml new file mode 100644 --- /dev/null +++ b/test/fixtures/countries.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +# one: +# column: value +# +# two: +# column: value diff --git a/test/unit/country_test.rb b/test/unit/country_test.rb new file mode 100644 --- /dev/null +++ b/test/unit/country_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class CountryTest < ActiveSupport::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end