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

@@ -11,25 +11,30
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
@@ -75,8 +80,10
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
@@ -57,6 +57,9
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
@@ -84,10 +87,14
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
@@ -30,6 +30,8
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| %>
@@ -61,6 +63,7
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
You need to be logged in to leave comments. Login now