# HG changeset patch # User Nattee Niparnan # Date 2017-01-05 09:47:21 # Node ID 7645a0f771ce3e7f7fcec6f41735f500d7cb1378 # Parent 8852e61287639a30fff0ea99dab2fcb6f95a0365 add show testcase feature diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,6 +37,15 @@ end end + def testcase_authorization + #admin always has privileged + if @current_user.admin? + return true + end + + unauthorized_redirect if GraderConfiguration["right.view_testcase"] + end + protected def authenticate 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 @@ -1,6 +1,7 @@ class ProblemsController < ApplicationController - before_filter :authenticate, :authorization + before_action :authenticate, :authorization + before_action :testcase_authorization, only: [:show_testcase] in_place_edit_for :problem, :name in_place_edit_for :problem, :full_name @@ -221,6 +222,10 @@ redirect_to :action => 'manage' end + def show_testcase + @problem = Problem.includes(:testcases).find(params[:id]) + end + ################################## protected diff --git a/app/controllers/testcases_controller.rb b/app/controllers/testcases_controller.rb new file mode 100644 --- /dev/null +++ b/app/controllers/testcases_controller.rb @@ -0,0 +1,24 @@ +class TestcasesController < ApplicationController + before_action :set_testcase, only: [:download_input,:download_sol] + before_action :testcase_authorization + + def download_input + send_data @testcase.input, type: 'text/plain', filename: "#{@testcase.problem.name}.#{@testcase.num}.in" + end + + def download_sol + send_data @testcase.sol, type: 'text/plain', filename: "#{@testcase.problem.name}.#{@testcase.num}.sol" + end + + + private + # Use callbacks to share common setup or constraints between actions. + def set_testcase + @testcase = Testcase.find(params[:id]) + end + + # Only allow a trusted parameter "white list" through. + def testcase_params + params[:testcase] + end +end diff --git a/app/helpers/testcases_helper.rb b/app/helpers/testcases_helper.rb new file mode 100644 --- /dev/null +++ b/app/helpers/testcases_helper.rb @@ -0,0 +1,2 @@ +module TestcasesHelper +end diff --git a/app/models/grader_configuration.rb b/app/models/grader_configuration.rb --- a/app/models/grader_configuration.rb +++ b/app/models/grader_configuration.rb @@ -10,6 +10,7 @@ MULTICONTESTS_KEY = 'system.multicontests' CONTEST_TIME_LIMIT_KEY = 'contest.time_limit' MULTIPLE_IP_LOGIN_KEY = 'right.multiple_ip_login' + VIEW_TESTCASE = 'right.view_testcase' cattr_accessor :config_cache cattr_accessor :task_grading_info_cache @@ -70,6 +71,10 @@ return (get(SYSTEM_MODE_CONF_KEY)=='analysis') end + def self.show_testcase + return get(VIEW_TESTCASE) + end + def self.allow_test_request(user) mode = get(SYSTEM_MODE_CONF_KEY) early_timeout = get(TEST_REQUEST_EARLY_TIMEOUT_KEY) diff --git a/app/views/main/_submission_short.html.haml b/app/views/main/_submission_short.html.haml --- a/app/views/main/_submission_short.html.haml +++ b/app/views/main/_submission_short.html.haml @@ -24,4 +24,6 @@ = link_to "#{t 'main.cmp_msg'}", compiler_msg_submission_path(submission.id), {popup: true,remote: true,class: 'btn btn-xs btn-info'} = link_to "#{t 'main.src_link'}",{:action => 'source', :id => submission.id}, class: 'btn btn-xs btn-info' = link_to "#{t 'main.submissions_link'}", problem_submissions_path(problem_id), class: 'btn btn-xs btn-info' + - if GraderConfiguration.show_testcase + = link_to "testcases", show_testcase_problem_path(problem_id), class: 'btn btn-xs btn-info' diff --git a/app/views/problems/show_testcase.html.haml b/app/views/problems/show_testcase.html.haml new file mode 100644 --- /dev/null +++ b/app/views/problems/show_testcase.html.haml @@ -0,0 +1,25 @@ +%h1 Test cases +%h2= @problem.long_name + +/navbar +%ul.nav.nav-pills{role: :tablist} + - @problem.testcases.each.with_index do |tc,id| + %li{role: :presentation, class: ('active' if id == 0)} + %a{href:"#tc#{tc.id}", role: 'tab', data: {toggle: 'tab'}}= tc.num + +/actual data +.tab-content + - @problem.testcases.each.with_index do |tc,id| + .tab-pane{id: "tc#{tc.id}",class: ('active' if id == 0)} + .row + .col-md-6 + %h3 Input + = link_to "Download",download_input_problem_testcase_path(@problem,tc),class: 'btn btn-info btn-sm' + .col-md-6 + %h3 Output + = link_to "Download",download_sol_problem_testcase_path(@problem,tc),class: 'btn btn-info btn-sm' + .row + .col-md-6 + %textarea{ rows: 25,readonly: true,style: "width:100%;resize=none;overflow-y: scroll;"}= tc.input + .col-md-6 + %textarea{ rows: 25,readonly: true,style: "width:100%;resize=none;overflow-y: scroll;"}= tc.sol diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ get 'toggle' get 'toggle_test' get 'stat' + get 'show_testcase' end collection do get 'turn_all_off' @@ -25,6 +26,13 @@ get 'import' get 'manage' end + + resources :testcases, only: [] do + member do + get 'download_input' + get 'download_sol' + end + end end resources :grader_configuration, controller: 'configurations' diff --git a/db/seeds.rb b/db/seeds.rb --- a/db/seeds.rb +++ b/db/seeds.rb @@ -89,6 +89,12 @@ :description => 'Heart beat response text' }, + { + :key => 'right.view_testcase', + :value_type => 'boolean', + :default_value => 'false', + :description => 'When true, any user can view/download test data' + }, # If Configuration['system.online_registration'] is true, the # system allows online registration, and will use these # information for sending confirmation emails. diff --git a/lib/grader_script.rb b/lib/grader_script.rb --- a/lib/grader_script.rb +++ b/lib/grader_script.rb @@ -49,10 +49,25 @@ output = `#{cmd}` Dir.chdir(cur_dir) - + return "import CMD: #{cmd}\n" + output end return '' end + def self.call_import_testcase(problem_name) + if GraderScript.grader_control_enabled? + cur_dir = `pwd`.chomp + Dir.chdir(GRADER_ROOT_DIR) + + script_name = File.join(GRADER_ROOT_DIR, "scripts/load_testcase") + cmd = "#{script_name} #{problem_name}" + + output = `#{cmd}` + + Dir.chdir(cur_dir) + return "Testcase import result:\n" + output + end + end + end diff --git a/lib/testdata_importer.rb b/lib/testdata_importer.rb --- a/lib/testdata_importer.rb +++ b/lib/testdata_importer.rb @@ -38,6 +38,9 @@ @log_msg << import_problem_pdf(dirname) @log_msg << import_full_score(dirname) + #import test data + @log_msg << GraderScript.call_import_testcase(@problem.name) + return true end diff --git a/test/controllers/testcases_controller_test.rb b/test/controllers/testcases_controller_test.rb new file mode 100644 --- /dev/null +++ b/test/controllers/testcases_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TestcasesControllerTest < ActionController::TestCase + setup do + @testcase = testcases(:one) + end +end