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: 157 inserted, 20 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 |
@@ -85,7 +85,74 | |||
|
85 | 85 | ustat << [0,false] |
|
86 | 86 | end |
|
87 | 87 | end |
|
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 | |
|
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 | + | |
|
91 | 158 | end |
@@ -1,8 +1,11 | |||
|
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 |
|
6 | 9 | end |
|
7 | 10 | end |
|
8 | 11 |
@@ -14,12 +14,13 | |||
|
14 | 14 | has_many :replied_messages, |
|
15 | 15 | :class_name => "Message", |
|
16 | 16 | :foreign_key => "receiver_id", |
|
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 |
|
23 | 24 | validates_length_of :full_name, :minimum => 1 |
|
24 | 25 | |
|
25 | 26 | validates_presence_of :password, :if => :password_required? |
@@ -1,13 +1,13 | |||
|
1 | 1 | = user_title_bar(@user) |
|
2 | 2 | |
|
3 | 3 | %h3 Your Messages |
|
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 |
|
11 | 11 | |
|
12 | 12 | %hr/ |
|
13 | 13 |
@@ -1,35 +1,39 | |||
|
1 | 1 | <% content_for :head do %> |
|
2 | 2 | <%= stylesheet_link_tag 'scaffold' %> |
|
3 | 3 | <% end %> |
|
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 | - <table border="0"> | |
|
11 | - <tr> | |
|
12 | - <td><label for="user_login">Login</label></td> | |
|
13 | - <td><label for="user_full_name">Full name</label></td> | |
|
14 | - <td><label for="user_alias">Alias</label></td> | |
|
15 | - <td><label for="user_password">Password</label></td> | |
|
16 | - <td><label for="user_password_confirmation">confirm</label></td> | |
|
17 | - </tr> | |
|
18 | - <tr> | |
|
19 | - <td><%= text_field 'user', 'login', :size => 10 %></td> | |
|
20 | - <td><%= text_field 'user', 'full_name', :size => 30 %></td> | |
|
21 | - <td><%= text_field 'user', 'alias', :size => 10 %></td> | |
|
22 | - <td><%= password_field 'user', 'password', :size => 10 %></td> | |
|
23 | - <td><%= password_field 'user', 'password_confirmation', :size => 10 %></td> | |
|
24 | - <td><%= submit_tag "Create" %></td> | |
|
25 | - </tr></table> | |
|
10 | + <table border="0"> | |
|
11 | + <tr> | |
|
12 | + <td><label for="user_login">Login</label></td> | |
|
13 | + <td><label for="user_full_name">Full name</label></td> | |
|
14 | + <td><label for="user_alias">Alias</label></td> | |
|
15 | + <td><label for="user_password">Password</label></td> | |
|
16 | + <td><label for="user_password_confirmation">confirm</label></td> | |
|
17 | + </tr> | |
|
18 | + <tr> | |
|
19 | + <td><%= text_field 'user', 'login', :size => 10 %></td> | |
|
20 | + <td><%= text_field 'user', 'full_name', :size => 30 %></td> | |
|
21 | + <td><%= text_field 'user', 'alias', :size => 10 %></td> | |
|
22 | + <td><%= password_field 'user', 'password', :size => 10 %></td> | |
|
23 | + <td><%= password_field 'user', 'password_confirmation', :size => 10 %></td> | |
|
24 | + <td><%= submit_tag "Create" %></td> | |
|
25 | + </tr></table> | |
|
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' %> | |
|
26 | 31 | <% end %> |
|
27 | 32 | |
|
28 | 33 | </div> |
|
29 | - | |
|
30 | 34 | <table> |
|
31 | 35 | <tr> |
|
32 | 36 | <% for column in User.content_columns %> |
|
33 | 37 | <th><%= column.human_name %></th> |
|
34 | 38 | <% end %> |
|
35 | 39 | </tr> |
@@ -6,13 +6,13 | |||
|
6 | 6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
7 | 7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
8 | 8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
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" |
|
16 | 16 | t.text "body" |
|
17 | 17 | t.boolean "published" |
|
18 | 18 | t.datetime "created_at" |
@@ -24,12 +24,18 | |||
|
24 | 24 | t.string "value_type" |
|
25 | 25 | t.string "value" |
|
26 | 26 | t.datetime "created_at" |
|
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" |
|
33 | 39 | t.datetime "created_at" |
|
34 | 40 | t.datetime "updated_at" |
|
35 | 41 | end |
@@ -111,12 +117,14 | |||
|
111 | 117 | create_table "sites", :force => true do |t| |
|
112 | 118 | t.string "name" |
|
113 | 119 | t.boolean "started" |
|
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| |
|
120 | 128 | t.integer "user_id" |
|
121 | 129 | t.integer "problem_id" |
|
122 | 130 | t.integer "language_id" |
@@ -169,11 +177,12 | |||
|
169 | 177 | t.string "full_name" |
|
170 | 178 | t.string "hashed_password" |
|
171 | 179 | t.string "salt", :limit => 5 |
|
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 |
|
178 | 187 | |
|
179 | 188 | end |
You need to be logged in to leave comments.
Login now