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: 26 inserted, 9 deleted

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