Description:
[web] test_request accepting additional file git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@232 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

r112:e81565ddb47a - - 2 files changed: 42 inserted, 1 deleted

@@ -32,103 +32,126
32
32
33 def language
33 def language
34 self.submission.language
34 self.submission.language
35 end
35 end
36
36
37 def self.get_inqueue_and_change_status(status)
37 def self.get_inqueue_and_change_status(status)
38 # since there will be only one grader grading TestRequest
38 # since there will be only one grader grading TestRequest
39 # we do not need locking (hopefully)
39 # we do not need locking (hopefully)
40
40
41 test_request = TestRequest.find(:first,
41 test_request = TestRequest.find(:first,
42 :order => "created_at",
42 :order => "created_at",
43 :conditions => {:status=> Task::STATUS_INQUEUE})
43 :conditions => {:status=> Task::STATUS_INQUEUE})
44 if test_request!=nil
44 if test_request!=nil
45 test_request.status = status
45 test_request.status = status
46 test_request.save!
46 test_request.save!
47 end
47 end
48
48
49 test_request
49 test_request
50 end
50 end
51
51
52 # interfacing with form
52 # interfacing with form
53 def self.new_from_form_params(user,params)
53 def self.new_from_form_params(user,params)
54 test_request = TestRequest.new
54 test_request = TestRequest.new
55 test_request.user = user
55 test_request.user = user
56 begin
56 begin
57 problem = Problem.find(params[:problem_id])
57 problem = Problem.find(params[:problem_id])
58 rescue ActiveRecord::RecordNotFound
58 rescue ActiveRecord::RecordNotFound
59 problem = nil
59 problem = nil
60 end
60 end
61 test_request.problem = problem
61 test_request.problem = problem
62 if problem!=nil
62 if problem!=nil
63 test_request.submission =
63 test_request.submission =
64 Submission.find_by_user_problem_number(user.id,
64 Submission.find_by_user_problem_number(user.id,
65 problem.id,
65 problem.id,
66 params[:submission_number])
66 params[:submission_number])
67 else
67 else
68 test_request.submission = nil
68 test_request.submission = nil
69 end
69 end
70
70
71 # checks if the user submits any input file
71 # checks if the user submits any input file
72 if params[:input_file]==nil or params[:input_file]==""
72 if params[:input_file]==nil or params[:input_file]==""
73 test_request.errors.add_to_base("No input submitted.")
73 test_request.errors.add_to_base("No input submitted.")
74 test_request.input_file_name = nil
74 test_request.input_file_name = nil
75 else
75 else
76 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
76 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
77 if test_request.input_file_name == nil
77 if test_request.input_file_name == nil
78 test_request.errors.add_to_base("No input submitted.")
78 test_request.errors.add_to_base("No input submitted.")
79 end
79 end
80 + if params[:additional_file]!=nil and params[:additional_file]!=""
81 + save_additional_file(params[:additional_file],
82 + "#{test_request.input_file_name}.files")
83 + end
80 end
84 end
81 test_request.submitted_at = Time.new
85 test_request.submitted_at = Time.new
82 test_request.status_inqueue
86 test_request.status_inqueue
83 test_request
87 test_request
84 end
88 end
85
89
86 protected
90 protected
87
91
88 def self.name_of(problem)
92 def self.name_of(problem)
89 if problem!=nil
93 if problem!=nil
90 problem.name
94 problem.name
91 else
95 else
92 "default"
96 "default"
93 end
97 end
94 end
98 end
95
99
96 def self.random_input_file_name(user,problem)
100 def self.random_input_file_name(user,problem)
97 problem_name = TestRequest.name_of(problem)
101 problem_name = TestRequest.name_of(problem)
98 begin
102 begin
99 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
103 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
100 end while File.exists?(tmpname)
104 end while File.exists?(tmpname)
101 tmpname
105 tmpname
102 end
106 end
103
107
104 def self.save_input_file(tempfile, user, problem)
108 def self.save_input_file(tempfile, user, problem)
105 new_file_name = random_input_file_name(user,problem)
109 new_file_name = random_input_file_name(user,problem)
106 dirname = File.dirname(new_file_name)
110 dirname = File.dirname(new_file_name)
107 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
111 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
108
112
109 # when the user did not submit any file
113 # when the user did not submit any file
110 return nil if tempfile==""
114 return nil if tempfile==""
111
115
112 if tempfile.instance_of?(Tempfile)
116 if tempfile.instance_of?(Tempfile)
113 tempfile.close
117 tempfile.close
114 FileUtils.move(tempfile.path,new_file_name)
118 FileUtils.move(tempfile.path,new_file_name)
115 else
119 else
116 File.open(new_file_name, "wb") do |f|
120 File.open(new_file_name, "wb") do |f|
117 f.write(tempfile.read)
121 f.write(tempfile.read)
118 end
122 end
119 end
123 end
120 new_file_name
124 new_file_name
121 end
125 end
122
126
127 + def self.save_additional_file(tempfile,dir)
128 + new_file_name = "#{dir}/#{tempfile.original_filename}"
129 + dirname = File.dirname(new_file_name)
130 + FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
131 +
132 + # when the user did not submit any file
133 + return nil if tempfile==""
134 +
135 + if tempfile.instance_of?(Tempfile)
136 + tempfile.close
137 + FileUtils.move(tempfile.path,new_file_name)
138 + else
139 + File.open(new_file_name, "wb") do |f|
140 + f.write(tempfile.read)
141 + end
142 + end
143 + new_file_name
144 + end
145 +
123 #
146 #
124 # validations
147 # validations
125 #
148 #
126 def must_have_valid_problem
149 def must_have_valid_problem
127 if problem==nil
150 if problem==nil
128 errors.add('problem',"must be specified.")
151 errors.add('problem',"must be specified.")
129 elsif (!problem.available) and (self.new_record?)
152 elsif (!problem.available) and (self.new_record?)
130 errors.add('problem',"must be valid.")
153 errors.add('problem',"must be valid.")
131 end
154 end
132 end
155 end
133
156
134 end
157 end
@@ -12,76 +12,94
12 submissionCount[<%= submission.problem_id %>]=<%= submission.number %>;
12 submissionCount[<%= submission.problem_id %>]=<%= submission.number %>;
13 <% end %>
13 <% end %>
14
14
15 function updateSubmissionList() {
15 function updateSubmissionList() {
16 currentProb = document.getElementById("test_request_problem_id").value;
16 currentProb = document.getElementById("test_request_problem_id").value;
17 count = submissionCount[currentProb];
17 count = submissionCount[currentProb];
18 submissionSelect = document.getElementById("test_request_submission_number");
18 submissionSelect = document.getElementById("test_request_submission_number");
19 old_len = submissionSelect.length;
19 old_len = submissionSelect.length;
20 // clear the box
20 // clear the box
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">
33 <div class="submitbox">
34 <%= error_messages_for 'submitted_test_request' %>
34 <%= error_messages_for 'submitted_test_request' %>
35 <% form_for :test_request, nil,
35 <% form_for :test_request, nil,
36 :url => { :action => 'submit'},
36 :url => { :action => 'submit'},
37 :html => { :multipart => true } do |f| %>
37 :html => { :multipart => true } do |f| %>
38 <table>
38 <table>
39 <tr>
39 <tr>
40 <td>Task:</td>
40 <td>Task:</td>
41 <td>
41 <td>
42 <%= select(:test_request,
42 <%= select(:test_request,
43 :problem_id,
43 :problem_id,
44 @problems.collect {|p| [p.name, p.id]}, {},
44 @problems.collect {|p| [p.name, p.id]}, {},
45 { :onclick => "updateSubmissionList();" }) %>
45 { :onclick => "updateSubmissionList();" }) %>
46 </td>
46 </td>
47 </tr>
47 </tr>
48 <tr>
48 <tr>
49 <td>Submission:</td>
49 <td>Submission:</td>
50 <td>
50 <td>
51 <%= select(:test_request,
51 <%= select(:test_request,
52 :submission_number,
52 :submission_number,
53 ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %>
53 ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %>
54 </td>
54 </td>
55 </tr>
55 </tr>
56 <tr>
56 <tr>
57 <td>Input data:</td>
57 <td>Input data:</td>
58 <td>
58 <td>
59 <%= f.file_field :input_file %>
59 <%= f.file_field :input_file %>
60 - (should be smaller than 2MB)
60 + </td>
61 + <td>
62 + (combined size should not exceed 2MB)
63 + </td>
64 + </tr>
65 + <tr>
66 + <td>
67 + Additional file<sup><span style="color:red">*</span></sup>:
61 </td>
68 </td>
69 + <td>
70 + <%= f.file_field :additional_file %>
71 + </td>
72 + <td>
73 + <small>
74 + * This option works <u>only</u> for task max.
75 + You can use this to submit <tt>questions.txt</tt>.<br/>
76 + The file shall be copied to the execution directory before your program runs.
77 + </small>
78 + </td>
79 + </tr>
62 <tr>
80 <tr>
63 <td colspan="2">
81 <td colspan="2">
64 <%= submit_tag 'submit' %>
82 <%= submit_tag 'submit' %>
65 </td>
83 </td>
66 </tr>
84 </tr>
67 </table>
85 </table>
68 <% end %>
86 <% end %>
69 </div>
87 </div>
70
88
71 <h3>Previous requests</h3>
89 <h3>Previous requests</h3>
72
90
73 <table class="info">
91 <table class="info">
74 <tr class="info-head">
92 <tr class="info-head">
75 <th>at</th>
93 <th>at</th>
76 <th>problem</th>
94 <th>problem</th>
77 <th>sub #</th>
95 <th>sub #</th>
78 <th>status</th>
96 <th>status</th>
79 <th>output (first 2kb)</th>
97 <th>output (first 2kb)</th>
80 <th>compiler message</th>
98 <th>compiler message</th>
81 <th>detail</th>
99 <th>detail</th>
82 </tr>
100 </tr>
83 <%= render :partial => 'test_request', :collection => @test_requests %>
101 <%= render :partial => 'test_request', :collection => @test_requests %>
84 </table>
102 </table>
85
103
86 <% end %>
104 <% end %>
87
105
You need to be logged in to leave comments. Login now