Description:
added problem description import
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r211:3a4974a6747f - - 3 files changed: 35 inserted, 3 deleted

@@ -1,49 +1,58
1 - content_for :head do
1 - content_for :head do
2 = stylesheet_link_tag 'problems'
2 = stylesheet_link_tag 'problems'
3 = javascript_include_tag :defaults
3 = javascript_include_tag :defaults
4
4
5 %h1 Import problems
5 %h1 Import problems
6
6
7 %p= link_to '[Back to problem list]', :action => 'list'
7 %p= link_to '[Back to problem list]', :action => 'list'
8
8
9 - if @problem and @problem.errors
9 - if @problem and @problem.errors
10 =error_messages_for 'problem'
10 =error_messages_for 'problem'
11
11
12 - form_tag({:action => 'do_import'}, :multipart => true) do
12 - form_tag({:action => 'do_import'}, :multipart => true) do
13 .submitbox
13 .submitbox
14 %table
14 %table
15 %tr
15 %tr
16 %td Name:
16 %td Name:
17 %td= text_field_tag 'name'
17 %td= text_field_tag 'name'
18 %tr
18 %tr
19 %td Full name:
19 %td Full name:
20 %td
20 %td
21 = text_field_tag 'full_name'
21 = text_field_tag 'full_name'
22 %span{:class => 'help'} Leave blank to use the same value as the name above.
22 %span{:class => 'help'} Leave blank to use the same value as the name above.
23 %tr
23 %tr
24 %td Testdata file:
24 %td Testdata file:
25 + %td= file_field_tag 'file'
26 + %tr
25 %td
27 %td
26 - = file_field_tag 'file'
28 + %td
29 + %span{:class => 'help'}
30 + In .zip, .tgz, tar.gz, .tar format.
31 + It should includes inputs (e.g., 1.in, 2a.in, 2b.in)
32 + and solutions (e.g., 1.sol, 2a.sol, 2b.sol).
33 + %br/
34 + You may put task description in *.html for raw html
35 + and *.md or *.markdown for markdown.
27 %tr
36 %tr
28 %td
37 %td
29 %td
38 %td
30 = check_box_tag 'import_to_db'
39 = check_box_tag 'import_to_db'
31 Import test data to database (for a test-pair task)
40 Import test data to database (for a test-pair task)
32 %tr
41 %tr
33 %td Time limit:
42 %td Time limit:
34 %td
43 %td
35 = text_field_tag 'time_limit'
44 = text_field_tag 'time_limit'
36 %span{:class => 'help'} In seconds. Leave blank to use 1 sec.
45 %span{:class => 'help'} In seconds. Leave blank to use 1 sec.
37 %tr
46 %tr
38 %td Memory limit:
47 %td Memory limit:
39 %td
48 %td
40 = text_field_tag 'memory_limit'
49 = text_field_tag 'memory_limit'
41 %span{:class => 'help'} In MB. Leave blank to use 32MB.
50 %span{:class => 'help'} In MB. Leave blank to use 32MB.
42 %tr
51 %tr
43 %td
52 %td
44 %td= submit_tag 'Import problem'
53 %td= submit_tag 'Import problem'
45
54
46 - if @log
55 - if @log
47 %h3 Import log
56 %h3 Import log
48 %pre.import-log
57 %pre.import-log
49 = @log
58 = @log
@@ -1,22 +1,20
1 <tr class="name">
1 <tr class="name">
2 <td>
2 <td>
3 <a name="<%= problem.name %>"></a>
3 <a name="<%= problem.name %>"></a>
4 <%= "#{problem.full_name} (#{problem.name})" %>
4 <%= "#{problem.full_name} (#{problem.name})" %>
5 </td>
5 </td>
6 </tr>
6 </tr>
7 <tr>
7 <tr>
8 <% td_class = (problem_counter%2==0) ? "desc-even" : "desc-odd" %>
8 <% td_class = (problem_counter%2==0) ? "desc-even" : "desc-odd" %>
9 <td class="<%= td_class %>">
9 <td class="<%= td_class %>">
10 <% if problem.description!=nil %>
10 <% if problem.description!=nil %>
11 <% if problem.description.markdowned %>
11 <% if problem.description.markdowned %>
12 <%= markdown(problem.description.body) %>
12 <%= markdown(problem.description.body) %>
13 <% else %>
13 <% else %>
14 - <pre>
15 <%= problem.description.body %>
14 <%= problem.description.body %>
16 - </pre>
17 <% end %>
15 <% end %>
18 <% else %>
16 <% else %>
19 (not available)
17 (not available)
20 <% end %>
18 <% end %>
21 </td>
19 </td>
22 </tr>
20 </tr>
@@ -10,48 +10,51
10
10
11 def import_from_file(tempfile,
11 def import_from_file(tempfile,
12 time_limit,
12 time_limit,
13 memory_limit,
13 memory_limit,
14 import_to_db=false)
14 import_to_db=false)
15
15
16 dirname = extract(tempfile)
16 dirname = extract(tempfile)
17 return false if not dirname
17 return false if not dirname
18 if not import_to_db
18 if not import_to_db
19 @log_msg = GraderScript.call_import_problem(@problem.name,
19 @log_msg = GraderScript.call_import_problem(@problem.name,
20 dirname,
20 dirname,
21 time_limit,
21 time_limit,
22 memory_limit)
22 memory_limit)
23 else
23 else
24 # Import test data to test pairs.
24 # Import test data to test pairs.
25
25
26 @problem.test_pairs.clear
26 @problem.test_pairs.clear
27 if import_test_pairs(dirname)
27 if import_test_pairs(dirname)
28 test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}"
28 test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}"
29 @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)"
29 @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)"
30 else
30 else
31 @log_msg = "Importing test pair failed. (0 test pairs imported)"
31 @log_msg = "Importing test pair failed. (0 test pairs imported)"
32 end
32 end
33 end
33 end
34 +
35 + @log_msg << import_problem_description(dirname)
36 +
34 return true
37 return true
35 end
38 end
36
39
37 protected
40 protected
38
41
39 def self.long_ext(filename)
42 def self.long_ext(filename)
40 i = filename.index('.')
43 i = filename.index('.')
41 len = filename.length
44 len = filename.length
42 return filename.slice(i..len)
45 return filename.slice(i..len)
43 end
46 end
44
47
45 def extract(tempfile)
48 def extract(tempfile)
46 testdata_filename = save_testdata_file(tempfile)
49 testdata_filename = save_testdata_file(tempfile)
47 ext = TestdataImporter.long_ext(tempfile.original_filename)
50 ext = TestdataImporter.long_ext(tempfile.original_filename)
48
51
49 extract_dir = File.join(GraderScript.raw_dir, @problem.name)
52 extract_dir = File.join(GraderScript.raw_dir, @problem.name)
50 begin
53 begin
51 Dir.mkdir extract_dir
54 Dir.mkdir extract_dir
52 rescue Errno::EEXIST
55 rescue Errno::EEXIST
53 end
56 end
54
57
55 if ext=='.tar.gz' or ext=='.tgz'
58 if ext=='.tar.gz' or ext=='.tgz'
56 cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}"
59 cmd = "tar -zxvf #{testdata_filename} -C #{extract_dir}"
57 elsif ext=='.tar'
60 elsif ext=='.tar'
@@ -85,25 +88,47
85 end
88 end
86 end
89 end
87
90
88 return testdata_filename
91 return testdata_filename
89 end
92 end
90
93
91 def import_test_pairs(dirname)
94 def import_test_pairs(dirname)
92 test_num = 1
95 test_num = 1
93 while FileTest.exists? "#{dirname}/#{test_num}.in"
96 while FileTest.exists? "#{dirname}/#{test_num}.in"
94 in_filename = "#{dirname}/#{test_num}.in"
97 in_filename = "#{dirname}/#{test_num}.in"
95 sol_filename = "#{dirname}/#{test_num}.sol"
98 sol_filename = "#{dirname}/#{test_num}.sol"
96
99
97 break if not FileTest.exists? sol_filename
100 break if not FileTest.exists? sol_filename
98
101
99 test_pair = TestPair.new(:input => open(in_filename).read,
102 test_pair = TestPair.new(:input => open(in_filename).read,
100 :solution => open(sol_filename).read,
103 :solution => open(sol_filename).read,
101 :problem => @problem)
104 :problem => @problem)
102 break if not test_pair.save
105 break if not test_pair.save
103
106
104 test_num += 1
107 test_num += 1
105 end
108 end
106 return test_num > 1
109 return test_num > 1
107 end
110 end
108
111
112 + def import_problem_description(dirname)
113 + html_files = Dir["#{dirname}/*.html"]
114 + markdown_files = Dir["#{dirname}/*.md"] + Dir["#{dirname}/*.markdown"]
115 + if (html_files.length != 0) or (markdown_files.length != 0)
116 + description = @problem.description || Description.new
117 +
118 + if html_files.length != 0
119 + filename = html_files[0]
120 + description.markdowned = false
121 + else
122 + filename = markdown_files[0]
123 + description.markdowned = true
109 end
124 end
125 +
126 + description.body = open(filename).read
127 + description.save
128 + @problem.description = description
129 + @problem.save
130 + return "\nProblem description imported from #{filename}."
131 + end
132 + end
133 +
134 + end
You need to be logged in to leave comments. Login now