Description:
Merge branch 'master' into codejom (bug fix)
Commit status:
[Not Reviewed]
References:
merge default
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r230:1925d77bb0c3 - - 1 file changed: 1 inserted, 1 deleted

@@ -1,408 +1,408
1 class MainController < ApplicationController
1 class MainController < ApplicationController
2
2
3 before_filter :authenticate, :except => [:index, :login]
3 before_filter :authenticate, :except => [:index, :login]
4 before_filter :check_viewability, :except => [:index, :login]
4 before_filter :check_viewability, :except => [:index, :login]
5
5
6 # COMMENTED OUT: filter in each action instead
6 # COMMENTED OUT: filter in each action instead
7 # before_filter :verify_time_limit, :only => [:submit]
7 # before_filter :verify_time_limit, :only => [:submit]
8
8
9 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
9 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
10 :redirect_to => { :action => :index }
10 :redirect_to => { :action => :index }
11
11
12 # COMMENT OUT: only need when having high load
12 # COMMENT OUT: only need when having high load
13 # caches_action :index, :login
13 # caches_action :index, :login
14
14
15 # NOTE: This method is not actually needed, 'config/routes.rb' has
15 # NOTE: This method is not actually needed, 'config/routes.rb' has
16 # assigned action login as a default action.
16 # assigned action login as a default action.
17 def index
17 def index
18 redirect_to :action => 'login'
18 redirect_to :action => 'login'
19 end
19 end
20
20
21 def login
21 def login
22 saved_notice = flash[:notice]
22 saved_notice = flash[:notice]
23 reset_session
23 reset_session
24 - flash[:notice] = saved_notice
24 + flash.now[:notice] = saved_notice
25
25
26 # EXPERIMENT:
26 # EXPERIMENT:
27 # Hide login if in single user mode and the url does not
27 # Hide login if in single user mode and the url does not
28 # explicitly specify /login
28 # explicitly specify /login
29 #
29 #
30 # logger.info "PATH: #{request.path}"
30 # logger.info "PATH: #{request.path}"
31 # if Configuration['system.single_user_mode'] and
31 # if Configuration['system.single_user_mode'] and
32 # request.path!='/main/login'
32 # request.path!='/main/login'
33 # @hidelogin = true
33 # @hidelogin = true
34 # end
34 # end
35
35
36 @announcements = Announcement.find_for_frontpage
36 @announcements = Announcement.find_for_frontpage
37 render :action => 'login', :layout => 'empty'
37 render :action => 'login', :layout => 'empty'
38 end
38 end
39
39
40 def list
40 def list
41 prepare_list_information
41 prepare_list_information
42 end
42 end
43
43
44 def help
44 def help
45 @user = User.find(session[:user_id])
45 @user = User.find(session[:user_id])
46 end
46 end
47
47
48 def submit
48 def submit
49 user = User.find(session[:user_id])
49 user = User.find(session[:user_id])
50
50
51 @submission = Submission.new(params[:submission])
51 @submission = Submission.new(params[:submission])
52 @submission.user = user
52 @submission.user = user
53 @submission.language_id = 0
53 @submission.language_id = 0
54 if (params['file']) and (params['file']!='')
54 if (params['file']) and (params['file']!='')
55 @submission.source = params['file'].read
55 @submission.source = params['file'].read
56 @submission.source_filename = params['file'].original_filename
56 @submission.source_filename = params['file'].original_filename
57 end
57 end
58 @submission.submitted_at = Time.new.gmtime
58 @submission.submitted_at = Time.new.gmtime
59
59
60 if Configuration.time_limit_mode? and user.contest_finished?
60 if Configuration.time_limit_mode? and user.contest_finished?
61 @submission.errors.add_to_base "The contest is over."
61 @submission.errors.add_to_base "The contest is over."
62 prepare_list_information
62 prepare_list_information
63 render :action => 'list' and return
63 render :action => 'list' and return
64 end
64 end
65
65
66 if @submission.valid?
66 if @submission.valid?
67 if @submission.save == false
67 if @submission.save == false
68 flash[:notice] = 'Error saving your submission'
68 flash[:notice] = 'Error saving your submission'
69 elsif Task.create(:submission_id => @submission.id,
69 elsif Task.create(:submission_id => @submission.id,
70 :status => Task::STATUS_INQUEUE) == false
70 :status => Task::STATUS_INQUEUE) == false
71 flash[:notice] = 'Error adding your submission to task queue'
71 flash[:notice] = 'Error adding your submission to task queue'
72 end
72 end
73 else
73 else
74 prepare_list_information
74 prepare_list_information
75 render :action => 'list' and return
75 render :action => 'list' and return
76 end
76 end
77 redirect_to :action => 'list'
77 redirect_to :action => 'list'
78 end
78 end
79
79
80 def source
80 def source
81 submission = Submission.find(params[:id])
81 submission = Submission.find(params[:id])
82 if submission.user_id == session[:user_id]
82 if submission.user_id == session[:user_id]
83 send_data(submission.source,
83 send_data(submission.source,
84 {:filename => submission.download_filename,
84 {:filename => submission.download_filename,
85 :type => 'text/plain'})
85 :type => 'text/plain'})
86 else
86 else
87 flash[:notice] = 'Error viewing source'
87 flash[:notice] = 'Error viewing source'
88 redirect_to :action => 'list'
88 redirect_to :action => 'list'
89 end
89 end
90 end
90 end
91
91
92 def compiler_msg
92 def compiler_msg
93 @submission = Submission.find(params[:id])
93 @submission = Submission.find(params[:id])
94 if @submission.user_id == session[:user_id]
94 if @submission.user_id == session[:user_id]
95 render :action => 'compiler_msg', :layout => 'empty'
95 render :action => 'compiler_msg', :layout => 'empty'
96 else
96 else
97 flash[:notice] = 'Error viewing source'
97 flash[:notice] = 'Error viewing source'
98 redirect_to :action => 'list'
98 redirect_to :action => 'list'
99 end
99 end
100 end
100 end
101
101
102 def submission
102 def submission
103 @user = User.find(session[:user_id])
103 @user = User.find(session[:user_id])
104 @problems = Problem.find_available_problems
104 @problems = Problem.find_available_problems
105 if params[:id]==nil
105 if params[:id]==nil
106 @problem = nil
106 @problem = nil
107 @submissions = nil
107 @submissions = nil
108 else
108 else
109 @problem = Problem.find_by_name(params[:id])
109 @problem = Problem.find_by_name(params[:id])
110 if not @problem.available
110 if not @problem.available
111 redirect_to :action => 'list'
111 redirect_to :action => 'list'
112 flash[:notice] = 'Error: submissions for that problem are not viewable.'
112 flash[:notice] = 'Error: submissions for that problem are not viewable.'
113 return
113 return
114 end
114 end
115 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
115 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
116 end
116 end
117 end
117 end
118
118
119 def result
119 def result
120 if !Configuration.show_grading_result
120 if !Configuration.show_grading_result
121 redirect_to :action => 'list' and return
121 redirect_to :action => 'list' and return
122 end
122 end
123 @user = User.find(session[:user_id])
123 @user = User.find(session[:user_id])
124 @submission = Submission.find(params[:id])
124 @submission = Submission.find(params[:id])
125 if @submission.user!=@user
125 if @submission.user!=@user
126 flash[:notice] = 'You are not allowed to view result of other users.'
126 flash[:notice] = 'You are not allowed to view result of other users.'
127 redirect_to :action => 'list' and return
127 redirect_to :action => 'list' and return
128 end
128 end
129 prepare_grading_result(@submission)
129 prepare_grading_result(@submission)
130 end
130 end
131
131
132 def load_output
132 def load_output
133 if !Configuration.show_grading_result or params[:num]==nil
133 if !Configuration.show_grading_result or params[:num]==nil
134 redirect_to :action => 'list' and return
134 redirect_to :action => 'list' and return
135 end
135 end
136 @user = User.find(session[:user_id])
136 @user = User.find(session[:user_id])
137 @submission = Submission.find(params[:id])
137 @submission = Submission.find(params[:id])
138 if @submission.user!=@user
138 if @submission.user!=@user
139 flash[:notice] = 'You are not allowed to view result of other users.'
139 flash[:notice] = 'You are not allowed to view result of other users.'
140 redirect_to :action => 'list' and return
140 redirect_to :action => 'list' and return
141 end
141 end
142 case_num = params[:num].to_i
142 case_num = params[:num].to_i
143 out_filename = output_filename(@user.login,
143 out_filename = output_filename(@user.login,
144 @submission.problem.name,
144 @submission.problem.name,
145 @submission.id,
145 @submission.id,
146 case_num)
146 case_num)
147 if !FileTest.exists?(out_filename)
147 if !FileTest.exists?(out_filename)
148 flash[:notice] = 'Output not found.'
148 flash[:notice] = 'Output not found.'
149 redirect_to :action => 'list' and return
149 redirect_to :action => 'list' and return
150 end
150 end
151
151
152 response.headers['Content-Type'] = "application/force-download"
152 response.headers['Content-Type'] = "application/force-download"
153 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
153 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
154 response.headers["X-Sendfile"] = out_filename
154 response.headers["X-Sendfile"] = out_filename
155 response.headers['Content-length'] = File.size(out_filename)
155 response.headers['Content-length'] = File.size(out_filename)
156 render :nothing => true
156 render :nothing => true
157 end
157 end
158
158
159 def error
159 def error
160 @user = User.find(session[:user_id])
160 @user = User.find(session[:user_id])
161 end
161 end
162
162
163 # announcement refreshing and hiding methods
163 # announcement refreshing and hiding methods
164
164
165 def announcements
165 def announcements
166 if params.has_key? 'recent'
166 if params.has_key? 'recent'
167 prepare_announcements(params[:recent])
167 prepare_announcements(params[:recent])
168 else
168 else
169 prepare_announcements
169 prepare_announcements
170 end
170 end
171 render(:partial => 'announcement',
171 render(:partial => 'announcement',
172 :collection => @announcements,
172 :collection => @announcements,
173 :locals => {:announcement_effect => true})
173 :locals => {:announcement_effect => true})
174 end
174 end
175
175
176 #
176 #
177 # actions for Code Jom
177 # actions for Code Jom
178 #
178 #
179 def download_input
179 def download_input
180 problem = Problem.find(params[:id])
180 problem = Problem.find(params[:id])
181 user = User.find(session[:user_id])
181 user = User.find(session[:user_id])
182 if user.can_request_new_test_pair_for? problem
182 if user.can_request_new_test_pair_for? problem
183 assignment = user.get_new_test_pair_assignment_for problem
183 assignment = user.get_new_test_pair_assignment_for problem
184 assignment.save
184 assignment.save
185
185
186 send_data(assignment.test_pair.input,
186 send_data(assignment.test_pair.input,
187 { :filename => "#{problem.name}-#{assignment.request_number}.in",
187 { :filename => "#{problem.name}-#{assignment.request_number}.in",
188 :type => 'text/plain' })
188 :type => 'text/plain' })
189 else
189 else
190 recent_assignment = user.get_recent_test_pair_assignment_for problem
190 recent_assignment = user.get_recent_test_pair_assignment_for problem
191 send_data(recent_assignment.test_pair.input,
191 send_data(recent_assignment.test_pair.input,
192 { :filename => "#{problem.name}-#{recent_assignment.request_number}.in",
192 { :filename => "#{problem.name}-#{recent_assignment.request_number}.in",
193 :type => 'text/plain' })
193 :type => 'text/plain' })
194 end
194 end
195 end
195 end
196
196
197 def submit_solution
197 def submit_solution
198 problem = Problem.find(params[:id])
198 problem = Problem.find(params[:id])
199 user = User.find(session[:user_id])
199 user = User.find(session[:user_id])
200 recent_assignment = user.get_recent_test_pair_assignment_for problem
200 recent_assignment = user.get_recent_test_pair_assignment_for problem
201
201
202 if recent_assignment == nil
202 if recent_assignment == nil
203 flash[:notice] = 'You have not requested for any input data for this problem. Please download an input first.'
203 flash[:notice] = 'You have not requested for any input data for this problem. Please download an input first.'
204 session[:current_problem_id] = problem.id
204 session[:current_problem_id] = problem.id
205 redirect_to :action => 'list' and return
205 redirect_to :action => 'list' and return
206 end
206 end
207
207
208 if recent_assignment.expired?
208 if recent_assignment.expired?
209 flash[:notice] = 'The current input is expired. Please download a new input data.'
209 flash[:notice] = 'The current input is expired. Please download a new input data.'
210 session[:current_problem_id] = problem.id
210 session[:current_problem_id] = problem.id
211 redirect_to :action => 'list' and return
211 redirect_to :action => 'list' and return
212 end
212 end
213
213
214 if recent_assignment.submitted
214 if recent_assignment.submitted
215 flash[:notice] = 'You have already submitted an incorrect solution for this input. Please download a new input data.'
215 flash[:notice] = 'You have already submitted an incorrect solution for this input. Please download a new input data.'
216 session[:current_problem_id] = problem.id
216 session[:current_problem_id] = problem.id
217 redirect_to :action => 'list' and return
217 redirect_to :action => 'list' and return
218 end
218 end
219
219
220 if params[:file] == nil
220 if params[:file] == nil
221 flash[:notice] = 'You have not submitted any output.'
221 flash[:notice] = 'You have not submitted any output.'
222 session[:current_problem_id] = problem.id
222 session[:current_problem_id] = problem.id
223 redirect_to :action => 'list' and return
223 redirect_to :action => 'list' and return
224 end
224 end
225
225
226 submitted_solution = params[:file].read
226 submitted_solution = params[:file].read
227 test_pair = recent_assignment.test_pair
227 test_pair = recent_assignment.test_pair
228 passed = test_pair.grade(submitted_solution)
228 passed = test_pair.grade(submitted_solution)
229 points = passed ? 100 : 0
229 points = passed ? 100 : 0
230 submission = Submission.new(:user => user,
230 submission = Submission.new(:user => user,
231 :problem => problem,
231 :problem => problem,
232 :source => submitted_solution,
232 :source => submitted_solution,
233 :source_filename => params['file'].original_filename,
233 :source_filename => params['file'].original_filename,
234 :language_id => 0,
234 :language_id => 0,
235 :submitted_at => Time.new.gmtime,
235 :submitted_at => Time.new.gmtime,
236 :graded_at => Time.new.gmtime,
236 :graded_at => Time.new.gmtime,
237 :points => points)
237 :points => points)
238 submission.save
238 submission.save
239 recent_assignment.submitted = true
239 recent_assignment.submitted = true
240 recent_assignment.save
240 recent_assignment.save
241
241
242 status = user.get_submission_status_for(problem)
242 status = user.get_submission_status_for(problem)
243 if status == nil
243 if status == nil
244 status = SubmissionStatus.new :user => user, :problem => problem, :submission_count => 0
244 status = SubmissionStatus.new :user => user, :problem => problem, :submission_count => 0
245 end
245 end
246
246
247 status.submission_count += 1
247 status.submission_count += 1
248 status.passed = passed
248 status.passed = passed
249 status.save
249 status.save
250
250
251 if passed
251 if passed
252 flash[:notice] = 'Correct solution.'
252 flash[:notice] = 'Correct solution.'
253 user.update_codejom_status
253 user.update_codejom_status
254 else
254 else
255 session[:current_problem_id] = problem.id
255 session[:current_problem_id] = problem.id
256 flash[:notice] = 'Incorrect solution.'
256 flash[:notice] = 'Incorrect solution.'
257 end
257 end
258 redirect_to :action => 'list'
258 redirect_to :action => 'list'
259 end
259 end
260
260
261 protected
261 protected
262
262
263 def prepare_announcements(recent=nil)
263 def prepare_announcements(recent=nil)
264 if Configuration.show_tasks_to?(@user)
264 if Configuration.show_tasks_to?(@user)
265 @announcements = Announcement.find_published(true)
265 @announcements = Announcement.find_published(true)
266 else
266 else
267 @announcements = Announcement.find_published
267 @announcements = Announcement.find_published
268 end
268 end
269 if recent!=nil
269 if recent!=nil
270 recent_id = recent.to_i
270 recent_id = recent.to_i
271 @announcements = @announcements.find_all { |a| a.id > recent_id }
271 @announcements = @announcements.find_all { |a| a.id > recent_id }
272 end
272 end
273 end
273 end
274
274
275 def prepare_timeout_information(problems)
275 def prepare_timeout_information(problems)
276 @submission_timeouts = {}
276 @submission_timeouts = {}
277 problems.each do |problem|
277 problems.each do |problem|
278 assignment = @user.get_recent_test_pair_assignment_for(problem)
278 assignment = @user.get_recent_test_pair_assignment_for(problem)
279 if assignment == nil
279 if assignment == nil
280 timeout = nil
280 timeout = nil
281 else
281 else
282 if assignment.expired?
282 if assignment.expired?
283 timeout = 0
283 timeout = 0
284 else
284 else
285 timeout = assignment.created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION - Time.new.gmtime
285 timeout = assignment.created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION - Time.new.gmtime
286 end
286 end
287 end
287 end
288 @submission_timeouts[problem.id] = timeout
288 @submission_timeouts[problem.id] = timeout
289 end
289 end
290 @submission_timeouts.each_pair {|k,v| puts "#{k} => #{v}"}
290 @submission_timeouts.each_pair {|k,v| puts "#{k} => #{v}"}
291 end
291 end
292
292
293 def prepare_list_information
293 def prepare_list_information
294 @user = User.find(session[:user_id])
294 @user = User.find(session[:user_id])
295
295
296 all_problems = Problem.find_available_problems
296 all_problems = Problem.find_available_problems
297
297
298 passed = {}
298 passed = {}
299 sub_count = {}
299 sub_count = {}
300 @user.submission_statuses.each do |status|
300 @user.submission_statuses.each do |status|
301 if status.passed
301 if status.passed
302 passed[status.problem_id] = true
302 passed[status.problem_id] = true
303 end
303 end
304 sub_count[status.problem_id] = status.submission_count
304 sub_count[status.problem_id] = status.submission_count
305 end
305 end
306
306
307 if session.has_key? :current_problem_id
307 if session.has_key? :current_problem_id
308 @current_problem_id = session[:current_problem_id]
308 @current_problem_id = session[:current_problem_id]
309 session.delete(:current_problem_id)
309 session.delete(:current_problem_id)
310 else
310 else
311 @current_problem_id = nil
311 @current_problem_id = nil
312 end
312 end
313
313
314 @problems = all_problems.reject { |problem| passed.has_key? problem.id }
314 @problems = all_problems.reject { |problem| passed.has_key? problem.id }
315
315
316 prepare_timeout_information(@problems)
316 prepare_timeout_information(@problems)
317
317
318 @prob_submissions = Array.new
318 @prob_submissions = Array.new
319 @problems.each do |p|
319 @problems.each do |p|
320 if sub_count.has_key? p.id
320 if sub_count.has_key? p.id
321 @prob_submissions << { :count => sub_count[p.id] }
321 @prob_submissions << { :count => sub_count[p.id] }
322 else
322 else
323 @prob_submissions << { :count => 0 }
323 @prob_submissions << { :count => 0 }
324 end
324 end
325 end
325 end
326 prepare_announcements
326 prepare_announcements
327 end
327 end
328
328
329 def check_viewability
329 def check_viewability
330 @user = User.find(session[:user_id])
330 @user = User.find(session[:user_id])
331 if (!Configuration.show_tasks_to?(@user)) and
331 if (!Configuration.show_tasks_to?(@user)) and
332 ((action_name=='submission') or (action_name=='submit'))
332 ((action_name=='submission') or (action_name=='submit'))
333 redirect_to :action => 'list' and return
333 redirect_to :action => 'list' and return
334 end
334 end
335 end
335 end
336
336
337 def prepare_grading_result(submission)
337 def prepare_grading_result(submission)
338 if Configuration.task_grading_info.has_key? submission.problem.name
338 if Configuration.task_grading_info.has_key? submission.problem.name
339 grading_info = Configuration.task_grading_info[submission.problem.name]
339 grading_info = Configuration.task_grading_info[submission.problem.name]
340 else
340 else
341 # guess task info from problem.full_score
341 # guess task info from problem.full_score
342 cases = submission.problem.full_score / 10
342 cases = submission.problem.full_score / 10
343 grading_info = {
343 grading_info = {
344 'testruns' => cases,
344 'testruns' => cases,
345 'testcases' => cases
345 'testcases' => cases
346 }
346 }
347 end
347 end
348 @test_runs = []
348 @test_runs = []
349 if grading_info['testruns'].is_a? Integer
349 if grading_info['testruns'].is_a? Integer
350 trun_count = grading_info['testruns']
350 trun_count = grading_info['testruns']
351 trun_count.times do |i|
351 trun_count.times do |i|
352 @test_runs << [ read_grading_result(@user.login,
352 @test_runs << [ read_grading_result(@user.login,
353 submission.problem.name,
353 submission.problem.name,
354 submission.id,
354 submission.id,
355 i+1) ]
355 i+1) ]
356 end
356 end
357 else
357 else
358 grading_info['testruns'].keys.sort.each do |num|
358 grading_info['testruns'].keys.sort.each do |num|
359 run = []
359 run = []
360 testrun = grading_info['testruns'][num]
360 testrun = grading_info['testruns'][num]
361 testrun.each do |c|
361 testrun.each do |c|
362 run << read_grading_result(@user.login,
362 run << read_grading_result(@user.login,
363 submission.problem.name,
363 submission.problem.name,
364 submission.id,
364 submission.id,
365 c)
365 c)
366 end
366 end
367 @test_runs << run
367 @test_runs << run
368 end
368 end
369 end
369 end
370 end
370 end
371
371
372 def grading_result_dir(user_name, problem_name, submission_id, case_num)
372 def grading_result_dir(user_name, problem_name, submission_id, case_num)
373 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
373 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
374 end
374 end
375
375
376 def output_filename(user_name, problem_name, submission_id, case_num)
376 def output_filename(user_name, problem_name, submission_id, case_num)
377 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
377 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
378 return "#{dir}/output.txt"
378 return "#{dir}/output.txt"
379 end
379 end
380
380
381 def read_grading_result(user_name, problem_name, submission_id, case_num)
381 def read_grading_result(user_name, problem_name, submission_id, case_num)
382 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
382 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
383 result_file_name = "#{dir}/result"
383 result_file_name = "#{dir}/result"
384 if !FileTest.exists?(result_file_name)
384 if !FileTest.exists?(result_file_name)
385 return {:num => case_num, :msg => 'program did not run'}
385 return {:num => case_num, :msg => 'program did not run'}
386 else
386 else
387 results = File.open(result_file_name).readlines
387 results = File.open(result_file_name).readlines
388 run_stat = extract_running_stat(results)
388 run_stat = extract_running_stat(results)
389 output_filename = "#{dir}/output.txt"
389 output_filename = "#{dir}/output.txt"
390 if FileTest.exists?(output_filename)
390 if FileTest.exists?(output_filename)
391 output_file = true
391 output_file = true
392 output_size = File.size(output_filename)
392 output_size = File.size(output_filename)
393 else
393 else
394 output_file = false
394 output_file = false
395 output_size = 0
395 output_size = 0
396 end
396 end
397
397
398 return {
398 return {
399 :num => case_num,
399 :num => case_num,
400 :msg => results[0],
400 :msg => results[0],
401 :run_stat => run_stat,
401 :run_stat => run_stat,
402 :output => output_file,
402 :output => output_file,
403 :output_size => output_size
403 :output_size => output_size
404 }
404 }
405 end
405 end
406 end
406 end
407
407
408 # copied from grader/script/lib/test_request_helper.rb
408 # copied from grader/script/lib/test_request_helper.rb
You need to be logged in to leave comments. Login now