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,103 +1,107
1 1 class MainController < ApplicationController
2 2
3 3 before_filter :authenticate, :except => [:index, :login]
4 4 before_filter :check_viewability, :except => [:index, :login]
5 5
6 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 12 # COMMENTED OUT: filter in each action instead
9 13 # before_filter :verify_time_limit, :only => [:submit]
10 14
11 15 verify :method => :post, :only => [:submit, :download_input, :submit_solution],
12 16 :redirect_to => { :action => :index }
13 17
14 18 # COMMENT OUT: only need when having high load
15 19 # caches_action :index, :login
16 20
17 21 # NOTE: This method is not actually needed, 'config/routes.rb' has
18 22 # assigned action login as a default action.
19 23 def index
20 24 redirect_to :action => 'login'
21 25 end
22 26
23 27 def login
24 28 saved_notice = flash[:notice]
25 29 reset_session
26 30 flash.now[:notice] = saved_notice
27 31
28 32 # EXPERIMENT:
29 33 # Hide login if in single user mode and the url does not
30 34 # explicitly specify /login
31 35 #
32 36 # logger.info "PATH: #{request.path}"
33 37 # if Configuration['system.single_user_mode'] and
34 38 # request.path!='/main/login'
35 39 # @hidelogin = true
36 40 # end
37 41
38 42 @announcements = Announcement.find_for_frontpage
39 43 render :action => 'login', :layout => 'empty'
40 44 end
41 45
42 46 def list
43 47 prepare_list_information
44 48 end
45 49
46 50 def help
47 51 @user = User.find(session[:user_id])
48 52 end
49 53
50 54 def submit
51 55 user = User.find(session[:user_id])
52 56
53 57 @submission = Submission.new(params[:submission])
54 58 @submission.user = user
55 59 @submission.language_id = 0
56 60 if (params['file']) and (params['file']!='')
57 61 @submission.source = params['file'].read
58 62 @submission.source_filename = params['file'].original_filename
59 63 end
60 64 @submission.submitted_at = Time.new.gmtime
61 65
62 66 if Configuration.time_limit_mode? and user.contest_finished?
63 67 @submission.errors.add_to_base "The contest is over."
64 68 prepare_list_information
65 69 render :action => 'list' and return
66 70 end
67 71
68 72 if @submission.valid?
69 73 if @submission.save == false
70 74 flash[:notice] = 'Error saving your submission'
71 75 elsif Task.create(:submission_id => @submission.id,
72 76 :status => Task::STATUS_INQUEUE) == false
73 77 flash[:notice] = 'Error adding your submission to task queue'
74 78 end
75 79 else
76 80 prepare_list_information
77 81 render :action => 'list' and return
78 82 end
79 83 redirect_to :action => 'list'
80 84 end
81 85
82 86 def source
83 87 submission = Submission.find(params[:id])
84 88 if submission.user_id == session[:user_id]
85 89 send_data(submission.source,
86 90 {:filename => submission.download_filename,
87 91 :type => 'text/plain'})
88 92 else
89 93 flash[:notice] = 'Error viewing source'
90 94 redirect_to :action => 'list'
91 95 end
92 96 end
93 97
94 98 def compiler_msg
95 99 @submission = Submission.find(params[:id])
96 100 if @submission.user_id == session[:user_id]
97 101 render :action => 'compiler_msg', :layout => 'empty'
98 102 else
99 103 flash[:notice] = 'Error viewing source'
100 104 redirect_to :action => 'list'
101 105 end
102 106 end
103 107
@@ -369,98 +373,104
369 373 i+1) ]
370 374 end
371 375 else
372 376 grading_info['testruns'].keys.sort.each do |num|
373 377 run = []
374 378 testrun = grading_info['testruns'][num]
375 379 testrun.each do |c|
376 380 run << read_grading_result(@user.login,
377 381 submission.problem.name,
378 382 submission.id,
379 383 c)
380 384 end
381 385 @test_runs << run
382 386 end
383 387 end
384 388 end
385 389
386 390 def grading_result_dir(user_name, problem_name, submission_id, case_num)
387 391 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
388 392 end
389 393
390 394 def output_filename(user_name, problem_name, submission_id, case_num)
391 395 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
392 396 return "#{dir}/output.txt"
393 397 end
394 398
395 399 def read_grading_result(user_name, problem_name, submission_id, case_num)
396 400 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
397 401 result_file_name = "#{dir}/result"
398 402 if !FileTest.exists?(result_file_name)
399 403 return {:num => case_num, :msg => 'program did not run'}
400 404 else
401 405 results = File.open(result_file_name).readlines
402 406 run_stat = extract_running_stat(results)
403 407 output_filename = "#{dir}/output.txt"
404 408 if FileTest.exists?(output_filename)
405 409 output_file = true
406 410 output_size = File.size(output_filename)
407 411 else
408 412 output_file = false
409 413 output_size = 0
410 414 end
411 415
412 416 return {
413 417 :num => case_num,
414 418 :msg => results[0],
415 419 :run_stat => run_stat,
416 420 :output => output_file,
417 421 :output_size => output_size
418 422 }
419 423 end
420 424 end
421 425
422 426 # copied from grader/script/lib/test_request_helper.rb
423 427 def extract_running_stat(results)
424 428 running_stat_line = results[-1]
425 429
426 430 # extract exit status line
427 431 run_stat = ""
428 432 if !(/[Cc]orrect/.match(results[0]))
429 433 run_stat = results[0].chomp
430 434 else
431 435 run_stat = 'Program exited normally'
432 436 end
433 437
434 438 logger.info "Stat line: #{running_stat_line}"
435 439
436 440 # extract running time
437 441 if res = /r(.*)u(.*)s/.match(running_stat_line)
438 442 seconds = (res[1].to_f + res[2].to_f)
439 443 time_stat = "Time used: #{seconds} sec."
440 444 else
441 445 seconds = nil
442 446 time_stat = "Time used: n/a sec."
443 447 end
444 448
445 449 # extract memory usage
446 450 if res = /s(.*)m/.match(running_stat_line)
447 451 memory_used = res[1].to_i
448 452 else
449 453 memory_used = -1
450 454 end
451 455
452 456 return {
453 457 :msg => "#{run_stat}\n#{time_stat}",
454 458 :running_time => seconds,
455 459 :exit_status => run_stat,
456 460 :memory_usage => memory_used
457 461 }
458 462 end
459 463
460 464 def update_user_start_time
461 465 user = User.find(session[:user_id])
462 466 UserContestStat.update_user_start_time(user)
463 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 475 end
466 476
You need to be logged in to leave comments. Login now