Description:
fixed login error message recurring problem
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r229:c28a214e4637 - - 1 file changed: 1 inserted, 1 deleted

@@ -1,323 +1,323
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],
9 verify :method => :post, :only => [:submit],
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 protected
176 protected
177
177
178 def prepare_announcements(recent=nil)
178 def prepare_announcements(recent=nil)
179 if Configuration.show_tasks_to?(@user)
179 if Configuration.show_tasks_to?(@user)
180 @announcements = Announcement.find_published(true)
180 @announcements = Announcement.find_published(true)
181 else
181 else
182 @announcements = Announcement.find_published
182 @announcements = Announcement.find_published
183 end
183 end
184 if recent!=nil
184 if recent!=nil
185 recent_id = recent.to_i
185 recent_id = recent.to_i
186 @announcements = @announcements.find_all { |a| a.id > recent_id }
186 @announcements = @announcements.find_all { |a| a.id > recent_id }
187 end
187 end
188 end
188 end
189
189
190 def prepare_list_information
190 def prepare_list_information
191 @problems = Problem.find_available_problems
191 @problems = Problem.find_available_problems
192 @prob_submissions = Array.new
192 @prob_submissions = Array.new
193 @user = User.find(session[:user_id])
193 @user = User.find(session[:user_id])
194 @problems.each do |p|
194 @problems.each do |p|
195 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
195 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
196 if sub!=nil
196 if sub!=nil
197 @prob_submissions << { :count => sub.number, :submission => sub }
197 @prob_submissions << { :count => sub.number, :submission => sub }
198 else
198 else
199 @prob_submissions << { :count => 0, :submission => nil }
199 @prob_submissions << { :count => 0, :submission => nil }
200 end
200 end
201 end
201 end
202 prepare_announcements
202 prepare_announcements
203 end
203 end
204
204
205 def check_viewability
205 def check_viewability
206 @user = User.find(session[:user_id])
206 @user = User.find(session[:user_id])
207 if (!Configuration.show_tasks_to?(@user)) and
207 if (!Configuration.show_tasks_to?(@user)) and
208 ((action_name=='submission') or (action_name=='submit'))
208 ((action_name=='submission') or (action_name=='submit'))
209 redirect_to :action => 'list' and return
209 redirect_to :action => 'list' and return
210 end
210 end
211 end
211 end
212
212
213 def prepare_grading_result(submission)
213 def prepare_grading_result(submission)
214 if Configuration.task_grading_info.has_key? submission.problem.name
214 if Configuration.task_grading_info.has_key? submission.problem.name
215 grading_info = Configuration.task_grading_info[submission.problem.name]
215 grading_info = Configuration.task_grading_info[submission.problem.name]
216 else
216 else
217 # guess task info from problem.full_score
217 # guess task info from problem.full_score
218 cases = submission.problem.full_score / 10
218 cases = submission.problem.full_score / 10
219 grading_info = {
219 grading_info = {
220 'testruns' => cases,
220 'testruns' => cases,
221 'testcases' => cases
221 'testcases' => cases
222 }
222 }
223 end
223 end
224 @test_runs = []
224 @test_runs = []
225 if grading_info['testruns'].is_a? Integer
225 if grading_info['testruns'].is_a? Integer
226 trun_count = grading_info['testruns']
226 trun_count = grading_info['testruns']
227 trun_count.times do |i|
227 trun_count.times do |i|
228 @test_runs << [ read_grading_result(@user.login,
228 @test_runs << [ read_grading_result(@user.login,
229 submission.problem.name,
229 submission.problem.name,
230 submission.id,
230 submission.id,
231 i+1) ]
231 i+1) ]
232 end
232 end
233 else
233 else
234 grading_info['testruns'].keys.sort.each do |num|
234 grading_info['testruns'].keys.sort.each do |num|
235 run = []
235 run = []
236 testrun = grading_info['testruns'][num]
236 testrun = grading_info['testruns'][num]
237 testrun.each do |c|
237 testrun.each do |c|
238 run << read_grading_result(@user.login,
238 run << read_grading_result(@user.login,
239 submission.problem.name,
239 submission.problem.name,
240 submission.id,
240 submission.id,
241 c)
241 c)
242 end
242 end
243 @test_runs << run
243 @test_runs << run
244 end
244 end
245 end
245 end
246 end
246 end
247
247
248 def grading_result_dir(user_name, problem_name, submission_id, case_num)
248 def grading_result_dir(user_name, problem_name, submission_id, case_num)
249 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
249 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
250 end
250 end
251
251
252 def output_filename(user_name, problem_name, submission_id, case_num)
252 def output_filename(user_name, problem_name, submission_id, case_num)
253 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
253 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
254 return "#{dir}/output.txt"
254 return "#{dir}/output.txt"
255 end
255 end
256
256
257 def read_grading_result(user_name, problem_name, submission_id, case_num)
257 def read_grading_result(user_name, problem_name, submission_id, case_num)
258 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
258 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
259 result_file_name = "#{dir}/result"
259 result_file_name = "#{dir}/result"
260 if !FileTest.exists?(result_file_name)
260 if !FileTest.exists?(result_file_name)
261 return {:num => case_num, :msg => 'program did not run'}
261 return {:num => case_num, :msg => 'program did not run'}
262 else
262 else
263 results = File.open(result_file_name).readlines
263 results = File.open(result_file_name).readlines
264 run_stat = extract_running_stat(results)
264 run_stat = extract_running_stat(results)
265 output_filename = "#{dir}/output.txt"
265 output_filename = "#{dir}/output.txt"
266 if FileTest.exists?(output_filename)
266 if FileTest.exists?(output_filename)
267 output_file = true
267 output_file = true
268 output_size = File.size(output_filename)
268 output_size = File.size(output_filename)
269 else
269 else
270 output_file = false
270 output_file = false
271 output_size = 0
271 output_size = 0
272 end
272 end
273
273
274 return {
274 return {
275 :num => case_num,
275 :num => case_num,
276 :msg => results[0],
276 :msg => results[0],
277 :run_stat => run_stat,
277 :run_stat => run_stat,
278 :output => output_file,
278 :output => output_file,
279 :output_size => output_size
279 :output_size => output_size
280 }
280 }
281 end
281 end
282 end
282 end
283
283
284 # copied from grader/script/lib/test_request_helper.rb
284 # copied from grader/script/lib/test_request_helper.rb
285 def extract_running_stat(results)
285 def extract_running_stat(results)
286 running_stat_line = results[-1]
286 running_stat_line = results[-1]
287
287
288 # extract exit status line
288 # extract exit status line
289 run_stat = ""
289 run_stat = ""
290 if !(/[Cc]orrect/.match(results[0]))
290 if !(/[Cc]orrect/.match(results[0]))
291 run_stat = results[0].chomp
291 run_stat = results[0].chomp
292 else
292 else
293 run_stat = 'Program exited normally'
293 run_stat = 'Program exited normally'
294 end
294 end
295
295
296 logger.info "Stat line: #{running_stat_line}"
296 logger.info "Stat line: #{running_stat_line}"
297
297
298 # extract running time
298 # extract running time
299 if res = /r(.*)u(.*)s/.match(running_stat_line)
299 if res = /r(.*)u(.*)s/.match(running_stat_line)
300 seconds = (res[1].to_f + res[2].to_f)
300 seconds = (res[1].to_f + res[2].to_f)
301 time_stat = "Time used: #{seconds} sec."
301 time_stat = "Time used: #{seconds} sec."
302 else
302 else
303 seconds = nil
303 seconds = nil
304 time_stat = "Time used: n/a sec."
304 time_stat = "Time used: n/a sec."
305 end
305 end
306
306
307 # extract memory usage
307 # extract memory usage
308 if res = /s(.*)m/.match(running_stat_line)
308 if res = /s(.*)m/.match(running_stat_line)
309 memory_used = res[1].to_i
309 memory_used = res[1].to_i
310 else
310 else
311 memory_used = -1
311 memory_used = -1
312 end
312 end
313
313
314 return {
314 return {
315 :msg => "#{run_stat}\n#{time_stat}",
315 :msg => "#{run_stat}\n#{time_stat}",
316 :running_time => seconds,
316 :running_time => seconds,
317 :exit_status => run_stat,
317 :exit_status => run_stat,
318 :memory_usage => memory_used
318 :memory_usage => memory_used
319 }
319 }
320 end
320 end
321
321
322 end
322 end
323
323
You need to be logged in to leave comments. Login now