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

r250:6820e08d6a38 - - 1 file changed: 10 inserted, 0 deleted

@@ -1,199 +1,203
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 append_before_filter :update_user_start_time, :except => [:index, :login]
6 append_before_filter :update_user_start_time, :except => [:index, :login]
7
7
8 + # to prevent log in box to be shown when user logged out of the
9 + # system only in some tab
10 + prepend_before_filter :reject_announcement_refresh_when_logged_out, :only => [:announcements]
11 +
8 # COMMENTED OUT: filter in each action instead
12 # COMMENTED OUT: filter in each action instead
9 # before_filter :verify_time_limit, :only => [:submit]
13 # before_filter :verify_time_limit, :only => [:submit]
10
14
11 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
15 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
12 :redirect_to => { :action => :index }
16 :redirect_to => { :action => :index }
13
17
14 # COMMENT OUT: only need when having high load
18 # COMMENT OUT: only need when having high load
15 # caches_action :index, :login
19 # caches_action :index, :login
16
20
17 # NOTE: This method is not actually needed, 'config/routes.rb' has
21 # NOTE: This method is not actually needed, 'config/routes.rb' has
18 # assigned action login as a default action.
22 # assigned action login as a default action.
19 def index
23 def index
20 redirect_to :action => 'login'
24 redirect_to :action => 'login'
21 end
25 end
22
26
23 def login
27 def login
24 saved_notice = flash[:notice]
28 saved_notice = flash[:notice]
25 reset_session
29 reset_session
26 flash.now[:notice] = saved_notice
30 flash.now[:notice] = saved_notice
27
31
28 # EXPERIMENT:
32 # EXPERIMENT:
29 # Hide login if in single user mode and the url does not
33 # Hide login if in single user mode and the url does not
30 # explicitly specify /login
34 # explicitly specify /login
31 #
35 #
32 # logger.info "PATH: #{request.path}"
36 # logger.info "PATH: #{request.path}"
33 # if Configuration['system.single_user_mode'] and
37 # if Configuration['system.single_user_mode'] and
34 # request.path!='/main/login'
38 # request.path!='/main/login'
35 # @hidelogin = true
39 # @hidelogin = true
36 # end
40 # end
37
41
38 @announcements = Announcement.find_for_frontpage
42 @announcements = Announcement.find_for_frontpage
39 render :action => 'login', :layout => 'empty'
43 render :action => 'login', :layout => 'empty'
40 end
44 end
41
45
42 def list
46 def list
43 prepare_list_information
47 prepare_list_information
44 end
48 end
45
49
46 def help
50 def help
47 @user = User.find(session[:user_id])
51 @user = User.find(session[:user_id])
48 end
52 end
49
53
50 def submit
54 def submit
51 user = User.find(session[:user_id])
55 user = User.find(session[:user_id])
52
56
53 @submission = Submission.new(params[:submission])
57 @submission = Submission.new(params[:submission])
54 @submission.user = user
58 @submission.user = user
55 @submission.language_id = 0
59 @submission.language_id = 0
56 if (params['file']) and (params['file']!='')
60 if (params['file']) and (params['file']!='')
57 @submission.source = params['file'].read
61 @submission.source = params['file'].read
58 @submission.source_filename = params['file'].original_filename
62 @submission.source_filename = params['file'].original_filename
59 end
63 end
60 @submission.submitted_at = Time.new.gmtime
64 @submission.submitted_at = Time.new.gmtime
61
65
62 if Configuration.time_limit_mode? and user.contest_finished?
66 if Configuration.time_limit_mode? and user.contest_finished?
63 @submission.errors.add_to_base "The contest is over."
67 @submission.errors.add_to_base "The contest is over."
64 prepare_list_information
68 prepare_list_information
65 render :action => 'list' and return
69 render :action => 'list' and return
66 end
70 end
67
71
68 if @submission.valid?
72 if @submission.valid?
69 if @submission.save == false
73 if @submission.save == false
70 flash[:notice] = 'Error saving your submission'
74 flash[:notice] = 'Error saving your submission'
71 elsif Task.create(:submission_id => @submission.id,
75 elsif Task.create(:submission_id => @submission.id,
72 :status => Task::STATUS_INQUEUE) == false
76 :status => Task::STATUS_INQUEUE) == false
73 flash[:notice] = 'Error adding your submission to task queue'
77 flash[:notice] = 'Error adding your submission to task queue'
74 end
78 end
75 else
79 else
76 prepare_list_information
80 prepare_list_information
77 render :action => 'list' and return
81 render :action => 'list' and return
78 end
82 end
79 redirect_to :action => 'list'
83 redirect_to :action => 'list'
80 end
84 end
81
85
82 def source
86 def source
83 submission = Submission.find(params[:id])
87 submission = Submission.find(params[:id])
84 if submission.user_id == session[:user_id]
88 if submission.user_id == session[:user_id]
85 send_data(submission.source,
89 send_data(submission.source,
86 {:filename => submission.download_filename,
90 {:filename => submission.download_filename,
87 :type => 'text/plain'})
91 :type => 'text/plain'})
88 else
92 else
89 flash[:notice] = 'Error viewing source'
93 flash[:notice] = 'Error viewing source'
90 redirect_to :action => 'list'
94 redirect_to :action => 'list'
91 end
95 end
92 end
96 end
93
97
94 def compiler_msg
98 def compiler_msg
95 @submission = Submission.find(params[:id])
99 @submission = Submission.find(params[:id])
96 if @submission.user_id == session[:user_id]
100 if @submission.user_id == session[:user_id]
97 render :action => 'compiler_msg', :layout => 'empty'
101 render :action => 'compiler_msg', :layout => 'empty'
98 else
102 else
99 flash[:notice] = 'Error viewing source'
103 flash[:notice] = 'Error viewing source'
100 redirect_to :action => 'list'
104 redirect_to :action => 'list'
101 end
105 end
102 end
106 end
103
107
104 def submission
108 def submission
105 # protect the action for Code Jom
109 # protect the action for Code Jom
106 redirect_to :action => 'list'
110 redirect_to :action => 'list'
107
111
108 @user = User.find(session[:user_id])
112 @user = User.find(session[:user_id])
109 @problems = Problem.find_available_problems
113 @problems = Problem.find_available_problems
110 if params[:id]==nil
114 if params[:id]==nil
111 @problem = nil
115 @problem = nil
112 @submissions = nil
116 @submissions = nil
113 else
117 else
114 @problem = Problem.find_by_name(params[:id])
118 @problem = Problem.find_by_name(params[:id])
115 if not @problem.available
119 if not @problem.available
116 redirect_to :action => 'list'
120 redirect_to :action => 'list'
117 flash[:notice] = 'Error: submissions for that problem are not viewable.'
121 flash[:notice] = 'Error: submissions for that problem are not viewable.'
118 return
122 return
119 end
123 end
120 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
124 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
121 end
125 end
122 end
126 end
123
127
124 def result
128 def result
125 if !Configuration.show_grading_result
129 if !Configuration.show_grading_result
126 redirect_to :action => 'list' and return
130 redirect_to :action => 'list' and return
127 end
131 end
128 @user = User.find(session[:user_id])
132 @user = User.find(session[:user_id])
129 @submission = Submission.find(params[:id])
133 @submission = Submission.find(params[:id])
130 if @submission.user!=@user
134 if @submission.user!=@user
131 flash[:notice] = 'You are not allowed to view result of other users.'
135 flash[:notice] = 'You are not allowed to view result of other users.'
132 redirect_to :action => 'list' and return
136 redirect_to :action => 'list' and return
133 end
137 end
134 prepare_grading_result(@submission)
138 prepare_grading_result(@submission)
135 end
139 end
136
140
137 def load_output
141 def load_output
138 if !Configuration.show_grading_result or params[:num]==nil
142 if !Configuration.show_grading_result or params[:num]==nil
139 redirect_to :action => 'list' and return
143 redirect_to :action => 'list' and return
140 end
144 end
141 @user = User.find(session[:user_id])
145 @user = User.find(session[:user_id])
142 @submission = Submission.find(params[:id])
146 @submission = Submission.find(params[:id])
143 if @submission.user!=@user
147 if @submission.user!=@user
144 flash[:notice] = 'You are not allowed to view result of other users.'
148 flash[:notice] = 'You are not allowed to view result of other users.'
145 redirect_to :action => 'list' and return
149 redirect_to :action => 'list' and return
146 end
150 end
147 case_num = params[:num].to_i
151 case_num = params[:num].to_i
148 out_filename = output_filename(@user.login,
152 out_filename = output_filename(@user.login,
149 @submission.problem.name,
153 @submission.problem.name,
150 @submission.id,
154 @submission.id,
151 case_num)
155 case_num)
152 if !FileTest.exists?(out_filename)
156 if !FileTest.exists?(out_filename)
153 flash[:notice] = 'Output not found.'
157 flash[:notice] = 'Output not found.'
154 redirect_to :action => 'list' and return
158 redirect_to :action => 'list' and return
155 end
159 end
156
160
157 response.headers['Content-Type'] = "application/force-download"
161 response.headers['Content-Type'] = "application/force-download"
158 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
162 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
159 response.headers["X-Sendfile"] = out_filename
163 response.headers["X-Sendfile"] = out_filename
160 response.headers['Content-length'] = File.size(out_filename)
164 response.headers['Content-length'] = File.size(out_filename)
161 render :nothing => true
165 render :nothing => true
162 end
166 end
163
167
164 def error
168 def error
165 @user = User.find(session[:user_id])
169 @user = User.find(session[:user_id])
166 end
170 end
167
171
168 # announcement refreshing and hiding methods
172 # announcement refreshing and hiding methods
169
173
170 def announcements
174 def announcements
171 if params.has_key? 'recent'
175 if params.has_key? 'recent'
172 prepare_announcements(params[:recent])
176 prepare_announcements(params[:recent])
173 else
177 else
174 prepare_announcements
178 prepare_announcements
175 end
179 end
176 render(:partial => 'announcement',
180 render(:partial => 'announcement',
177 :collection => @announcements,
181 :collection => @announcements,
178 :locals => {:announcement_effect => true})
182 :locals => {:announcement_effect => true})
179 end
183 end
180
184
181 #
185 #
182 # actions for Code Jom
186 # actions for Code Jom
183 #
187 #
184 def download_input
188 def download_input
185 user = User.find(session[:user_id])
189 user = User.find(session[:user_id])
186
190
187 if Configuration.time_limit_mode? and user.contest_finished?
191 if Configuration.time_limit_mode? and user.contest_finished?
188 redirect_to :action => 'list' and return
192 redirect_to :action => 'list' and return
189 end
193 end
190
194
191 problem = Problem.find(params[:id])
195 problem = Problem.find(params[:id])
192 if user.can_request_new_test_pair_for? problem
196 if user.can_request_new_test_pair_for? problem
193 assignment = user.get_new_test_pair_assignment_for problem
197 assignment = user.get_new_test_pair_assignment_for problem
194 assignment.save
198 assignment.save
195
199
196 send_data(assignment.test_pair.input,
200 send_data(assignment.test_pair.input,
197 { :filename => "#{problem.name}-#{assignment.request_number}.in",
201 { :filename => "#{problem.name}-#{assignment.request_number}.in",
198 :type => 'text/plain' })
202 :type => 'text/plain' })
199 else
203 else
@@ -273,194 +277,200
273 end
277 end
274
278
275 protected
279 protected
276
280
277 def prepare_announcements(recent=nil)
281 def prepare_announcements(recent=nil)
278 if Configuration.show_tasks_to?(@user)
282 if Configuration.show_tasks_to?(@user)
279 @announcements = Announcement.find_published(true)
283 @announcements = Announcement.find_published(true)
280 else
284 else
281 @announcements = Announcement.find_published
285 @announcements = Announcement.find_published
282 end
286 end
283 if recent!=nil
287 if recent!=nil
284 recent_id = recent.to_i
288 recent_id = recent.to_i
285 @announcements = @announcements.find_all { |a| a.id > recent_id }
289 @announcements = @announcements.find_all { |a| a.id > recent_id }
286 end
290 end
287 end
291 end
288
292
289 def prepare_timeout_information(problems)
293 def prepare_timeout_information(problems)
290 @submission_timeouts = {}
294 @submission_timeouts = {}
291 problems.each do |problem|
295 problems.each do |problem|
292 assignment = @user.get_recent_test_pair_assignment_for(problem)
296 assignment = @user.get_recent_test_pair_assignment_for(problem)
293 if assignment == nil
297 if assignment == nil
294 timeout = nil
298 timeout = nil
295 else
299 else
296 if (assignment.expired?) or (assignment.submitted)
300 if (assignment.expired?) or (assignment.submitted)
297 timeout = 0
301 timeout = 0
298 else
302 else
299 timeout = assignment.created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION - Time.new.gmtime
303 timeout = assignment.created_at + TEST_ASSIGNMENT_EXPIRATION_DURATION - Time.new.gmtime
300 end
304 end
301 end
305 end
302 @submission_timeouts[problem.id] = timeout
306 @submission_timeouts[problem.id] = timeout
303 end
307 end
304 @submission_timeouts.each_pair {|k,v| puts "#{k} => #{v}"}
308 @submission_timeouts.each_pair {|k,v| puts "#{k} => #{v}"}
305 end
309 end
306
310
307 def prepare_list_information
311 def prepare_list_information
308 @user = User.find(session[:user_id])
312 @user = User.find(session[:user_id])
309
313
310 all_problems = Problem.find_available_problems
314 all_problems = Problem.find_available_problems
311
315
312 passed = {}
316 passed = {}
313 sub_count = {}
317 sub_count = {}
314 @user.submission_statuses.each do |status|
318 @user.submission_statuses.each do |status|
315 if status.passed
319 if status.passed
316 passed[status.problem_id] = true
320 passed[status.problem_id] = true
317 end
321 end
318 sub_count[status.problem_id] = status.submission_count
322 sub_count[status.problem_id] = status.submission_count
319 end
323 end
320
324
321 if session.has_key? :current_problem_id
325 if session.has_key? :current_problem_id
322 @current_problem_id = session[:current_problem_id]
326 @current_problem_id = session[:current_problem_id]
323 session.delete(:current_problem_id)
327 session.delete(:current_problem_id)
324 else
328 else
325 @current_problem_id = nil
329 @current_problem_id = nil
326 end
330 end
327
331
328 @problems = all_problems.reject { |problem| passed.has_key? problem.id }
332 @problems = all_problems.reject { |problem| passed.has_key? problem.id }
329
333
330 prepare_timeout_information(@problems)
334 prepare_timeout_information(@problems)
331
335
332 @prob_submissions = Array.new
336 @prob_submissions = Array.new
333 @problems.each do |p|
337 @problems.each do |p|
334 if sub_count.has_key? p.id
338 if sub_count.has_key? p.id
335 @prob_submissions << { :count => sub_count[p.id] }
339 @prob_submissions << { :count => sub_count[p.id] }
336 else
340 else
337 @prob_submissions << { :count => 0 }
341 @prob_submissions << { :count => 0 }
338 end
342 end
339 end
343 end
340 prepare_announcements
344 prepare_announcements
341 end
345 end
342
346
343 def check_viewability
347 def check_viewability
344 @user = User.find(session[:user_id])
348 @user = User.find(session[:user_id])
345 if (!Configuration.show_tasks_to?(@user)) and
349 if (!Configuration.show_tasks_to?(@user)) and
346 ((action_name=='submission') or (action_name=='submit'))
350 ((action_name=='submission') or (action_name=='submit'))
347 redirect_to :action => 'list' and return
351 redirect_to :action => 'list' and return
348 end
352 end
349 end
353 end
350
354
351 def prepare_grading_result(submission)
355 def prepare_grading_result(submission)
352 if Configuration.task_grading_info.has_key? submission.problem.name
356 if Configuration.task_grading_info.has_key? submission.problem.name
353 grading_info = Configuration.task_grading_info[submission.problem.name]
357 grading_info = Configuration.task_grading_info[submission.problem.name]
354 else
358 else
355 # guess task info from problem.full_score
359 # guess task info from problem.full_score
356 cases = submission.problem.full_score / 10
360 cases = submission.problem.full_score / 10
357 grading_info = {
361 grading_info = {
358 'testruns' => cases,
362 'testruns' => cases,
359 'testcases' => cases
363 'testcases' => cases
360 }
364 }
361 end
365 end
362 @test_runs = []
366 @test_runs = []
363 if grading_info['testruns'].is_a? Integer
367 if grading_info['testruns'].is_a? Integer
364 trun_count = grading_info['testruns']
368 trun_count = grading_info['testruns']
365 trun_count.times do |i|
369 trun_count.times do |i|
366 @test_runs << [ read_grading_result(@user.login,
370 @test_runs << [ read_grading_result(@user.login,
367 submission.problem.name,
371 submission.problem.name,
368 submission.id,
372 submission.id,
369 i+1) ]
373 i+1) ]
370 end
374 end
371 else
375 else
372 grading_info['testruns'].keys.sort.each do |num|
376 grading_info['testruns'].keys.sort.each do |num|
373 run = []
377 run = []
374 testrun = grading_info['testruns'][num]
378 testrun = grading_info['testruns'][num]
375 testrun.each do |c|
379 testrun.each do |c|
376 run << read_grading_result(@user.login,
380 run << read_grading_result(@user.login,
377 submission.problem.name,
381 submission.problem.name,
378 submission.id,
382 submission.id,
379 c)
383 c)
380 end
384 end
381 @test_runs << run
385 @test_runs << run
382 end
386 end
383 end
387 end
384 end
388 end
385
389
386 def grading_result_dir(user_name, problem_name, submission_id, case_num)
390 def grading_result_dir(user_name, problem_name, submission_id, case_num)
387 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
391 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
388 end
392 end
389
393
390 def output_filename(user_name, problem_name, submission_id, case_num)
394 def output_filename(user_name, problem_name, submission_id, case_num)
391 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
395 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
392 return "#{dir}/output.txt"
396 return "#{dir}/output.txt"
393 end
397 end
394
398
395 def read_grading_result(user_name, problem_name, submission_id, case_num)
399 def read_grading_result(user_name, problem_name, submission_id, case_num)
396 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
400 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
397 result_file_name = "#{dir}/result"
401 result_file_name = "#{dir}/result"
398 if !FileTest.exists?(result_file_name)
402 if !FileTest.exists?(result_file_name)
399 return {:num => case_num, :msg => 'program did not run'}
403 return {:num => case_num, :msg => 'program did not run'}
400 else
404 else
401 results = File.open(result_file_name).readlines
405 results = File.open(result_file_name).readlines
402 run_stat = extract_running_stat(results)
406 run_stat = extract_running_stat(results)
403 output_filename = "#{dir}/output.txt"
407 output_filename = "#{dir}/output.txt"
404 if FileTest.exists?(output_filename)
408 if FileTest.exists?(output_filename)
405 output_file = true
409 output_file = true
406 output_size = File.size(output_filename)
410 output_size = File.size(output_filename)
407 else
411 else
408 output_file = false
412 output_file = false
409 output_size = 0
413 output_size = 0
410 end
414 end
411
415
412 return {
416 return {
413 :num => case_num,
417 :num => case_num,
414 :msg => results[0],
418 :msg => results[0],
415 :run_stat => run_stat,
419 :run_stat => run_stat,
416 :output => output_file,
420 :output => output_file,
417 :output_size => output_size
421 :output_size => output_size
418 }
422 }
419 end
423 end
420 end
424 end
421
425
422 # copied from grader/script/lib/test_request_helper.rb
426 # copied from grader/script/lib/test_request_helper.rb
423 def extract_running_stat(results)
427 def extract_running_stat(results)
424 running_stat_line = results[-1]
428 running_stat_line = results[-1]
425
429
426 # extract exit status line
430 # extract exit status line
427 run_stat = ""
431 run_stat = ""
428 if !(/[Cc]orrect/.match(results[0]))
432 if !(/[Cc]orrect/.match(results[0]))
429 run_stat = results[0].chomp
433 run_stat = results[0].chomp
430 else
434 else
431 run_stat = 'Program exited normally'
435 run_stat = 'Program exited normally'
432 end
436 end
433
437
434 logger.info "Stat line: #{running_stat_line}"
438 logger.info "Stat line: #{running_stat_line}"
435
439
436 # extract running time
440 # extract running time
437 if res = /r(.*)u(.*)s/.match(running_stat_line)
441 if res = /r(.*)u(.*)s/.match(running_stat_line)
438 seconds = (res[1].to_f + res[2].to_f)
442 seconds = (res[1].to_f + res[2].to_f)
439 time_stat = "Time used: #{seconds} sec."
443 time_stat = "Time used: #{seconds} sec."
440 else
444 else
441 seconds = nil
445 seconds = nil
442 time_stat = "Time used: n/a sec."
446 time_stat = "Time used: n/a sec."
443 end
447 end
444
448
445 # extract memory usage
449 # extract memory usage
446 if res = /s(.*)m/.match(running_stat_line)
450 if res = /s(.*)m/.match(running_stat_line)
447 memory_used = res[1].to_i
451 memory_used = res[1].to_i
448 else
452 else
449 memory_used = -1
453 memory_used = -1
450 end
454 end
451
455
452 return {
456 return {
453 :msg => "#{run_stat}\n#{time_stat}",
457 :msg => "#{run_stat}\n#{time_stat}",
454 :running_time => seconds,
458 :running_time => seconds,
455 :exit_status => run_stat,
459 :exit_status => run_stat,
456 :memory_usage => memory_used
460 :memory_usage => memory_used
457 }
461 }
458 end
462 end
459
463
460 def update_user_start_time
464 def update_user_start_time
461 user = User.find(session[:user_id])
465 user = User.find(session[:user_id])
462 UserContestStat.update_user_start_time(user)
466 UserContestStat.update_user_start_time(user)
463 end
467 end
464
468
469 + def reject_announcement_refresh_when_logged_out
470 + if not session[:user_id]
471 + render :text => 'Access forbidden', :status => 403
472 + end
473 + end
474 +
465 end
475 end
466
476
You need to be logged in to leave comments. Login now