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:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r112:e81565ddb47a - - 2 files changed: 42 inserted, 1 deleted
@@ -56,79 +56,102 | |||
|
56 | 56 | begin |
|
57 | 57 | problem = Problem.find(params[:problem_id]) |
|
58 | 58 | rescue ActiveRecord::RecordNotFound |
|
59 | 59 | problem = nil |
|
60 | 60 | end |
|
61 | 61 | test_request.problem = problem |
|
62 | 62 | if problem!=nil |
|
63 | 63 | test_request.submission = |
|
64 | 64 | Submission.find_by_user_problem_number(user.id, |
|
65 | 65 | problem.id, |
|
66 | 66 | params[:submission_number]) |
|
67 | 67 | else |
|
68 | 68 | test_request.submission = nil |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | # checks if the user submits any input file |
|
72 | 72 | if params[:input_file]==nil or params[:input_file]=="" |
|
73 | 73 | test_request.errors.add_to_base("No input submitted.") |
|
74 | 74 | test_request.input_file_name = nil |
|
75 | 75 | else |
|
76 | 76 | test_request.input_file_name = save_input_file(params[:input_file], user, problem) |
|
77 | 77 | if test_request.input_file_name == nil |
|
78 | 78 | test_request.errors.add_to_base("No input submitted.") |
|
79 | 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 | 84 | end |
|
81 | 85 | test_request.submitted_at = Time.new |
|
82 | 86 | test_request.status_inqueue |
|
83 | 87 | test_request |
|
84 | 88 | end |
|
85 | 89 | |
|
86 | 90 | protected |
|
87 | 91 | |
|
88 | 92 | def self.name_of(problem) |
|
89 | 93 | if problem!=nil |
|
90 | 94 | problem.name |
|
91 | 95 | else |
|
92 | 96 | "default" |
|
93 | 97 | end |
|
94 | 98 | end |
|
95 | 99 | |
|
96 | 100 | def self.random_input_file_name(user,problem) |
|
97 | 101 | problem_name = TestRequest.name_of(problem) |
|
98 | 102 | begin |
|
99 | 103 | tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}" |
|
100 | 104 | end while File.exists?(tmpname) |
|
101 | 105 | tmpname |
|
102 | 106 | end |
|
103 | 107 | |
|
104 | 108 | def self.save_input_file(tempfile, user, problem) |
|
105 | 109 | new_file_name = random_input_file_name(user,problem) |
|
106 | 110 | dirname = File.dirname(new_file_name) |
|
107 | 111 | FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname) |
|
108 | 112 | |
|
109 | 113 | # when the user did not submit any file |
|
110 | 114 | return nil if tempfile=="" |
|
111 | 115 | |
|
112 | 116 | if tempfile.instance_of?(Tempfile) |
|
113 | 117 | tempfile.close |
|
114 | 118 | FileUtils.move(tempfile.path,new_file_name) |
|
115 | 119 | else |
|
116 | 120 | File.open(new_file_name, "wb") do |f| |
|
117 | 121 | f.write(tempfile.read) |
|
118 | 122 | end |
|
119 | 123 | end |
|
120 | 124 | new_file_name |
|
121 | 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 | 147 | # validations |
|
125 | 148 | # |
|
126 | 149 | def must_have_valid_problem |
|
127 | 150 | if problem==nil |
|
128 | 151 | errors.add('problem',"must be specified.") |
|
129 | 152 | elsif (!problem.available) and (self.new_record?) |
|
130 | 153 | errors.add('problem',"must be valid.") |
|
131 | 154 | end |
|
132 | 155 | end |
|
133 | 156 | |
|
134 | 157 | end |
@@ -36,50 +36,68 | |||
|
36 | 36 | :url => { :action => 'submit'}, |
|
37 | 37 | :html => { :multipart => true } do |f| %> |
|
38 | 38 | <table> |
|
39 | 39 | <tr> |
|
40 | 40 | <td>Task:</td> |
|
41 | 41 | <td> |
|
42 | 42 | <%= select(:test_request, |
|
43 | 43 | :problem_id, |
|
44 | 44 | @problems.collect {|p| [p.name, p.id]}, {}, |
|
45 | 45 | { :onclick => "updateSubmissionList();" }) %> |
|
46 | 46 | </td> |
|
47 | 47 | </tr> |
|
48 | 48 | <tr> |
|
49 | 49 | <td>Submission:</td> |
|
50 | 50 | <td> |
|
51 | 51 | <%= select(:test_request, |
|
52 | 52 | :submission_number, |
|
53 | 53 | ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %> |
|
54 | 54 | </td> |
|
55 | 55 | </tr> |
|
56 | 56 | <tr> |
|
57 | 57 | <td>Input data:</td> |
|
58 | 58 | <td> |
|
59 | 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 | 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 | 80 | <tr> |
|
63 | 81 | <td colspan="2"> |
|
64 | 82 | <%= submit_tag 'submit' %> |
|
65 | 83 | </td> |
|
66 | 84 | </tr> |
|
67 | 85 | </table> |
|
68 | 86 | <% end %> |
|
69 | 87 | </div> |
|
70 | 88 | |
|
71 | 89 | <h3>Previous requests</h3> |
|
72 | 90 | |
|
73 | 91 | <table class="info"> |
|
74 | 92 | <tr class="info-head"> |
|
75 | 93 | <th>at</th> |
|
76 | 94 | <th>problem</th> |
|
77 | 95 | <th>sub #</th> |
|
78 | 96 | <th>status</th> |
|
79 | 97 | <th>output (first 2kb)</th> |
|
80 | 98 | <th>compiler message</th> |
|
81 | 99 | <th>detail</th> |
|
82 | 100 | </tr> |
|
83 | 101 | <%= render :partial => 'test_request', :collection => @test_requests %> |
|
84 | 102 | </table> |
|
85 | 103 |
You need to be logged in to leave comments.
Login now