# HG changeset patch # User Jittat Fakcharoenphol # Date 2010-01-17 22:49:43 # Node ID 0c551aa1f64a9fc028b17eee1c4663e2451761fa # Parent 491c30075d87b87c35cb5a8448c5278350754737 imports test pairs diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -150,21 +150,17 @@ end def do_import - @problem, import_log = Problem.new_from_import_form_params(params) + old_problem = Problem.find_by_name(params[:name]) + @problem, import_log = Problem.create_from_import_form_params(params, + old_problem) if @problem.errors.length != 0 render :action => 'import' and return end - old_problem = Problem.find_by_name(@problem.name) if old_problem!=nil - old_problem.full_name = @problem.full_name - @problem = old_problem - flash[:notice] = "The test data has been replaced for problem #{@problem.name}" end - - @problem.save @log = import_log end diff --git a/app/models/problem.rb b/app/models/problem.rb --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,7 +1,7 @@ class Problem < ActiveRecord::Base belongs_to :description - has_many :test_pairs + has_many :test_pairs, :dependent => :delete_all validates_presence_of :name validates_format_of :name, :with => /^\w+$/ @@ -14,21 +14,12 @@ find(:all, :conditions => {:available => true}, :order => "date_added DESC") end - def self.new_from_import_form_params(params) - problem = Problem.new + def self.create_from_import_form_params(params, old_problem=nil) + problem = old_problem || Problem.new import_params = Problem.extract_params_and_check(params, problem) if not problem.valid? - return problem - end - - importer = TestdataImporter.new - - if not importer.import_from_file(problem.name, - import_params[:file], - import_params[:time_limit], - import_params[:memory_limit]) - problem.errors.add_to_base('Import error.') + return problem, 'Error importing' end problem.full_score = 100 @@ -36,6 +27,22 @@ problem.test_allowed = true problem.output_only = false problem.available = false + + if not problem.save + return problem, 'Error importing' + end + + import_to_db = params.has_key? :import_to_db + + importer = TestdataImporter.new(problem) + + if not importer.import_from_file(import_params[:file], + import_params[:time_limit], + import_params[:memory_limit], + import_to_db) + problem.errors.add_to_base('Import error.') + end + return problem, importer.log_msg end diff --git a/app/views/problems/import.html.haml b/app/views/problems/import.html.haml --- a/app/views/problems/import.html.haml +++ b/app/views/problems/import.html.haml @@ -22,7 +22,13 @@ %span{:class => 'help'} Leave blank to use the same value as the name above. %tr %td Testdata file: - %td= file_field_tag 'file' + %td + = file_field_tag 'file' + %tr + %td + %td + = check_box_tag 'import_to_db' + Import test data to database (for a test-pair task) %tr %td Time limit: %td diff --git a/lib/testdata_importer.rb b/lib/testdata_importer.rb --- a/lib/testdata_importer.rb +++ b/lib/testdata_importer.rb @@ -4,17 +4,33 @@ attr :log_msg - def import_from_file(problem_name, - tempfile, + def initialize(problem) + @problem = problem + end + + def import_from_file(tempfile, time_limit, - memory_limit) + memory_limit, + import_to_db=false) - dirname = TestdataImporter.extract(problem_name, tempfile) + dirname = extract(tempfile) return false if not dirname - @log_msg = GraderScript.call_import_problem(problem_name, - dirname, - time_limit, - memory_limit) + if not import_to_db + @log_msg = GraderScript.call_import_problem(@problem.name, + dirname, + time_limit, + memory_limit) + else + # Import test data to test pairs. + + @problem.test_pairs.clear + if import_test_pairs(dirname) + test_pair_count = TestPair.count :conditions => "problem_id = #{@problem.id}" + @log_msg = "Importing test pair successful. (#{test_pair_count} test pairs imported)" + else + @log_msg = "Importing test pair failed. (0 test pairs imported)" + end + end return true end @@ -26,12 +42,11 @@ return filename.slice(i..len) end - def self.extract(problem_name, tempfile) - testdata_filename = TestdataImporter.save_testdata_file(problem_name, - tempfile) + def extract(tempfile) + testdata_filename = save_testdata_file(tempfile) ext = TestdataImporter.long_ext(tempfile.original_filename) - extract_dir = File.join(GraderScript.raw_dir, problem_name) + extract_dir = File.join(GraderScript.raw_dir, @problem.name) begin Dir.mkdir extract_dir rescue Errno::EEXIST @@ -55,9 +70,9 @@ return File.dirname(files[0]) end - def self.save_testdata_file(problem_name, tempfile) + def save_testdata_file(tempfile) ext = TestdataImporter.long_ext(tempfile.original_filename) - testdata_filename = File.join(Dir.tmpdir,"#{problem_name}#{ext}") + testdata_filename = File.join(Dir.tmpdir,"#{@problem.name}#{ext}") return nil if tempfile=="" @@ -73,4 +88,22 @@ return testdata_filename end + def import_test_pairs(dirname) + test_num = 1 + while FileTest.exists? "#{dirname}/#{test_num}.in" + in_filename = "#{dirname}/#{test_num}.in" + sol_filename = "#{dirname}/#{test_num}.sol" + + break if not FileTest.exists? sol_filename + + test_pair = TestPair.new(:input => open(in_filename).read, + :solution => open(sol_filename).read, + :problem => @problem) + break if not test_pair.save + + test_num += 1 + end + return test_num > 1 + end + end