Description:
[web] import from site
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@224 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r106:695a33fa7e55 - - 12 files changed: 141 inserted, 4 deleted
@@ -0,0 +1,6 | |||
|
1 | + return to | |
|
2 | + = link_to '[user list]', :action => 'list' | |
|
3 | + | |
|
4 | + %h3 Import log | |
|
5 | + | |
|
6 | + = simple_format(@import_log) |
@@ -0,0 +1,12 | |||
|
1 | + class CreateCountries < ActiveRecord::Migration | |
|
2 | + def self.up | |
|
3 | + create_table :countries do |t| | |
|
4 | + t.column :name, :string | |
|
5 | + t.timestamps | |
|
6 | + end | |
|
7 | + end | |
|
8 | + | |
|
9 | + def self.down | |
|
10 | + drop_table :countries | |
|
11 | + end | |
|
12 | + end |
@@ -0,0 +1,15 | |||
|
1 | + class AddCountryToSitesAndUsers < ActiveRecord::Migration | |
|
2 | + def self.up | |
|
3 | + add_column 'sites', 'country_id', :integer | |
|
4 | + add_column 'sites', 'password', :string | |
|
5 | + | |
|
6 | + add_column 'users', 'country_id', :integer | |
|
7 | + end | |
|
8 | + | |
|
9 | + def self.down | |
|
10 | + remove_column 'users', 'country_id' | |
|
11 | + | |
|
12 | + remove_column 'sites', 'country_id' | |
|
13 | + remove_column 'sites', 'password' | |
|
14 | + end | |
|
15 | + end |
@@ -0,0 +1,7 | |||
|
1 | + # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | |
|
2 | + | |
|
3 | + # one: | |
|
4 | + # column: value | |
|
5 | + # | |
|
6 | + # two: | |
|
7 | + # column: value |
@@ -0,0 +1,8 | |||
|
1 | + require File.dirname(__FILE__) + '/../test_helper' | |
|
2 | + | |
|
3 | + class CountryTest < ActiveSupport::TestCase | |
|
4 | + # Replace this with your real tests. | |
|
5 | + def test_truth | |
|
6 | + assert true | |
|
7 | + end | |
|
8 | + end |
@@ -88,4 +88,71 | |||
|
88 | 88 | @scorearray << ustat |
|
89 | 89 | end |
|
90 | 90 | end |
|
91 | + | |
|
92 | + def import | |
|
93 | + if params[:file]=='' | |
|
94 | + flash[:notice] = 'Error importing no file' | |
|
95 | + redirect_to :action => 'list' and return | |
|
91 | 96 | end |
|
97 | + import_from_file(params[:file]) | |
|
98 | + end | |
|
99 | + | |
|
100 | + protected | |
|
101 | + | |
|
102 | + def import_from_file(f) | |
|
103 | + data_hash = YAML.load(f) | |
|
104 | + @import_log = "" | |
|
105 | + | |
|
106 | + country_data = data_hash[:countries] | |
|
107 | + site_data = data_hash[:sites] | |
|
108 | + user_data = data_hash[:users] | |
|
109 | + | |
|
110 | + # import country | |
|
111 | + countries = {} | |
|
112 | + country_data.each_pair do |id,country| | |
|
113 | + c = Country.find_by_name(country[:name]) | |
|
114 | + if c!=nil | |
|
115 | + countries[id] = c | |
|
116 | + @import_log << "Found #{country[:name]}\n" | |
|
117 | + else | |
|
118 | + countries[id] = Country.new(:name => country[:name]) | |
|
119 | + countries[id].save | |
|
120 | + @import_log << "Created #{country[:name]}\n" | |
|
121 | + end | |
|
122 | + end | |
|
123 | + | |
|
124 | + # import sites | |
|
125 | + sites = {} | |
|
126 | + site_data.each_pair do |id,site| | |
|
127 | + s = Site.find_by_name(site[:name]) | |
|
128 | + if s!=nil | |
|
129 | + @import_log << "Found #{site[:name]}\n" | |
|
130 | + else | |
|
131 | + s = Site.new(:name => site[:name]) | |
|
132 | + @import_log << "Created #{site[:name]}\n" | |
|
133 | + end | |
|
134 | + s.password = site[:password] | |
|
135 | + s.country = countries[site[:country_id]] | |
|
136 | + s.save | |
|
137 | + sites[id] = s | |
|
138 | + end | |
|
139 | + | |
|
140 | + # import users | |
|
141 | + user_data.each_pair do |id,user| | |
|
142 | + u = User.find_by_login(user[:login]) | |
|
143 | + if u!=nil | |
|
144 | + @import_log << "Found #{user[:login]}\n" | |
|
145 | + else | |
|
146 | + u = User.new(:login => user[:login]) | |
|
147 | + @import_log << "Created #{user[:login]}\n" | |
|
148 | + end | |
|
149 | + u.full_name = user[:name] | |
|
150 | + u.password = user[:password] | |
|
151 | + u.country = countries[user[:country_id]] | |
|
152 | + u.site = sites[user[:site_id]] | |
|
153 | + u.save | |
|
154 | + end | |
|
155 | + | |
|
156 | + end | |
|
157 | + | |
|
158 | + end |
@@ -1,5 +1,8 | |||
|
1 | 1 | class Site < ActiveRecord::Base |
|
2 | 2 | |
|
3 | + belongs_to :country | |
|
4 | + has_many :users | |
|
5 | + | |
|
3 | 6 | def clear_start_time_if_not_started |
|
4 | 7 | if !self.started |
|
5 | 8 | self.start_time = nil |
@@ -17,6 +17,7 | |||
|
17 | 17 | :order => 'created_at DESC' |
|
18 | 18 | |
|
19 | 19 | belongs_to :site |
|
20 | + belongs_to :country | |
|
20 | 21 | |
|
21 | 22 | validates_presence_of :login |
|
22 | 23 | validates_presence_of :full_name |
@@ -4,7 +4,7 | |||
|
4 | 4 | |
|
5 | 5 | - form_for 'message', nil, :url => { :action => 'create'} do |f| |
|
6 | 6 | %p |
|
7 | - %b New message | |
|
7 | + %b New clarification request | |
|
8 | 8 | = submit_tag "Post" |
|
9 | 9 | %br/ |
|
10 | 10 | = f.text_area :body, :rows => 5, :cols => 100 |
@@ -4,7 +4,7 | |||
|
4 | 4 | |
|
5 | 5 | <h1>Listing users</h1> |
|
6 | 6 | |
|
7 | - <div style="border: solid 1px; margin: 2px"> | |
|
7 | + <div class="submitbox"> | |
|
8 | 8 | <b>Quick add</b> |
|
9 | 9 | <% form_tag :action => 'create' do %> |
|
10 | 10 | <table border="0"> |
@@ -24,9 +24,13 | |||
|
24 | 24 | <td><%= submit_tag "Create" %></td> |
|
25 | 25 | </tr></table> |
|
26 | 26 | <% end %> |
|
27 | + <br/> | |
|
28 | + <b>Import from site management</b> | |
|
29 | + <% form_tag({:action => 'import'}, :multipart => true) do %> | |
|
30 | + File: <%= file_field_tag 'file' %> <%= submit_tag 'Import' %> | |
|
31 | + <% end %> | |
|
27 | 32 | |
|
28 | 33 | </div> |
|
29 | - | |
|
30 | 34 | <table> |
|
31 | 35 | <tr> |
|
32 | 36 | <% for column in User.content_columns %> |
@@ -9,7 +9,7 | |||
|
9 | 9 | # |
|
10 | 10 | # It's strongly recommended to check this file into your version control system. |
|
11 | 11 | |
|
12 |
- ActiveRecord::Schema.define(:version => 3 |
|
|
12 | + ActiveRecord::Schema.define(:version => 35) do | |
|
13 | 13 | |
|
14 | 14 | create_table "announcements", :force => true do |t| |
|
15 | 15 | t.string "author" |
@@ -27,6 +27,12 | |||
|
27 | 27 | t.datetime "updated_at" |
|
28 | 28 | end |
|
29 | 29 | |
|
30 | + create_table "countries", :force => true do |t| | |
|
31 | + t.string "name" | |
|
32 | + t.datetime "created_at" | |
|
33 | + t.datetime "updated_at" | |
|
34 | + end | |
|
35 | + | |
|
30 | 36 | create_table "descriptions", :force => true do |t| |
|
31 | 37 | t.text "body" |
|
32 | 38 | t.boolean "markdowned" |
@@ -114,6 +120,8 | |||
|
114 | 120 | t.datetime "start_time" |
|
115 | 121 | t.datetime "created_at" |
|
116 | 122 | t.datetime "updated_at" |
|
123 | + t.integer "country_id" | |
|
124 | + t.string "password" | |
|
117 | 125 | end |
|
118 | 126 | |
|
119 | 127 | create_table "submissions", :force => true do |t| |
@@ -172,6 +180,7 | |||
|
172 | 180 | t.string "alias" |
|
173 | 181 | t.string "email" |
|
174 | 182 | t.integer "site_id" |
|
183 | + t.integer "country_id" | |
|
175 | 184 | end |
|
176 | 185 | |
|
177 | 186 | add_index "users", ["login"], :name => "index_users_on_login", :unique => true |
You need to be logged in to leave comments.
Login now