Description:
[web] fixed test_request error when user does not put in any file git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@170 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r86:9bdacfc7b6d7 - - 3 files changed: 27 inserted, 10 deleted

@@ -2,43 +2,48
2
2
3 before_filter :authenticate
3 before_filter :authenticate
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 def index
13 def index
14 - @user = User.find(session[:user_id])
15 prepare_index_information
14 prepare_index_information
16 - @test_requests = @user.test_requests
17 end
15 end
18
16
19 def submit
17 def submit
20 @user = User.find(session[:user_id])
18 @user = User.find(session[:user_id])
21
19
22 - if @user.site!=nil and @user.site.finished?
20 + @submitted_test_request = TestRequest.new_from_form_params(@user,params[:test_request])
23 - flash[:notice] = 'Error saving your test submission: Contest is over.'
21 +
24 - redirect_to :action => 'index' and return
22 + if @submitted_test_request.errors.length != 0
23 + prepare_index_information
24 + render :action => 'index' and return
25 end
25 end
26
26
27 - test_request = TestRequest.new_from_form_params(@user,params[:test_request])
27 + if @user.site!=nil and @user.site.finished?
28 - if test_request.save
28 + @submitted_test_request.errors.add_to_base('Contest is over.')
29 + prepare_index_information
30 + render :action => 'index' and return
31 + end
32 +
33 + if @submitted_test_request.save
29 redirect_to :action => 'index'
34 redirect_to :action => 'index'
30 else
35 else
31 - flash[:notice] = 'Error saving your test submission'
36 + prepare_index_information
32 - redirect_to :action => 'index'
37 + render :action => 'index'
33 end
38 end
34 end
39 end
35
40
36 def read
41 def read
37 user = User.find(session[:user_id])
42 user = User.find(session[:user_id])
38 begin
43 begin
39 test_request = TestRequest.find(params[:id])
44 test_request = TestRequest.find(params[:id])
40 rescue
45 rescue
41 test_request = nil
46 test_request = nil
42 end
47 end
43 if test_request==nil or test_request.user_id != user.id
48 if test_request==nil or test_request.user_id != user.id
44 flash[:notice] = 'Invalid output'
49 flash[:notice] = 'Invalid output'
@@ -66,17 +71,19
66 @test_request = nil
71 @test_request = nil
67 end
72 end
68 if @test_request==nil or @test_request.user_id != @user.id
73 if @test_request==nil or @test_request.user_id != @user.id
69 flash[:notice] = 'Invalid request'
74 flash[:notice] = 'Invalid request'
70 redirect_to :action => 'index'
75 redirect_to :action => 'index'
71 return
76 return
72 end
77 end
73 end
78 end
74
79
75 protected
80 protected
76
81
77 def prepare_index_information
82 def prepare_index_information
83 + @user = User.find(session[:user_id])
78 @submissions = Submission.find_last_for_all_available_problems(@user.id)
84 @submissions = Submission.find_last_for_all_available_problems(@user.id)
79 @problems = @submissions.collect { |submission| submission.problem }
85 @problems = @submissions.collect { |submission| submission.problem }
86 + @test_requests = @user.test_requests
80 end
87 end
81
88
82 end
89 end
@@ -48,24 +48,27
48
48
49 # interfacing with form
49 # interfacing with form
50 def self.new_from_form_params(user,params)
50 def self.new_from_form_params(user,params)
51 test_request = TestRequest.new
51 test_request = TestRequest.new
52 test_request.user = user
52 test_request.user = user
53 problem = Problem.find(params[:problem_id])
53 problem = Problem.find(params[:problem_id])
54 test_request.problem = problem
54 test_request.problem = problem
55 test_request.submission =
55 test_request.submission =
56 Submission.find_by_user_problem_number(user.id,
56 Submission.find_by_user_problem_number(user.id,
57 problem.id,
57 problem.id,
58 params[:submission_number])
58 params[:submission_number])
59 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
59 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
60 + if test_request.input_file_name == nil
61 + test_request.errors.add_to_base("No input submitted.")
62 + end
60 test_request.submitted_at = Time.new
63 test_request.submitted_at = Time.new
61 test_request.status_inqueue
64 test_request.status_inqueue
62 test_request
65 test_request
63 end
66 end
64
67
65 protected
68 protected
66
69
67 def self.name_of(problem)
70 def self.name_of(problem)
68 if problem!=nil
71 if problem!=nil
69 problem.name
72 problem.name
70 else
73 else
71 "default"
74 "default"
@@ -75,23 +78,27
75 def self.random_input_file_name(user,problem)
78 def self.random_input_file_name(user,problem)
76 problem_name = TestRequest.name_of(problem)
79 problem_name = TestRequest.name_of(problem)
77 begin
80 begin
78 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
81 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
79 end while File.exists?(tmpname)
82 end while File.exists?(tmpname)
80 tmpname
83 tmpname
81 end
84 end
82
85
83 def self.save_input_file(tempfile, user, problem)
86 def self.save_input_file(tempfile, user, problem)
84 new_file_name = random_input_file_name(user,problem)
87 new_file_name = random_input_file_name(user,problem)
85 dirname = File.dirname(new_file_name)
88 dirname = File.dirname(new_file_name)
86 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
89 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
90 +
91 + # when the user did not submit any file
92 + return nil if tempfile==""
93 +
87 if tempfile.instance_of?(Tempfile)
94 if tempfile.instance_of?(Tempfile)
88 tempfile.close
95 tempfile.close
89 FileUtils.move(tempfile.path,new_file_name)
96 FileUtils.move(tempfile.path,new_file_name)
90 - else
97 + else
91 File.open(new_file_name, "wb") do |f|
98 File.open(new_file_name, "wb") do |f|
92 f.write(tempfile.read)
99 f.write(tempfile.read)
93 end
100 end
94 end
101 end
95 new_file_name
102 new_file_name
96 end
103 end
97 end
104 end
@@ -21,24 +21,26
21 for(i=0; i<old_len; i++)
21 for(i=0; i<old_len; i++)
22 submissionSelect.remove(0);
22 submissionSelect.remove(0);
23 for(i=count; i>=1; i--) {
23 for(i=count; i>=1; i--) {
24 try {
24 try {
25 submissionSelect.add(new Option(""+i,""+i,false,false),null);
25 submissionSelect.add(new Option(""+i,""+i,false,false),null);
26 } catch(ex) {
26 } catch(ex) {
27 submissionSelect.add(new Option(""+i,""+i,false,false));
27 submissionSelect.add(new Option(""+i,""+i,false,false));
28 }
28 }
29 }
29 }
30 }
30 }
31 </script>
31 </script>
32
32
33 + <div class="submitbox">
34 + <%= error_messages_for 'submitted_test_request' %>
33 <% form_for :test_request, nil,
35 <% form_for :test_request, nil,
34 :url => { :action => 'submit'},
36 :url => { :action => 'submit'},
35 :html => { :multipart => true } do |f| %>
37 :html => { :multipart => true } do |f| %>
36 <table>
38 <table>
37 <tr>
39 <tr>
38 <td>Task:</td>
40 <td>Task:</td>
39 <td>
41 <td>
40 <%= select(:test_request,
42 <%= select(:test_request,
41 :problem_id,
43 :problem_id,
42 @problems.collect {|p| [p.name, p.id]}, {},
44 @problems.collect {|p| [p.name, p.id]}, {},
43 { :onclick => "updateSubmissionList();" }) %>
45 { :onclick => "updateSubmissionList();" }) %>
44 </td>
46 </td>
@@ -52,24 +54,25
52 </td>
54 </td>
53 </tr>
55 </tr>
54 <tr>
56 <tr>
55 <td>Input data:</td>
57 <td>Input data:</td>
56 <td><%= f.file_field :input_file %></td>
58 <td><%= f.file_field :input_file %></td>
57 <tr>
59 <tr>
58 <td colspan="2">
60 <td colspan="2">
59 <%= submit_tag 'submit' %>
61 <%= submit_tag 'submit' %>
60 </td>
62 </td>
61 </tr>
63 </tr>
62 </table>
64 </table>
63 <% end %>
65 <% end %>
66 + </div>
64
67
65 <h3>Previous requests</h3>
68 <h3>Previous requests</h3>
66
69
67 <table class="info">
70 <table class="info">
68 <tr class="info-head">
71 <tr class="info-head">
69 <th>at</th>
72 <th>at</th>
70 <th>problem</th>
73 <th>problem</th>
71 <th>sub #</th>
74 <th>sub #</th>
72 <th>status</th>
75 <th>status</th>
73 <th>output (first 2kb)</th>
76 <th>output (first 2kb)</th>
74 <th>compiler message</th>
77 <th>compiler message</th>
75 <th>detail</th>
78 <th>detail</th>
You need to be logged in to leave comments. Login now