# HG changeset patch # User Nattee Niparnan # Date 2017-01-24 04:59:16 # Node ID b4dd5e5f23ab000ff8f18adfeef1366d30a6717b # Parent 79a5d1c218bad4e2a02096a8c8448be4aee67823 - add view testcase toggle for each problem - refactor: move view testcase to testcase_controller - change flash rendering 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 @@ -39,11 +39,14 @@ def testcase_authorization #admin always has privileged + puts "haha" if @current_user.admin? return true end - unauthorized_redirect if GraderConfiguration["right.view_testcase"] + puts "hehe" + puts GraderConfiguration["right.view_testcase"] + unauthorized_redirect unless GraderConfiguration["right.view_testcase"] end protected @@ -108,13 +111,15 @@ def authorization return false unless authenticate + puts "haha 1" user = User.find(session[:user_id]) unless user.roles.detect { |role| - role.rights.detect{ |right| - right.controller == self.class.controller_name and - (right.action == 'all' or right.action == action_name) - } + role.rights.detect{ |right| + right.controller == self.class.controller_name and + (right.action == 'all' or right.action == action_name) + } } + puts "haha 2" flash[:notice] = 'You are not authorized to view the page you requested' #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') redirect_to :controller => 'main', :action => 'login' 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 @@ -135,6 +135,14 @@ end end + def toggle_view_testcase + @problem = Problem.find(params[:id]) + @problem.update_attributes(view_testcase: !(@problem.view_testcase?) ) + respond_to do |format| + format.js { } + end + end + def turn_all_off Problem.available.all.each do |problem| problem.available = false @@ -222,10 +230,6 @@ 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 --- a/app/controllers/testcases_controller.rb +++ b/app/controllers/testcases_controller.rb @@ -10,6 +10,14 @@ send_data @testcase.sol, type: 'text/plain', filename: "#{@testcase.problem.name}.#{@testcase.num}.sol" end + def show_problem + @problem = Problem.includes(:testcases).find(params[:problem_id]) + unless @current_user.admin? or @problem.view_testcase + flash[:error] = 'You cannot view the testcase of this problem' + redirect_to :controller => 'main', :action => 'list' + end + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -196,4 +196,27 @@ markdown.to_html.html_safe end + + BOOTSTRAP_FLASH_MSG = { + success: 'alert-success', + error: 'alert-danger', + alert: 'alert-block', + notice: 'alert-info' + } + + def bootstrap_class_for(flash_type) + BOOTSTRAP_FLASH_MSG.fetch(flash_type.to_sym, flash_type.to_s) + end + + def flash_messages + puts "flahs size = #{flash.count}" + flash.each do |msg_type, message| + concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do + concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' }) + concat message + end) + end + nil + end + end diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -11,5 +11,6 @@ %body = render 'layouts/header' - = content_tag(:p,flash[:notice],class: 'alert alert-success') if flash[:notice]!=nil + /= content_tag(:p,flash[:notice],class: 'alert alert-success') if flash[:notice]!=nil + = flash_messages = yield diff --git a/app/views/main/_login_box.html.haml b/app/views/main/_login_box.html.haml --- a/app/views/main/_login_box.html.haml +++ b/app/views/main/_login_box.html.haml @@ -6,9 +6,11 @@ %br/ %br/ + - puts flash.inspect - if flash[:notice] %hr/ %b= flash[:notice] + %b= haha %hr/ %div{ :style => "border: solid 1px gray; padding: 4px; background: #eeeeff;"} 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 @@ -25,5 +25,5 @@ = 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' + = link_to "testcases", show_problem_testcases_path(problem_id), class: 'btn btn-xs btn-info' diff --git a/app/views/problems/index.html.haml b/app/views/problems/index.html.haml --- a/app/views/problems/index.html.haml +++ b/app/views/problems/index.html.haml @@ -26,6 +26,9 @@ Avail? %sup{class: 'text-primary',data: {toggle: 'tooltip'}, title: 'Let user submits to this problem?' } [?] %th.text-center + View Data? + %sup{class: 'text-primary',data: {toggle: 'tooltip'}, title: 'Let user view the testcase of this problem?' } [?] + %th.text-center Test? %sup{class: 'text-primary',data: {toggle: 'tooltip'}, title: 'Let user uses test interface on this problem?' } [?] - if GraderConfiguration.multicontests? @@ -38,6 +41,7 @@ %td.text-right= problem.full_score #in_place_editor_field :problem, :full_score, {}, :rows=>1 %td= problem.date_added %td= toggle_button(@problem.available?, toggle_problem_path(@problem), "problem-avail-#{@problem.id}") + %td= toggle_button(@problem.view_testcase?, toggle_view_testcase_problem_path(@problem), "problem-view-testcase-#{@problem.id}") %td= toggle_button(@problem.test_allowed?, toggle_test_problem_path(@problem), "problem-test-#{@problem.id}") - if GraderConfiguration.multicontests? %td diff --git a/app/views/problems/show_testcase.html.haml b/app/views/problems/show_testcase.html.haml deleted file mode 100644 --- a/app/views/problems/show_testcase.html.haml +++ /dev/null @@ -1,25 +0,0 @@ -%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/app/views/problems/toggle_view_testcase.js.haml b/app/views/problems/toggle_view_testcase.js.haml new file mode 100644 --- /dev/null +++ b/app/views/problems/toggle_view_testcase.js.haml @@ -0,0 +1,2 @@ += render partial: 'toggle_button', + locals: {button_id: "#problem-view-testcase-#{@problem.id}",button_on: @problem.view_testcase?} diff --git a/app/views/testcases/show_problem.html.haml b/app/views/testcases/show_problem.html.haml new file mode 100644 --- /dev/null +++ b/app/views/testcases/show_problem.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_testcase_path(tc),class: 'btn btn-info btn-sm' + .col-md-6 + %h3 Output + = link_to "Download",download_sol_testcase_path(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 @@ -20,8 +20,8 @@ member do get 'toggle' get 'toggle_test' + get 'toggle_view_testcase' get 'stat' - get 'show_testcase' end collection do get 'turn_all_off' @@ -30,11 +30,15 @@ get 'manage' end - resources :testcases, only: [] do - member do - get 'download_input' - get 'download_sol' - end + end + + resources :testcases, only: [] do + member do + get 'download_input' + get 'download_sol' + end + collection do + get 'show_problem/:problem_id(/:test_num)' => 'testcases#show_problem', as: 'show_problem' end end diff --git a/db/migrate/20170124024527_add_view_testcase_to_problem.rb b/db/migrate/20170124024527_add_view_testcase_to_problem.rb new file mode 100644 --- /dev/null +++ b/db/migrate/20170124024527_add_view_testcase_to_problem.rb @@ -0,0 +1,5 @@ +class AddViewTestcaseToProblem < ActiveRecord::Migration + def change + add_column :problems, :view_testcase, :bool + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -11,16 +11,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170123162543) do +ActiveRecord::Schema.define(version: 20170124024527) do create_table "announcements", force: :cascade do |t| t.string "author", limit: 255 - t.text "body", limit: 16777215 + t.text "body", limit: 65535 t.boolean "published" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "frontpage", default: false - t.boolean "contest_only", default: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "frontpage", default: false + t.boolean "contest_only", default: false t.string "title", limit: 255 t.string "notes", limit: 255 end @@ -50,19 +50,19 @@ end create_table "descriptions", force: :cascade do |t| - t.text "body", limit: 16777215 + t.text "body", limit: 65535 t.boolean "markdowned" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "grader_configurations", force: :cascade do |t| t.string "key", limit: 255 t.string "value_type", limit: 255 t.string "value", limit: 255 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.text "description", limit: 16777215 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "description", limit: 65535 end create_table "grader_processes", force: :cascade do |t| @@ -107,10 +107,10 @@ t.integer "sender_id", limit: 4 t.integer "receiver_id", limit: 4 t.integer "replying_message_id", limit: 4 - t.text "body", limit: 16777215 + t.text "body", limit: 65535 t.boolean "replied" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "problems", force: :cascade do |t| @@ -124,6 +124,7 @@ t.boolean "test_allowed" t.boolean "output_only" t.string "description_filename", limit: 255 + t.boolean "view_testcase" end create_table "rights", force: :cascade do |t| @@ -152,7 +153,7 @@ create_table "sessions", force: :cascade do |t| t.string "session_id", limit: 255 - t.text "data", limit: 16777215 + t.text "data", limit: 65535 t.datetime "updated_at" end @@ -180,14 +181,14 @@ t.integer "user_id", limit: 4 t.integer "problem_id", limit: 4 t.integer "language_id", limit: 4 - t.text "source", limit: 16777215 + t.text "source", limit: 65535 t.binary "binary", limit: 65535 t.datetime "submitted_at" t.datetime "compiled_at" - t.text "compiler_message", limit: 16777215 + t.text "compiler_message", limit: 65535 t.datetime "graded_at" t.integer "points", limit: 4 - t.text "grader_comment", limit: 16777215 + t.text "grader_comment", limit: 65535 t.integer "number", limit: 4 t.string "source_filename", limit: 255 t.float "max_runtime", limit: 24 @@ -208,10 +209,10 @@ create_table "test_pairs", force: :cascade do |t| t.integer "problem_id", limit: 4 - t.text "input", limit: 4294967295 - t.text "solution", limit: 4294967295 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.text "input", limit: 16777215 + t.text "solution", limit: 16777215 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "test_requests", force: :cascade do |t| @@ -222,13 +223,13 @@ t.string "output_file_name", limit: 255 t.string "running_stat", limit: 255 t.integer "status", limit: 4 - t.datetime "updated_at", null: false + t.datetime "updated_at", null: false t.datetime "submitted_at" t.datetime "compiled_at" - t.text "compiler_message", limit: 16777215 + t.text "compiler_message", limit: 65535 t.datetime "graded_at" t.string "grader_comment", limit: 255 - t.datetime "created_at", null: false + t.datetime "created_at", null: false t.float "running_time", limit: 24 t.string "exit_status", limit: 255 t.integer "memory_usage", limit: 4 @@ -243,8 +244,8 @@ t.integer "score", limit: 4 t.text "input", limit: 4294967295 t.text "sol", limit: 4294967295 - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at" + t.datetime "updated_at" end add_index "testcases", ["problem_id"], name: "index_testcases_on_problem_id", using: :btree @@ -269,10 +270,10 @@ t.boolean "activated", default: false t.datetime "created_at" t.datetime "updated_at" - t.string "section", limit: 255 t.boolean "enabled", default: true t.string "remark", limit: 255 t.string "last_ip", limit: 255 + t.string "section", limit: 255 end add_index "users", ["login"], name: "index_users_on_login", unique: true, using: :btree