Description:
fixed single contest bug, reported by K. Siththa
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r292:35efcd905ebb - - 1 file changed: 1 inserted, 1 deleted

@@ -11,339 +11,339
11 11
12 12 # COMMENTED OUT: filter in each action instead
13 13 # before_filter :verify_time_limit, :only => [:submit]
14 14
15 15 verify :method => :post, :only => [:submit],
16 16 :redirect_to => { :action => :index }
17 17
18 18 # COMMENT OUT: only need when having high load
19 19 # caches_action :index, :login
20 20
21 21 # NOTE: This method is not actually needed, 'config/routes.rb' has
22 22 # assigned action login as a default action.
23 23 def index
24 24 redirect_to :action => 'login'
25 25 end
26 26
27 27 def login
28 28 saved_notice = flash[:notice]
29 29 reset_session
30 30 flash.now[:notice] = saved_notice
31 31
32 32 # EXPERIMENT:
33 33 # Hide login if in single user mode and the url does not
34 34 # explicitly specify /login
35 35 #
36 36 # logger.info "PATH: #{request.path}"
37 37 # if Configuration['system.single_user_mode'] and
38 38 # request.path!='/main/login'
39 39 # @hidelogin = true
40 40 # end
41 41
42 42 @announcements = Announcement.find_for_frontpage
43 43 render :action => 'login', :layout => 'empty'
44 44 end
45 45
46 46 def list
47 47 prepare_list_information
48 48 end
49 49
50 50 def help
51 51 @user = User.find(session[:user_id])
52 52 end
53 53
54 54 def submit
55 55 user = User.find(session[:user_id])
56 56
57 57 @submission = Submission.new(params[:submission])
58 58 @submission.user = user
59 59 @submission.language_id = 0
60 60 if (params['file']) and (params['file']!='')
61 61 @submission.source = params['file'].read
62 62 @submission.source_filename = params['file'].original_filename
63 63 end
64 64 @submission.submitted_at = Time.new.gmtime
65 65
66 66 if Configuration.time_limit_mode? and user.contest_finished?
67 67 @submission.errors.add_to_base "The contest is over."
68 68 prepare_list_information
69 69 render :action => 'list' and return
70 70 end
71 71
72 72 if @submission.valid?
73 73 if @submission.save == false
74 74 flash[:notice] = 'Error saving your submission'
75 75 elsif Task.create(:submission_id => @submission.id,
76 76 :status => Task::STATUS_INQUEUE) == false
77 77 flash[:notice] = 'Error adding your submission to task queue'
78 78 end
79 79 else
80 80 prepare_list_information
81 81 render :action => 'list' and return
82 82 end
83 83 redirect_to :action => 'list'
84 84 end
85 85
86 86 def source
87 87 submission = Submission.find(params[:id])
88 88 if submission.user_id == session[:user_id]
89 89 send_data(submission.source,
90 90 {:filename => submission.download_filename,
91 91 :type => 'text/plain'})
92 92 else
93 93 flash[:notice] = 'Error viewing source'
94 94 redirect_to :action => 'list'
95 95 end
96 96 end
97 97
98 98 def compiler_msg
99 99 @submission = Submission.find(params[:id])
100 100 if @submission.user_id == session[:user_id]
101 101 render :action => 'compiler_msg', :layout => 'empty'
102 102 else
103 103 flash[:notice] = 'Error viewing source'
104 104 redirect_to :action => 'list'
105 105 end
106 106 end
107 107
108 108 def submission
109 109 @user = User.find(session[:user_id])
110 110 @problems = @user.available_problems
111 111 if params[:id]==nil
112 112 @problem = nil
113 113 @submissions = nil
114 114 else
115 115 @problem = Problem.find_by_name(params[:id])
116 116 if not @problem.available
117 117 redirect_to :action => 'list'
118 118 flash[:notice] = 'Error: submissions for that problem are not viewable.'
119 119 return
120 120 end
121 121 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
122 122 end
123 123 end
124 124
125 125 def result
126 126 if !Configuration.show_grading_result
127 127 redirect_to :action => 'list' and return
128 128 end
129 129 @user = User.find(session[:user_id])
130 130 @submission = Submission.find(params[:id])
131 131 if @submission.user!=@user
132 132 flash[:notice] = 'You are not allowed to view result of other users.'
133 133 redirect_to :action => 'list' and return
134 134 end
135 135 prepare_grading_result(@submission)
136 136 end
137 137
138 138 def load_output
139 139 if !Configuration.show_grading_result or params[:num]==nil
140 140 redirect_to :action => 'list' and return
141 141 end
142 142 @user = User.find(session[:user_id])
143 143 @submission = Submission.find(params[:id])
144 144 if @submission.user!=@user
145 145 flash[:notice] = 'You are not allowed to view result of other users.'
146 146 redirect_to :action => 'list' and return
147 147 end
148 148 case_num = params[:num].to_i
149 149 out_filename = output_filename(@user.login,
150 150 @submission.problem.name,
151 151 @submission.id,
152 152 case_num)
153 153 if !FileTest.exists?(out_filename)
154 154 flash[:notice] = 'Output not found.'
155 155 redirect_to :action => 'list' and return
156 156 end
157 157
158 158 if defined?(USE_APACHE_XSENDFILE) and USE_APACHE_XSENDFILE
159 159 response.headers['Content-Type'] = "application/force-download"
160 160 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
161 161 response.headers["X-Sendfile"] = out_filename
162 162 response.headers['Content-length'] = File.size(out_filename)
163 163 render :nothing => true
164 164 else
165 165 send_file out_filename, :stream => false, :filename => "output-#{case_num}.txt", :type => "text/plain"
166 166 end
167 167 end
168 168
169 169 def error
170 170 @user = User.find(session[:user_id])
171 171 end
172 172
173 173 # announcement refreshing and hiding methods
174 174
175 175 def announcements
176 176 if params.has_key? 'recent'
177 177 prepare_announcements(params[:recent])
178 178 else
179 179 prepare_announcements
180 180 end
181 181 render(:partial => 'announcement',
182 182 :collection => @announcements,
183 183 :locals => {:announcement_effect => true})
184 184 end
185 185
186 186 protected
187 187
188 188 def prepare_announcements(recent=nil)
189 189 if Configuration.show_tasks_to?(@user)
190 190 @announcements = Announcement.find_published(true)
191 191 else
192 192 @announcements = Announcement.find_published
193 193 end
194 194 if recent!=nil
195 195 recent_id = recent.to_i
196 196 @announcements = @announcements.find_all { |a| a.id > recent_id }
197 197 end
198 198 end
199 199
200 200 def prepare_list_information
201 201 @user = User.find(session[:user_id])
202 202 if not Configuration.multicontests?
203 - @problems = problem_list_for_user(@user)
203 + @problems = @user.available_problems
204 204 else
205 205 @contest_problems = @user.available_problems_group_by_contests
206 206 @problems = @user.available_problems
207 207 end
208 208 @prob_submissions = {}
209 209 @problems.each do |p|
210 210 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
211 211 if sub!=nil
212 212 @prob_submissions[p.id] = { :count => sub.number, :submission => sub }
213 213 else
214 214 @prob_submissions[p.id] = { :count => 0, :submission => nil }
215 215 end
216 216 end
217 217 prepare_announcements
218 218 end
219 219
220 220 def check_viewability
221 221 @user = User.find(session[:user_id])
222 222 if (!Configuration.show_tasks_to?(@user)) and
223 223 ((action_name=='submission') or (action_name=='submit'))
224 224 redirect_to :action => 'list' and return
225 225 end
226 226 end
227 227
228 228 def prepare_grading_result(submission)
229 229 if Configuration.task_grading_info.has_key? submission.problem.name
230 230 grading_info = Configuration.task_grading_info[submission.problem.name]
231 231 else
232 232 # guess task info from problem.full_score
233 233 cases = submission.problem.full_score / 10
234 234 grading_info = {
235 235 'testruns' => cases,
236 236 'testcases' => cases
237 237 }
238 238 end
239 239 @test_runs = []
240 240 if grading_info['testruns'].is_a? Integer
241 241 trun_count = grading_info['testruns']
242 242 trun_count.times do |i|
243 243 @test_runs << [ read_grading_result(@user.login,
244 244 submission.problem.name,
245 245 submission.id,
246 246 i+1) ]
247 247 end
248 248 else
249 249 grading_info['testruns'].keys.sort.each do |num|
250 250 run = []
251 251 testrun = grading_info['testruns'][num]
252 252 testrun.each do |c|
253 253 run << read_grading_result(@user.login,
254 254 submission.problem.name,
255 255 submission.id,
256 256 c)
257 257 end
258 258 @test_runs << run
259 259 end
260 260 end
261 261 end
262 262
263 263 def grading_result_dir(user_name, problem_name, submission_id, case_num)
264 264 return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}"
265 265 end
266 266
267 267 def output_filename(user_name, problem_name, submission_id, case_num)
268 268 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
269 269 return "#{dir}/output.txt"
270 270 end
271 271
272 272 def read_grading_result(user_name, problem_name, submission_id, case_num)
273 273 dir = grading_result_dir(user_name,problem_name, submission_id, case_num)
274 274 result_file_name = "#{dir}/result"
275 275 if !FileTest.exists?(result_file_name)
276 276 return {:num => case_num, :msg => 'program did not run'}
277 277 else
278 278 results = File.open(result_file_name).readlines
279 279 run_stat = extract_running_stat(results)
280 280 output_filename = "#{dir}/output.txt"
281 281 if FileTest.exists?(output_filename)
282 282 output_file = true
283 283 output_size = File.size(output_filename)
284 284 else
285 285 output_file = false
286 286 output_size = 0
287 287 end
288 288
289 289 return {
290 290 :num => case_num,
291 291 :msg => results[0],
292 292 :run_stat => run_stat,
293 293 :output => output_file,
294 294 :output_size => output_size
295 295 }
296 296 end
297 297 end
298 298
299 299 # copied from grader/script/lib/test_request_helper.rb
300 300 def extract_running_stat(results)
301 301 running_stat_line = results[-1]
302 302
303 303 # extract exit status line
304 304 run_stat = ""
305 305 if !(/[Cc]orrect/.match(results[0]))
306 306 run_stat = results[0].chomp
307 307 else
308 308 run_stat = 'Program exited normally'
309 309 end
310 310
311 311 logger.info "Stat line: #{running_stat_line}"
312 312
313 313 # extract running time
314 314 if res = /r(.*)u(.*)s/.match(running_stat_line)
315 315 seconds = (res[1].to_f + res[2].to_f)
316 316 time_stat = "Time used: #{seconds} sec."
317 317 else
318 318 seconds = nil
319 319 time_stat = "Time used: n/a sec."
320 320 end
321 321
322 322 # extract memory usage
323 323 if res = /s(.*)m/.match(running_stat_line)
324 324 memory_used = res[1].to_i
325 325 else
326 326 memory_used = -1
327 327 end
328 328
329 329 return {
330 330 :msg => "#{run_stat}\n#{time_stat}",
331 331 :running_time => seconds,
332 332 :exit_status => run_stat,
333 333 :memory_usage => memory_used
334 334 }
335 335 end
336 336
337 337 def update_user_start_time
338 338 user = User.find(session[:user_id])
339 339 user.update_start_time
340 340 end
341 341
342 342 def reject_announcement_refresh_when_logged_out
343 343 if not session[:user_id]
344 344 render :text => 'Access forbidden', :status => 403
345 345 end
346 346 end
347 347
348 348 end
349 349
You need to be logged in to leave comments. Login now