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:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r106:695a33fa7e55 - - 12 files changed: 157 inserted, 20 deleted

@@ -0,0 +1,5
1 + class Country < ActiveRecord::Base
2 +
3 + has_many :sites
4 +
5 + end
@@ -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
@@ -67,25 +67,92
67 67 def destroy
68 68 User.find(params[:id]).destroy
69 69 redirect_to :action => 'list'
70 70 end
71 71
72 72 def user_stat
73 73 @problems = Problem.find_available_problems
74 74 @users = User.find(:all)
75 75 @scorearray = Array.new
76 76 @users.each do |u|
77 77 ustat = Array.new
78 78 ustat[0] = u.login
79 79 ustat[1] = u.full_name
80 80 @problems.each do |p|
81 81 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
82 82 if (sub!=nil) and (sub.points!=nil)
83 83 ustat << [sub.points, (sub.points>=p.full_score)]
84 84 else
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,24 +1,27
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
9 12 def finished?
10 13 if !self.started
11 14 return false
12 15 end
13 16
14 17 contest_time = Configuration['contest.time_limit']
15 18 if tmatch = /(\d+):(\d+)/.match(contest_time)
16 19 h = tmatch[1].to_i
17 20 m = tmatch[2].to_i
18 21 return Time.now > (self.start_time + h.hour + m.minute)
19 22 else
20 23 false
21 24 end
22 25 end
23 26
24 27 end
@@ -1,43 +1,44
1 1 require 'digest/sha1'
2 2
3 3 class User < ActiveRecord::Base
4 4
5 5 has_and_belongs_to_many :roles
6 6
7 7 has_many :test_requests, :order => "submitted_at DESC"
8 8
9 9 has_many :messages,
10 10 :class_name => "Message",
11 11 :foreign_key => "sender_id",
12 12 :order => 'created_at DESC'
13 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?
26 27 validates_length_of :password, :within => 4..20, :if => :password_required?
27 28 validates_confirmation_of :password, :if => :password_required?
28 29
29 30 attr_accessor :password
30 31
31 32 before_save :encrypt_new_password
32 33
33 34 def self.authenticate(login, password)
34 35 user = find_by_login(login)
35 36 return user if user && user.authenticated?(password)
36 37 end
37 38
38 39 def authenticated?(password)
39 40 hashed_password == User.encrypt(password,self.salt)
40 41 end
41 42
42 43 def admin?
43 44 self.roles.detect {|r| r.name == 'admin' }
@@ -1,14 +1,14
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
14 14 = render :partial => 'message', :collection => @messages, :locals => {:reply => false}
@@ -1,53 +1,57
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>
36 40
37 41 <% for user in @users %>
38 42 <tr>
39 43 <% for column in User.content_columns %>
40 44 <td><%=h user.send(column.name) %></td>
41 45 <% end %>
42 46 <td><%= link_to 'Show', :action => 'show', :id => user %></td>
43 47 <td><%= link_to 'Edit', :action => 'edit', :id => user %></td>
44 48 <td><%= link_to 'Destroy', { :action => 'destroy', :id => user }, :confirm => 'Are you sure?', :method => :post %></td>
45 49 </tr>
46 50 <% end %>
47 51 </table>
48 52
49 53
50 54 <br />
51 55
52 56 <%= link_to 'New user', :action => 'new' %>
53 57 <%= link_to 'New list of users', :action => 'new_list' %>
@@ -1,53 +1,59
1 1 # This file is auto-generated from the current state of the database. Instead of editing this file,
2 2 # please use the migrations feature of ActiveRecord to incrementally modify your database, and
3 3 # then regenerate this schema definition.
4 4 #
5 5 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
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 => 33) do
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"
19 19 t.datetime "updated_at"
20 20 end
21 21
22 22 create_table "configurations", :force => true do |t|
23 23 t.string "key"
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
36 42
37 43 create_table "grader_processes", :force => true do |t|
38 44 t.string "host", :limit => 20
39 45 t.integer "pid"
40 46 t.string "mode"
41 47 t.boolean "active"
42 48 t.datetime "created_at"
43 49 t.datetime "updated_at"
44 50 t.integer "task_id"
45 51 t.string "task_type"
46 52 end
47 53
48 54 add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid"
49 55
50 56 create_table "languages", :force => true do |t|
51 57 t.string "name", :limit => 10
52 58 t.string "pretty_name"
53 59 t.string "ext", :limit => 10
@@ -93,48 +99,50
93 99 end
94 100
95 101 create_table "roles_users", :id => false, :force => true do |t|
96 102 t.integer "role_id"
97 103 t.integer "user_id"
98 104 end
99 105
100 106 add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id"
101 107
102 108 create_table "sessions", :force => true do |t|
103 109 t.string "session_id"
104 110 t.text "data"
105 111 t.datetime "updated_at"
106 112 end
107 113
108 114 add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id"
109 115 add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
110 116
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"
123 131 t.text "source"
124 132 t.binary "binary"
125 133 t.datetime "submitted_at"
126 134 t.datetime "compiled_at"
127 135 t.text "compiler_message"
128 136 t.datetime "graded_at"
129 137 t.integer "points"
130 138 t.text "grader_comment"
131 139 t.integer "number"
132 140 t.string "source_filename"
133 141 end
134 142
135 143 add_index "submissions", ["user_id", "problem_id", "number"], :name => "index_submissions_on_user_id_and_problem_id_and_number", :unique => true
136 144 add_index "submissions", ["user_id", "problem_id"], :name => "index_submissions_on_user_id_and_problem_id"
137 145
138 146 create_table "tasks", :force => true do |t|
139 147 t.integer "submission_id"
140 148 t.datetime "created_at"
@@ -151,29 +159,30
151 159 t.string "running_stat"
152 160 t.integer "status"
153 161 t.datetime "updated_at"
154 162 t.datetime "submitted_at"
155 163 t.datetime "compiled_at"
156 164 t.text "compiler_message"
157 165 t.datetime "graded_at"
158 166 t.string "grader_comment"
159 167 t.datetime "created_at"
160 168 t.float "running_time"
161 169 t.string "exit_status"
162 170 t.integer "memory_usage"
163 171 end
164 172
165 173 add_index "test_requests", ["user_id", "problem_id"], :name => "index_test_requests_on_user_id_and_problem_id"
166 174
167 175 create_table "users", :force => true do |t|
168 176 t.string "login", :limit => 10
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