Description:
fixed submission view bug, reported by chalet
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@292 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
r153:15e1ee6d602b - - 1 file changed: 5 inserted, 0 deleted
@@ -1,299 +1,304 | |||
|
1 | 1 | class MainController < ApplicationController |
|
2 | 2 | |
|
3 | 3 | SYSTEM_MODE_CONF_KEY = 'system.mode' |
|
4 | 4 | |
|
5 | 5 | before_filter :authenticate, :except => [:index, :login] |
|
6 | 6 | before_filter :check_viewability, :except => [:index, :login] |
|
7 | 7 | |
|
8 | 8 | # |
|
9 | 9 | # COMMENT OUT: filter in each action instead |
|
10 | 10 | # |
|
11 | 11 | # before_filter :verify_time_limit, :only => [:submit] |
|
12 | 12 | |
|
13 | 13 | verify :method => :post, :only => [:submit], |
|
14 | 14 | :redirect_to => { :action => :index } |
|
15 | 15 | |
|
16 | 16 | # COMMENT OUT, only need when having high load |
|
17 | 17 | # caches_action :index, :login |
|
18 | 18 | |
|
19 | 19 | def index |
|
20 | 20 | redirect_to :action => 'login' |
|
21 | 21 | end |
|
22 | 22 | |
|
23 | 23 | def login |
|
24 | 24 | saved_notice = flash[:notice] |
|
25 | 25 | reset_session |
|
26 | 26 | flash[:notice] = saved_notice |
|
27 | 27 | |
|
28 | 28 | # |
|
29 | 29 | # These are for site administrator login |
|
30 | 30 | # |
|
31 | 31 | @countries = Country.find(:all, :include => :sites) |
|
32 | 32 | @country_select = @countries.collect { |c| [c.name, c.id] } |
|
33 | 33 | |
|
34 | 34 | @country_select_with_all = [['Any',0]] |
|
35 | 35 | @countries.each do |country| |
|
36 | 36 | @country_select_with_all << [country.name, country.id] |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | @site_select = [] |
|
40 | 40 | @countries.each do |country| |
|
41 | 41 | country.sites.each do |site| |
|
42 | 42 | @site_select << ["#{site.name}, #{country.name}", site.id] |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | @announcements = Announcement.find_for_frontpage |
|
47 | 47 | render :action => 'login', :layout => 'empty' |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def list |
|
51 | 51 | prepare_list_information |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def help |
|
55 | 55 | @user = User.find(session[:user_id]) |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def submit |
|
59 | 59 | user = User.find(session[:user_id]) |
|
60 | 60 | |
|
61 | 61 | @submission = Submission.new(params[:submission]) |
|
62 | 62 | @submission.user = user |
|
63 | 63 | @submission.language_id = 0 |
|
64 | 64 | if params['file']!='' |
|
65 | 65 | @submission.source = params['file'].read |
|
66 | 66 | @submission.source_filename = params['file'].original_filename |
|
67 | 67 | end |
|
68 | 68 | @submission.submitted_at = Time.new.gmtime |
|
69 | 69 | |
|
70 | 70 | if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and |
|
71 | 71 | user.site!=nil and user.site.finished? |
|
72 | 72 | @submission.errors.add_to_base "The contest is over." |
|
73 | 73 | prepare_list_information |
|
74 | 74 | render :action => 'list' and return |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | if @submission.valid? |
|
78 | 78 | if @submission.save == false |
|
79 | 79 | flash[:notice] = 'Error saving your submission' |
|
80 | 80 | elsif Task.create(:submission_id => @submission.id, |
|
81 | 81 | :status => Task::STATUS_INQUEUE) == false |
|
82 | 82 | flash[:notice] = 'Error adding your submission to task queue' |
|
83 | 83 | end |
|
84 | 84 | else |
|
85 | 85 | prepare_list_information |
|
86 | 86 | render :action => 'list' and return |
|
87 | 87 | end |
|
88 | 88 | redirect_to :action => 'list' |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def source |
|
92 | 92 | submission = Submission.find(params[:id]) |
|
93 | 93 | if submission.user_id == session[:user_id] |
|
94 | 94 | if submission.problem.output_only |
|
95 | 95 | fname = submission.source_filename |
|
96 | 96 | else |
|
97 | 97 | fname = submission.problem.name + '.' + submission.language.ext |
|
98 | 98 | end |
|
99 | 99 | send_data(submission.source, |
|
100 | 100 | {:filename => fname, |
|
101 | 101 | :type => 'text/plain'}) |
|
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 compiler_msg |
|
109 | 109 | @submission = Submission.find(params[:id]) |
|
110 | 110 | if @submission.user_id == session[:user_id] |
|
111 | 111 | render :action => 'compiler_msg', :layout => 'empty' |
|
112 | 112 | else |
|
113 | 113 | flash[:notice] = 'Error viewing source' |
|
114 | 114 | redirect_to :action => 'list' |
|
115 | 115 | end |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | def submission |
|
119 | 119 | @user = User.find(session[:user_id]) |
|
120 | 120 | @problems = Problem.find_available_problems |
|
121 | 121 | if params[:id]==nil |
|
122 | 122 | @problem = nil |
|
123 | 123 | @submissions = nil |
|
124 | 124 | else |
|
125 | 125 | @problem = Problem.find_by_name(params[:id]) |
|
126 | + if not @problem.available | |
|
127 | + redirect_to :action => 'list' | |
|
128 | + flash[:notice] = 'Error: submissions for that problem is not available' | |
|
129 | + return | |
|
130 | + end | |
|
126 | 131 | @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id) |
|
127 | 132 | end |
|
128 | 133 | end |
|
129 | 134 | |
|
130 | 135 | def result |
|
131 | 136 | if !Configuration.show_grading_result |
|
132 | 137 | redirect_to :action => 'list' and return |
|
133 | 138 | end |
|
134 | 139 | @user = User.find(session[:user_id]) |
|
135 | 140 | @submission = Submission.find(params[:id]) |
|
136 | 141 | if @submission.user!=@user |
|
137 | 142 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
138 | 143 | redirect_to :action => 'list' and return |
|
139 | 144 | end |
|
140 | 145 | prepare_grading_result(@submission) |
|
141 | 146 | end |
|
142 | 147 | |
|
143 | 148 | def load_output |
|
144 | 149 | if !Configuration.show_grading_result or params[:num]==nil |
|
145 | 150 | redirect_to :action => 'list' and return |
|
146 | 151 | end |
|
147 | 152 | @user = User.find(session[:user_id]) |
|
148 | 153 | @submission = Submission.find(params[:id]) |
|
149 | 154 | if @submission.user!=@user |
|
150 | 155 | flash[:notice] = 'You are not allowed to view result of other users.' |
|
151 | 156 | redirect_to :action => 'list' and return |
|
152 | 157 | end |
|
153 | 158 | case_num = params[:num].to_i |
|
154 | 159 | out_filename = output_filename(@user.login, |
|
155 | 160 | @submission.problem.name, |
|
156 | 161 | @submission.id, |
|
157 | 162 | case_num) |
|
158 | 163 | if !FileTest.exists?(out_filename) |
|
159 | 164 | flash[:notice] = 'Output not found.' |
|
160 | 165 | redirect_to :action => 'list' and return |
|
161 | 166 | end |
|
162 | 167 | |
|
163 | 168 | response.headers['Content-Type'] = "application/force-download" |
|
164 | 169 | response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\"" |
|
165 | 170 | response.headers["X-Sendfile"] = out_filename |
|
166 | 171 | response.headers['Content-length'] = File.size(out_filename) |
|
167 | 172 | render :nothing => true |
|
168 | 173 | end |
|
169 | 174 | |
|
170 | 175 | def error |
|
171 | 176 | @user = User.find(session[:user_id]) |
|
172 | 177 | end |
|
173 | 178 | |
|
174 | 179 | protected |
|
175 | 180 | def prepare_list_information |
|
176 | 181 | @problems = Problem.find_available_problems |
|
177 | 182 | @prob_submissions = Array.new |
|
178 | 183 | @user = User.find(session[:user_id]) |
|
179 | 184 | @problems.each do |p| |
|
180 | 185 | sub = Submission.find_last_by_user_and_problem(@user.id,p.id) |
|
181 | 186 | if sub!=nil |
|
182 | 187 | @prob_submissions << { :count => sub.number, :submission => sub } |
|
183 | 188 | else |
|
184 | 189 | @prob_submissions << { :count => 0, :submission => nil } |
|
185 | 190 | end |
|
186 | 191 | end |
|
187 | 192 | @announcements = Announcement.find_published |
|
188 | 193 | end |
|
189 | 194 | |
|
190 | 195 | def check_viewability |
|
191 | 196 | @user = User.find(session[:user_id]) |
|
192 | 197 | if (!Configuration.show_tasks_to?(@user)) and |
|
193 | 198 | ((action_name=='submission') or (action_name=='submit')) |
|
194 | 199 | redirect_to :action => 'list' and return |
|
195 | 200 | end |
|
196 | 201 | end |
|
197 | 202 | |
|
198 | 203 | def prepare_grading_result(submission) |
|
199 | 204 | grading_info = Configuration.task_grading_info[submission.problem.name] |
|
200 | 205 | @test_runs = [] |
|
201 | 206 | if grading_info['testruns'].is_a? Integer |
|
202 | 207 | trun_count = grading_info['testruns'] |
|
203 | 208 | trun_count.times do |i| |
|
204 | 209 | @test_runs << [ read_grading_result(@user.login, |
|
205 | 210 | submission.problem.name, |
|
206 | 211 | submission.id, |
|
207 | 212 | i+1) ] |
|
208 | 213 | end |
|
209 | 214 | else |
|
210 | 215 | grading_info['testruns'].keys.sort.each do |num| |
|
211 | 216 | run = [] |
|
212 | 217 | testrun = grading_info['testruns'][num] |
|
213 | 218 | testrun.each do |c| |
|
214 | 219 | run << read_grading_result(@user.login, |
|
215 | 220 | submission.problem.name, |
|
216 | 221 | submission.id, |
|
217 | 222 | c) |
|
218 | 223 | end |
|
219 | 224 | @test_runs << run |
|
220 | 225 | end |
|
221 | 226 | end |
|
222 | 227 | end |
|
223 | 228 | |
|
224 | 229 | def grading_result_dir(user_name, problem_name, submission_id, case_num) |
|
225 | 230 | return "#{GRADING_RESULT_DIR}/#{user_name}/#{problem_name}/#{submission_id}/test-result/#{case_num}" |
|
226 | 231 | end |
|
227 | 232 | |
|
228 | 233 | def output_filename(user_name, problem_name, submission_id, case_num) |
|
229 | 234 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
230 | 235 | return "#{dir}/output.txt" |
|
231 | 236 | end |
|
232 | 237 | |
|
233 | 238 | def read_grading_result(user_name, problem_name, submission_id, case_num) |
|
234 | 239 | dir = grading_result_dir(user_name,problem_name, submission_id, case_num) |
|
235 | 240 | result_file_name = "#{dir}/result" |
|
236 | 241 | if !FileTest.exists?(result_file_name) |
|
237 | 242 | return {:num => case_num, :msg => 'program did not run'} |
|
238 | 243 | else |
|
239 | 244 | results = File.open(result_file_name).readlines |
|
240 | 245 | run_stat = extract_running_stat(results) |
|
241 | 246 | output_filename = "#{dir}/output.txt" |
|
242 | 247 | if FileTest.exists?(output_filename) |
|
243 | 248 | output_file = true |
|
244 | 249 | output_size = File.size(output_filename) |
|
245 | 250 | else |
|
246 | 251 | output_file = false |
|
247 | 252 | output_size = 0 |
|
248 | 253 | end |
|
249 | 254 | |
|
250 | 255 | return { |
|
251 | 256 | :num => case_num, |
|
252 | 257 | :msg => results[0], |
|
253 | 258 | :run_stat => run_stat, |
|
254 | 259 | :output => output_file, |
|
255 | 260 | :output_size => output_size |
|
256 | 261 | } |
|
257 | 262 | end |
|
258 | 263 | end |
|
259 | 264 | |
|
260 | 265 | # copied from grader/script/lib/test_request_helper.rb |
|
261 | 266 | def extract_running_stat(results) |
|
262 | 267 | running_stat_line = results[-1] |
|
263 | 268 | |
|
264 | 269 | # extract exit status line |
|
265 | 270 | run_stat = "" |
|
266 | 271 | if !(/[Cc]orrect/.match(results[0])) |
|
267 | 272 | run_stat = results[0].chomp |
|
268 | 273 | else |
|
269 | 274 | run_stat = 'Program exited normally' |
|
270 | 275 | end |
|
271 | 276 | |
|
272 | 277 | logger.info "Stat line: #{running_stat_line}" |
|
273 | 278 | |
|
274 | 279 | # extract running time |
|
275 | 280 | if res = /r(.*)u(.*)s/.match(running_stat_line) |
|
276 | 281 | seconds = (res[1].to_f + res[2].to_f) |
|
277 | 282 | time_stat = "Time used: #{seconds} sec." |
|
278 | 283 | else |
|
279 | 284 | seconds = nil |
|
280 | 285 | time_stat = "Time used: n/a sec." |
|
281 | 286 | end |
|
282 | 287 | |
|
283 | 288 | # extract memory usage |
|
284 | 289 | if res = /s(.*)m/.match(running_stat_line) |
|
285 | 290 | memory_used = res[1].to_i |
|
286 | 291 | else |
|
287 | 292 | memory_used = -1 |
|
288 | 293 | end |
|
289 | 294 | |
|
290 | 295 | return { |
|
291 | 296 | :msg => "#{run_stat}\n#{time_stat}", |
|
292 | 297 | :running_time => seconds, |
|
293 | 298 | :exit_status => run_stat, |
|
294 | 299 | :memory_usage => memory_used |
|
295 | 300 | } |
|
296 | 301 | end |
|
297 | 302 | |
|
298 | 303 | end |
|
299 | 304 |
You need to be logged in to leave comments.
Login now