Description:
[web] fix time.new, time.now to use gmtime
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@249 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
r121:00c0c912cae9 - - 3 files changed: 3 inserted, 3 deleted
@@ -1,129 +1,129 | |||||
|
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 |
|
4 | ||
|
5 | # |
|
5 | # |
|
6 | # COMMENT OUT: filter in each action instead |
|
6 | # COMMENT OUT: filter in each action instead |
|
7 | # |
|
7 | # |
|
8 | # before_filter :verify_time_limit, :only => [:submit] |
|
8 | # before_filter :verify_time_limit, :only => [:submit] |
|
9 |
|
9 | ||
|
10 | verify :method => :post, :only => [:submit], |
|
10 | verify :method => :post, :only => [:submit], |
|
11 | :redirect_to => { :action => :index } |
|
11 | :redirect_to => { :action => :index } |
|
12 |
|
12 | ||
|
13 |
|
13 | ||
|
14 | def index |
|
14 | def index |
|
15 | redirect_to :action => 'login' |
|
15 | redirect_to :action => 'login' |
|
16 | end |
|
16 | end |
|
17 |
|
17 | ||
|
18 | def login |
|
18 | def login |
|
19 | saved_notice = flash[:notice] |
|
19 | saved_notice = flash[:notice] |
|
20 | reset_session |
|
20 | reset_session |
|
21 | flash[:notice] = saved_notice |
|
21 | flash[:notice] = saved_notice |
|
22 |
|
22 | ||
|
23 | render :action => 'login', :layout => 'empty' |
|
23 | render :action => 'login', :layout => 'empty' |
|
24 | end |
|
24 | end |
|
25 |
|
25 | ||
|
26 | def list |
|
26 | def list |
|
27 | prepare_list_information |
|
27 | prepare_list_information |
|
28 | end |
|
28 | end |
|
29 |
|
29 | ||
|
30 | def help |
|
30 | def help |
|
31 | @user = User.find(session[:user_id]) |
|
31 | @user = User.find(session[:user_id]) |
|
32 | end |
|
32 | end |
|
33 |
|
33 | ||
|
34 | def submit |
|
34 | def submit |
|
35 | user = User.find(session[:user_id]) |
|
35 | user = User.find(session[:user_id]) |
|
36 |
|
36 | ||
|
37 | @submission = Submission.new(params[:submission]) |
|
37 | @submission = Submission.new(params[:submission]) |
|
38 | @submission.user = user |
|
38 | @submission.user = user |
|
39 | @submission.language_id = 0 |
|
39 | @submission.language_id = 0 |
|
40 | if params['file']!='' |
|
40 | if params['file']!='' |
|
41 | @submission.source = params['file'].read |
|
41 | @submission.source = params['file'].read |
|
42 | @submission.source_filename = params['file'].original_filename |
|
42 | @submission.source_filename = params['file'].original_filename |
|
43 | end |
|
43 | end |
|
44 | - @submission.submitted_at = Time.new |
|
44 | + @submission.submitted_at = Time.new.gmtime |
|
45 |
|
45 | ||
|
46 | if user.site!=nil and user.site.finished? |
|
46 | if user.site!=nil and user.site.finished? |
|
47 | @submission.errors.add_to_base "The contest is over." |
|
47 | @submission.errors.add_to_base "The contest is over." |
|
48 | prepare_list_information |
|
48 | prepare_list_information |
|
49 | render :action => 'list' and return |
|
49 | render :action => 'list' and return |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | if @submission.valid? |
|
52 | if @submission.valid? |
|
53 | if @submission.save == false |
|
53 | if @submission.save == false |
|
54 | flash[:notice] = 'Error saving your submission' |
|
54 | flash[:notice] = 'Error saving your submission' |
|
55 | elsif Task.create(:submission_id => @submission.id, |
|
55 | elsif Task.create(:submission_id => @submission.id, |
|
56 | :status => Task::STATUS_INQUEUE) == false |
|
56 | :status => Task::STATUS_INQUEUE) == false |
|
57 | flash[:notice] = 'Error adding your submission to task queue' |
|
57 | flash[:notice] = 'Error adding your submission to task queue' |
|
58 | end |
|
58 | end |
|
59 | else |
|
59 | else |
|
60 | prepare_list_information |
|
60 | prepare_list_information |
|
61 | render :action => 'list' and return |
|
61 | render :action => 'list' and return |
|
62 | end |
|
62 | end |
|
63 | redirect_to :action => 'list' |
|
63 | redirect_to :action => 'list' |
|
64 | end |
|
64 | end |
|
65 |
|
65 | ||
|
66 | def source |
|
66 | def source |
|
67 | submission = Submission.find(params[:id]) |
|
67 | submission = Submission.find(params[:id]) |
|
68 | if submission.user_id == session[:user_id] |
|
68 | if submission.user_id == session[:user_id] |
|
69 | if submission.problem.output_only |
|
69 | if submission.problem.output_only |
|
70 | fname = submission.source_filename |
|
70 | fname = submission.source_filename |
|
71 | else |
|
71 | else |
|
72 | fname = submission.problem.name + '.' + submission.language.ext |
|
72 | fname = submission.problem.name + '.' + submission.language.ext |
|
73 | end |
|
73 | end |
|
74 | send_data(submission.source, |
|
74 | send_data(submission.source, |
|
75 | {:filename => fname, |
|
75 | {:filename => fname, |
|
76 | :type => 'text/plain'}) |
|
76 | :type => 'text/plain'}) |
|
77 | else |
|
77 | else |
|
78 | flash[:notice] = 'Error viewing source' |
|
78 | flash[:notice] = 'Error viewing source' |
|
79 | redirect_to :action => 'list' |
|
79 | redirect_to :action => 'list' |
|
80 | end |
|
80 | end |
|
81 | end |
|
81 | end |
|
82 |
|
82 | ||
|
83 | def compiler_msg |
|
83 | def compiler_msg |
|
84 | @submission = Submission.find(params[:id]) |
|
84 | @submission = Submission.find(params[:id]) |
|
85 | if @submission.user_id == session[:user_id] |
|
85 | if @submission.user_id == session[:user_id] |
|
86 | render :action => 'compiler_msg', :layout => 'empty' |
|
86 | render :action => 'compiler_msg', :layout => 'empty' |
|
87 | else |
|
87 | else |
|
88 | flash[:notice] = 'Error viewing source' |
|
88 | flash[:notice] = 'Error viewing source' |
|
89 | redirect_to :action => 'list' |
|
89 | redirect_to :action => 'list' |
|
90 | end |
|
90 | end |
|
91 | end |
|
91 | end |
|
92 |
|
92 | ||
|
93 | def submission |
|
93 | def submission |
|
94 | @user = User.find(session[:user_id]) |
|
94 | @user = User.find(session[:user_id]) |
|
95 | @problems = Problem.find_available_problems |
|
95 | @problems = Problem.find_available_problems |
|
96 | if params[:id]==nil |
|
96 | if params[:id]==nil |
|
97 | @problem = nil |
|
97 | @problem = nil |
|
98 | @submissions = nil |
|
98 | @submissions = nil |
|
99 | else |
|
99 | else |
|
100 | @problem = Problem.find_by_name(params[:id]) |
|
100 | @problem = Problem.find_by_name(params[:id]) |
|
101 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
101 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
102 | end |
|
102 | end |
|
103 | end |
|
103 | end |
|
104 |
|
104 | ||
|
105 | def error |
|
105 | def error |
|
106 | @user = User.find(session[:user_id]) |
|
106 | @user = User.find(session[:user_id]) |
|
107 | end |
|
107 | end |
|
108 |
|
108 | ||
|
109 | protected |
|
109 | protected |
|
110 | def prepare_list_information |
|
110 | def prepare_list_information |
|
111 | @problems = Problem.find_available_problems |
|
111 | @problems = Problem.find_available_problems |
|
112 | @prob_submissions = Array.new |
|
112 | @prob_submissions = Array.new |
|
113 | @user = User.find(session[:user_id]) |
|
113 | @user = User.find(session[:user_id]) |
|
114 | @problems.each do |p| |
|
114 | @problems.each do |p| |
|
115 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
115 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
116 | if sub!=nil |
|
116 | if sub!=nil |
|
117 | @prob_submissions << { :count => sub.number, :submission => sub } |
|
117 | @prob_submissions << { :count => sub.number, :submission => sub } |
|
118 | else |
|
118 | else |
|
119 | @prob_submissions << { :count => 0, :submission => nil } |
|
119 | @prob_submissions << { :count => 0, :submission => nil } |
|
120 | end |
|
120 | end |
|
121 | end |
|
121 | end |
|
122 |
|
122 | ||
|
123 | @announcements = Announcement.find(:all, |
|
123 | @announcements = Announcement.find(:all, |
|
124 | :conditions => "published = 1", |
|
124 | :conditions => "published = 1", |
|
125 | :order => "created_at DESC") |
|
125 | :order => "created_at DESC") |
|
126 | end |
|
126 | end |
|
127 |
|
127 | ||
|
128 | end |
|
128 | end |
|
129 |
|
129 |
@@ -1,27 +1,27 | |||||
|
1 | class Site < ActiveRecord::Base |
|
1 | class Site < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :country |
|
3 | belongs_to :country |
|
4 | has_many :users |
|
4 | has_many :users |
|
5 |
|
5 | ||
|
6 | def clear_start_time_if_not_started |
|
6 | def clear_start_time_if_not_started |
|
7 | if !self.started |
|
7 | if !self.started |
|
8 | self.start_time = nil |
|
8 | self.start_time = nil |
|
9 | end |
|
9 | end |
|
10 | end |
|
10 | end |
|
11 |
|
11 | ||
|
12 | def finished? |
|
12 | def finished? |
|
13 | if !self.started |
|
13 | if !self.started |
|
14 | return false |
|
14 | return false |
|
15 | end |
|
15 | end |
|
16 |
|
16 | ||
|
17 | contest_time = Configuration['contest.time_limit'] |
|
17 | contest_time = Configuration['contest.time_limit'] |
|
18 | if tmatch = /(\d+):(\d+)/.match(contest_time) |
|
18 | if tmatch = /(\d+):(\d+)/.match(contest_time) |
|
19 | h = tmatch[1].to_i |
|
19 | h = tmatch[1].to_i |
|
20 | m = tmatch[2].to_i |
|
20 | m = tmatch[2].to_i |
|
21 | - return Time.now > (self.start_time + h.hour + m.minute) |
|
21 | + return Time.now.gmtime > (self.start_time + h.hour + m.minute) |
|
22 | else |
|
22 | else |
|
23 | false |
|
23 | false |
|
24 | end |
|
24 | end |
|
25 | end |
|
25 | end |
|
26 |
|
26 | ||
|
27 | end |
|
27 | end |
@@ -1,157 +1,157 | |||||
|
1 | # |
|
1 | # |
|
2 | # A TestRequest is a composition of submission with user's testdata. |
|
2 | # A TestRequest is a composition of submission with user's testdata. |
|
3 | # |
|
3 | # |
|
4 | # Note about TestRequest#problem: Usually, A TestRequest has to be |
|
4 | # Note about TestRequest#problem: Usually, A TestRequest has to be |
|
5 | # associated with a problem, so that execution environment can be |
|
5 | # associated with a problem, so that execution environment can be |
|
6 | # determined. However, to be more flexible, we have to ensure that |
|
6 | # determined. However, to be more flexible, we have to ensure that |
|
7 | # it works as well with problem=nil. In this case, we shall provide |
|
7 | # it works as well with problem=nil. In this case, we shall provide |
|
8 | # a "default" execution environment for it. This can be done |
|
8 | # a "default" execution environment for it. This can be done |
|
9 | # seamlessly by using TestRequest#problem_name or |
|
9 | # seamlessly by using TestRequest#problem_name or |
|
10 | # TestRequest#name_of(problem) when retrieving the name of the |
|
10 | # TestRequest#name_of(problem) when retrieving the name of the |
|
11 | # problem: #name_of would return problem.name when problem!=nil and |
|
11 | # problem: #name_of would return problem.name when problem!=nil and |
|
12 | # it would return "default" when problem=nil, #problem_name just |
|
12 | # it would return "default" when problem=nil, #problem_name just |
|
13 | # call #name_of. |
|
13 | # call #name_of. |
|
14 | # |
|
14 | # |
|
15 |
|
15 | ||
|
16 | require 'fileutils' |
|
16 | require 'fileutils' |
|
17 |
|
17 | ||
|
18 | class TestRequest < Task |
|
18 | class TestRequest < Task |
|
19 |
|
19 | ||
|
20 | set_table_name "test_requests" |
|
20 | set_table_name "test_requests" |
|
21 |
|
21 | ||
|
22 | belongs_to :user |
|
22 | belongs_to :user |
|
23 | belongs_to :problem |
|
23 | belongs_to :problem |
|
24 | belongs_to :submission |
|
24 | belongs_to :submission |
|
25 |
|
25 | ||
|
26 | validates_presence_of :submission |
|
26 | validates_presence_of :submission |
|
27 | validate :must_have_valid_problem |
|
27 | validate :must_have_valid_problem |
|
28 |
|
28 | ||
|
29 | def problem_name |
|
29 | def problem_name |
|
30 | TestRequest.name_of(self.problem) |
|
30 | TestRequest.name_of(self.problem) |
|
31 | end |
|
31 | end |
|
32 |
|
32 | ||
|
33 | def language |
|
33 | def language |
|
34 | self.submission.language |
|
34 | self.submission.language |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | def self.get_inqueue_and_change_status(status) |
|
37 | def self.get_inqueue_and_change_status(status) |
|
38 | # since there will be only one grader grading TestRequest |
|
38 | # since there will be only one grader grading TestRequest |
|
39 | # we do not need locking (hopefully) |
|
39 | # we do not need locking (hopefully) |
|
40 |
|
40 | ||
|
41 | test_request = TestRequest.find(:first, |
|
41 | test_request = TestRequest.find(:first, |
|
42 | :order => "created_at", |
|
42 | :order => "created_at", |
|
43 | :conditions => {:status=> Task::STATUS_INQUEUE}) |
|
43 | :conditions => {:status=> Task::STATUS_INQUEUE}) |
|
44 | if test_request!=nil |
|
44 | if test_request!=nil |
|
45 | test_request.status = status |
|
45 | test_request.status = status |
|
46 | test_request.save! |
|
46 | test_request.save! |
|
47 | end |
|
47 | end |
|
48 |
|
48 | ||
|
49 | test_request |
|
49 | test_request |
|
50 | end |
|
50 | end |
|
51 |
|
51 | ||
|
52 | # interfacing with form |
|
52 | # interfacing with form |
|
53 | def self.new_from_form_params(user,params) |
|
53 | def self.new_from_form_params(user,params) |
|
54 | test_request = TestRequest.new |
|
54 | test_request = TestRequest.new |
|
55 | test_request.user = user |
|
55 | test_request.user = user |
|
56 | begin |
|
56 | begin |
|
57 | problem = Problem.find(params[:problem_id]) |
|
57 | problem = Problem.find(params[:problem_id]) |
|
58 | rescue ActiveRecord::RecordNotFound |
|
58 | rescue ActiveRecord::RecordNotFound |
|
59 | problem = nil |
|
59 | problem = nil |
|
60 | end |
|
60 | end |
|
61 | test_request.problem = problem |
|
61 | test_request.problem = problem |
|
62 | if problem!=nil |
|
62 | if problem!=nil |
|
63 | test_request.submission = |
|
63 | test_request.submission = |
|
64 | Submission.find_by_user_problem_number(user.id, |
|
64 | Submission.find_by_user_problem_number(user.id, |
|
65 | problem.id, |
|
65 | problem.id, |
|
66 | params[:submission_number]) |
|
66 | params[:submission_number]) |
|
67 | else |
|
67 | else |
|
68 | test_request.submission = nil |
|
68 | test_request.submission = nil |
|
69 | end |
|
69 | end |
|
70 |
|
70 | ||
|
71 | # checks if the user submits any input file |
|
71 | # checks if the user submits any input file |
|
72 | if params[:input_file]==nil or params[:input_file]=="" |
|
72 | if params[:input_file]==nil or params[:input_file]=="" |
|
73 | test_request.errors.add_to_base("No input submitted.") |
|
73 | test_request.errors.add_to_base("No input submitted.") |
|
74 | test_request.input_file_name = nil |
|
74 | test_request.input_file_name = nil |
|
75 | else |
|
75 | else |
|
76 | test_request.input_file_name = save_input_file(params[:input_file], user, problem) |
|
76 | test_request.input_file_name = save_input_file(params[:input_file], user, problem) |
|
77 | if test_request.input_file_name == nil |
|
77 | if test_request.input_file_name == nil |
|
78 | test_request.errors.add_to_base("No input submitted.") |
|
78 | test_request.errors.add_to_base("No input submitted.") |
|
79 | end |
|
79 | end |
|
80 | if params[:additional_file]!=nil and params[:additional_file]!="" |
|
80 | if params[:additional_file]!=nil and params[:additional_file]!="" |
|
81 | save_additional_file(params[:additional_file], |
|
81 | save_additional_file(params[:additional_file], |
|
82 | "#{test_request.input_file_name}.files") |
|
82 | "#{test_request.input_file_name}.files") |
|
83 | end |
|
83 | end |
|
84 | end |
|
84 | end |
|
85 | - test_request.submitted_at = Time.new |
|
85 | + test_request.submitted_at = Time.new.gmtime |
|
86 | test_request.status_inqueue |
|
86 | test_request.status_inqueue |
|
87 | test_request |
|
87 | test_request |
|
88 | end |
|
88 | end |
|
89 |
|
89 | ||
|
90 | protected |
|
90 | protected |
|
91 |
|
91 | ||
|
92 | def self.name_of(problem) |
|
92 | def self.name_of(problem) |
|
93 | if problem!=nil |
|
93 | if problem!=nil |
|
94 | problem.name |
|
94 | problem.name |
|
95 | else |
|
95 | else |
|
96 | "default" |
|
96 | "default" |
|
97 | end |
|
97 | end |
|
98 | end |
|
98 | end |
|
99 |
|
99 | ||
|
100 | def self.random_input_file_name(user,problem) |
|
100 | def self.random_input_file_name(user,problem) |
|
101 | problem_name = TestRequest.name_of(problem) |
|
101 | problem_name = TestRequest.name_of(problem) |
|
102 | begin |
|
102 | begin |
|
103 | tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
103 | tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
104 | end while File.exists?(tmpname) |
|
104 | end while File.exists?(tmpname) |
|
105 | tmpname |
|
105 | tmpname |
|
106 | end |
|
106 | end |
|
107 |
|
107 | ||
|
108 | def self.save_input_file(tempfile, user, problem) |
|
108 | def self.save_input_file(tempfile, user, problem) |
|
109 | new_file_name = random_input_file_name(user,problem) |
|
109 | new_file_name = random_input_file_name(user,problem) |
|
110 | dirname = File.dirname(new_file_name) |
|
110 | dirname = File.dirname(new_file_name) |
|
111 | FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) |
|
111 | FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) |
|
112 |
|
112 | ||
|
113 | # when the user did not submit any file |
|
113 | # when the user did not submit any file |
|
114 | return nil if tempfile=="" |
|
114 | return nil if tempfile=="" |
|
115 |
|
115 | ||
|
116 | if tempfile.instance_of?(Tempfile) |
|
116 | if tempfile.instance_of?(Tempfile) |
|
117 | tempfile.close |
|
117 | tempfile.close |
|
118 | FileUtils.move(tempfile.path,new_file_name) |
|
118 | FileUtils.move(tempfile.path,new_file_name) |
|
119 | else |
|
119 | else |
|
120 | File.open(new_file_name, "wb") do |f| |
|
120 | File.open(new_file_name, "wb") do |f| |
|
121 | f.write(tempfile.read) |
|
121 | f.write(tempfile.read) |
|
122 | end |
|
122 | end |
|
123 | end |
|
123 | end |
|
124 | new_file_name |
|
124 | new_file_name |
|
125 | end |
|
125 | end |
|
126 |
|
126 | ||
|
127 | def self.save_additional_file(tempfile,dir) |
|
127 | def self.save_additional_file(tempfile,dir) |
|
128 | new_file_name = "#{dir}/#{tempfile.original_filename}" |
|
128 | new_file_name = "#{dir}/#{tempfile.original_filename}" |
|
129 | dirname = File.dirname(new_file_name) |
|
129 | dirname = File.dirname(new_file_name) |
|
130 | FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) |
|
130 | FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) |
|
131 |
|
131 | ||
|
132 | # when the user did not submit any file |
|
132 | # when the user did not submit any file |
|
133 | return nil if tempfile=="" |
|
133 | return nil if tempfile=="" |
|
134 |
|
134 | ||
|
135 | if tempfile.instance_of?(Tempfile) |
|
135 | if tempfile.instance_of?(Tempfile) |
|
136 | tempfile.close |
|
136 | tempfile.close |
|
137 | FileUtils.move(tempfile.path,new_file_name) |
|
137 | FileUtils.move(tempfile.path,new_file_name) |
|
138 | else |
|
138 | else |
|
139 | File.open(new_file_name, "wb") do |f| |
|
139 | File.open(new_file_name, "wb") do |f| |
|
140 | f.write(tempfile.read) |
|
140 | f.write(tempfile.read) |
|
141 | end |
|
141 | end |
|
142 | end |
|
142 | end |
|
143 | new_file_name |
|
143 | new_file_name |
|
144 | end |
|
144 | end |
|
145 |
|
145 | ||
|
146 | # |
|
146 | # |
|
147 | # validations |
|
147 | # validations |
|
148 | # |
|
148 | # |
|
149 | def must_have_valid_problem |
|
149 | def must_have_valid_problem |
|
150 | if problem==nil |
|
150 | if problem==nil |
|
151 | errors.add('problem',"must be specified.") |
|
151 | errors.add('problem',"must be specified.") |
|
152 | elsif (!problem.available) and (self.new_record?) |
|
152 | elsif (!problem.available) and (self.new_record?) |
|
153 | errors.add('problem',"must be valid.") |
|
153 | errors.add('problem',"must be valid.") |
|
154 | end |
|
154 | end |
|
155 | end |
|
155 | end |
|
156 |
|
156 | ||
|
157 | end |
|
157 | end |
You need to be logged in to leave comments.
Login now