diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index a1e7299a..c6fa78d9 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -28,6 +28,156 @@ class WelcomeController < ApplicationController skip_before_filter :check_authentication, :only => [:index] require 'simple_xlsx_reader' + + DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z) + def shixun_to_local + identifiers = params[:identifiers].split(",") + shixuns = Shixun.where(identifier: identifiers) + # 不重复导入 + ActiveRecord::Base.transaction do + begin + shixuns.each do |shixun| + if LocalShixun.where(shixun_id: shixun.id).blank? + + local_shixun = LocalShixun.create!(name: shixun.name, description: shixun.description, user_id: User.current.id, status: 0, + trainee: shixun.trainee, webssh: shixun.webssh, multi_webssh: shixun.multi_webssh, + can_copy: shixun.can_copy, identifier: generate_identifier, shixun_id: shixun.id, + use_scope: shixun.use_scope, visits: 1, evaluate_script: shixun.evaluate_script) + + # 同步镜像 + if shixun.mirror_repositories.present? + shixun.mirror_repositories.each do |mirror| + LocalMirrorRepository.create!(:local_shixun_id => local_shixun.id, :mirror_repository_id => mirror.id) + end + end + + # 同步技术标签 + shixun.shixun_tag_repertoires.each do |str| + LocalShixunTagRepertoire.create!(:tag_repertoire_id => str.tag_repertoire_id, :local_shixun_id => local_shixun.id) + end + + # 不需要同步版本库,版本库应该是从本地导入到线上的时候由线上版本创建的 + + # 同步复制关卡 + if shixun.challenges.present? + shixun.challenges.each do |challenge| + new_challenge = LocalChallenge.new + new_challenge.attributes = challenge.attributes.dup.except("id","shixun_id","user_id") + new_challenge.local_shixun_id = local_shixun.id + new_challenge.save! + # 评测题,选择题暂时不考虑 + # 同步测试集 + if challenge.test_sets.present? + challenge.test_sets.each do |test_set| + new_test_set = LocalTestSet.new + new_test_set.attributes = test_set.attributes.dup.except("id","challenge_id") + new_test_set.local_challenge_id = new_challenge.id + new_test_set.save! + end + end + # 同步关卡标签 + challenge_tags = ChallengeTag.where("challenge_id =? and challenge_choose_id is null", challenge.id) + if challenge_tags.present? + challenge_tags.each do |challenge_tag| + LocalChallengeTag.create!(:local_challenge_id => new_challenge.id, :name => challenge_tag.try(:name)) + end + end + end + end + end + end + render :json => {status: 0, message: "同步成功"} + rescue Exception => e + logger.error("shixun_local_in ##{e.message}") + render :json => {status: -1, message: "同步失败,#{e.message}"} + raise ActiveRecord::Rollback + end + + end + end + + def local_to_shixun + ActiveRecord::Base.transaction do + LocalShixun.find_each do |local_shixun| + identifier = generate_identifier + shixun = Shixun.create!(name: local_shixun.name, description: local_shixun.description, user_id: User.current.id, + trainee: local_shixun.trainee, webssh: local_shixun.webssh, multi_webssh: local_shixun.multi_webssh, + can_copy: local_shixun.can_copy, identifier: identifier, reset_time: Time.now, + modify_time: Time.now, use_scope: local_shixun.use_scope, visits: 1, evaluate_script: local_shixun.evaluate_script) + m = ShixunMember.new(:user_id => User.current.id, :role => 1) + shixun.shixun_members << m + + # 同步镜像 + local_mirrors = LocalMirrorRepository.where(local_shixun_id: local_shixun.id) + if local_mirrors.present? + local_mirrors.each do |local_mirror| + ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => local_mirror.mirror_repository_id) + end + end + + # 同步技术标签 + local_shixun_tags = LocalShixunTagRepertoire.where(local_shixun_id: local_shixun.id) + if local_shixun_tags.present? + local_shixun_tags.each do |str| + ShixunTagRepertoire.create!(:tag_repertoire_id => str.tag_repertoire_id, :shixun_id => shixun.id) + end + end + + # 创建版本库 + repository = Repository.new + repository.shixun = shixun + repository.type = 'Repository::Gitlab' + repository.identifier = shixun.identifier.downcase + repository.project_id = -1 + repository.save! + s = Trustie::Gitlab::Sync.new + s.create_shixun(shixun, repository) + raise "版本库创建失败" if shixun.gpid.blank? # 若和gitlab没同步成功,则抛出异常 + g = Gitlab.client + shixun.update_column(:git_url, g.project(shixun.gpid).path_with_namespace) + + # 同步关卡信息 + local_challenges = LocalChallenge.where(local_shixun_id: local_shixun.id) + if local_challenges.present? + local_challenges.each do |local_challenge| + new_challenge = Challenge.new + new_challenge.attributes = local_challenge.attributes.dup.except("id","local_shixun_id","user_id") + new_challenge.user_id = User.current.id + new_challenge.shixun_id = shixun.id + new_challenge.save! + + # 同步测试集 + local_test_sets = LocalTestSet.where(local_challenge_id: local_challenge.id) + if local_test_sets.present? + local_test_sets.each do |local_test_set| + new_test_set = TestSet.new + new_test_set.attributes = local_test_set.attributes.dup.except("id","challenge_id") + new_test_set.challenge_id = new_challenge.id + new_test_set.save! + end + end + + # 同步关卡标签 + local_challenge_tags = LocalChallengeTag.where(local_challenge_id: local_challenge.id) + if local_challenge_tags.present? + local_challenge_tags.each do |local_challenge_tag| + ChallengeTag.create!(:challenge_id => new_challenge.id, :name => local_challenge_tag.try(:name)) + end + end + end + end + render :json => {status: 0, message: "success", identifier: shixun.identifier} + end + end + end + + + # 生成表示码 + def generate_identifier + code = DCODES.sample(8).join + return generate_identifier if Shixun.where(identifier: code).present? + code + end def index images = PortalImage.where(status: true).order("position asc") diff --git a/app/models/local_challenge.rb b/app/models/local_challenge.rb new file mode 100644 index 00000000..10cf4586 --- /dev/null +++ b/app/models/local_challenge.rb @@ -0,0 +1,3 @@ +class LocalChallenge < ActiveRecord::Base + +end diff --git a/app/models/local_challenge_tag.rb b/app/models/local_challenge_tag.rb new file mode 100644 index 00000000..9768ef30 --- /dev/null +++ b/app/models/local_challenge_tag.rb @@ -0,0 +1,3 @@ +class LocalChallengeTag < ActiveRecord::Base + +end diff --git a/app/models/local_mirror_repository.rb b/app/models/local_mirror_repository.rb new file mode 100644 index 00000000..d402a2df --- /dev/null +++ b/app/models/local_mirror_repository.rb @@ -0,0 +1,3 @@ +class LocalMirrorRepository < ActiveRecord::Base + +end diff --git a/app/models/local_shixun.rb b/app/models/local_shixun.rb new file mode 100644 index 00000000..b8480236 --- /dev/null +++ b/app/models/local_shixun.rb @@ -0,0 +1,3 @@ +class LocalShixun < ActiveRecord::Base + +end diff --git a/app/models/local_shixun_tag_repertoire.rb b/app/models/local_shixun_tag_repertoire.rb new file mode 100644 index 00000000..b3d2721b --- /dev/null +++ b/app/models/local_shixun_tag_repertoire.rb @@ -0,0 +1,2 @@ +class LocalShixunTagRepertoire < ActiveRecord::Base +end diff --git a/app/models/local_test_set.rb b/app/models/local_test_set.rb new file mode 100644 index 00000000..f4320b58 --- /dev/null +++ b/app/models/local_test_set.rb @@ -0,0 +1,3 @@ +class LocalTestSet < ActiveRecord::Base + +end diff --git a/app/models/shixun_tag_repertoire.rb b/app/models/shixun_tag_repertoire.rb index 83a52ee6..8164c98d 100644 --- a/app/models/shixun_tag_repertoire.rb +++ b/app/models/shixun_tag_repertoire.rb @@ -1,4 +1,4 @@ -class ShixunTagRepertoire < ActiveRecord::Base + class ShixunTagRepertoire < ActiveRecord::Base # attr_accessible :title, :body belongs_to :shixun belongs_to :tag_repertoire diff --git a/config/routes.rb b/config/routes.rb index 3acb9123..28319fd3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -281,6 +281,9 @@ RedmineApp::Application.routes.draw do ## oauth相关 end get 'welcome/ccf' => 'welcome#ccf' + get 'welcome/shixun_to_local' => 'welcome#shixun_to_local' + get 'welcome/local_to_shixun' => 'welcome#local_to_shixun' + # get 'competitions/home' => 'competitions#home' # get 'competitions/hn' => 'competitions#index' # get 'competitions/db' => 'competitions#db' diff --git a/db/migrate/20190329070441_migrate_homework_common_end_times.rb b/db/migrate/20190329070441_migrate_homework_common_end_times.rb index 0fa7b6cb..ac6bb087 100644 --- a/db/migrate/20190329070441_migrate_homework_common_end_times.rb +++ b/db/migrate/20190329070441_migrate_homework_common_end_times.rb @@ -1,12 +1,12 @@ class MigrateHomeworkCommonEndTimes < ActiveRecord::Migration def up - homework_commons = HomeworkCommon.where("id < 21549") - homework_commons.find_each do |homework| - sql = "update homework_commons set end_time = (select max(end_time) from - homework_seconds where id = #{homework.id}) where id = #{homework.id}" - - ActiveRecord::Base.connection.execute(sql) - end + # homework_commons = HomeworkCommon.where("id < 21549") + # homework_commons.find_each do |homework| + # sql = "update homework_commons set end_time = (select max(end_time) from + # homework_seconds where id = #{homework.id}) where id = #{homework.id}" + # + # ActiveRecord::Base.connection.execute(sql) + # end end def down diff --git a/db/migrate/20190329080718_migrate_homework_common_end_time2.rb b/db/migrate/20190329080718_migrate_homework_common_end_time2.rb index 65d4670a..19f048fe 100644 --- a/db/migrate/20190329080718_migrate_homework_common_end_time2.rb +++ b/db/migrate/20190329080718_migrate_homework_common_end_time2.rb @@ -1,11 +1,11 @@ class MigrateHomeworkCommonEndTime2 < ActiveRecord::Migration def up - homework_commons = HomeworkCommon.where("id >= 21549") - homework_commons.each do |homework| - if homework.publish_time.nil? - homework.update_column('end_time', nil) - end - end + # homework_commons = HomeworkCommon.where("id >= 21549") + # homework_commons.each do |homework| + # if homework.publish_time.nil? + # homework.update_column('end_time', nil) + # end + # end end def down diff --git a/db/migrate/20190412012134_create_local_shixuns.rb b/db/migrate/20190412012134_create_local_shixuns.rb new file mode 100644 index 00000000..39d5adae --- /dev/null +++ b/db/migrate/20190412012134_create_local_shixuns.rb @@ -0,0 +1,47 @@ +class CreateLocalShixuns < ActiveRecord::Migration + def change + create_table :local_shixuns do |t| + t.string :name + t.text :description + t.integer :user_id + t.integer :gpid + t.integer :visits + t.integer :status + t.string :language + t.text :authentication + t.string :identifier + t.text :propaedeutics + t.integer :trainee + t.integer :major_id + t.integer :webssh + t.boolean :homepage_show + t.boolean :hidden + t.integer :fork_from + t.boolean :can_copy + t.datetime :modifiy_tim + t.datetime :reset_time + t.integer :closer_id + t.datetime :end_time + t.string :git_url + t.boolean :vnc + t.integer :myshixuns_count + t.integer :challenges_count + t.integer :use_scope + t.text :evaluate_script + t.integer :mirror_script_id + t.string :image_text + t.boolean :code_hidden + t.boolean :task_pass + t.integer :exec_time + t.string :test_set_permission + t.boolean :sigle_training + t.boolean :hide_code + t.boolean :multi_webssh + t.integer :excute_time + t.integer :averge_star + t.integer :forbid_copy + + t.timestamps + end + end +end diff --git a/db/migrate/20190412025818_create_local_mirror_repositories.rb b/db/migrate/20190412025818_create_local_mirror_repositories.rb new file mode 100644 index 00000000..a47bff50 --- /dev/null +++ b/db/migrate/20190412025818_create_local_mirror_repositories.rb @@ -0,0 +1,10 @@ +class CreateLocalMirrorRepositories < ActiveRecord::Migration + def change + create_table :local_mirror_repositories do |t| + t.integer :local_shixun_id + t.integer :mirror_repository_id + + t.timestamps + end + end +end diff --git a/db/migrate/20190412033547_create_local_shixun_tag_repertoires.rb b/db/migrate/20190412033547_create_local_shixun_tag_repertoires.rb new file mode 100644 index 00000000..d870316a --- /dev/null +++ b/db/migrate/20190412033547_create_local_shixun_tag_repertoires.rb @@ -0,0 +1,10 @@ +class CreateLocalShixunTagRepertoires < ActiveRecord::Migration + def change + create_table :local_shixun_tag_repertoires do |t| + t.integer :tag_repertoire_id + t.integer :local_shixun_id + + t.timestamps + end + end +end diff --git a/db/migrate/20190412062718_create_local_challenges.rb b/db/migrate/20190412062718_create_local_challenges.rb new file mode 100644 index 00000000..156e2a4b --- /dev/null +++ b/db/migrate/20190412062718_create_local_challenges.rb @@ -0,0 +1,29 @@ +class CreateLocalChallenges < ActiveRecord::Migration + def change + create_table :local_challenges do |t| + t.integer :local_shixun_id + t.string :subject + t.integer :status + t.integer :position + t.text :task_pass + t.text :answer + t.integer :score + t.integer :visits + t.string :path + t.integer :evaluation_way + t.integer :difficulty + t.string :exec_path + t.integer :code_line + t.integer :st + t.text :web_route + t.text :picture_path + t.text :expect_picture_path + t.integer :challenge_tags_count + t.datetime :modify_time + t.string :original_picture_path + t.integer :show_type + + t.timestamps + end + end +end diff --git a/db/migrate/20190412062923_create_local_test_sets.rb b/db/migrate/20190412062923_create_local_test_sets.rb new file mode 100644 index 00000000..1ec6e0af --- /dev/null +++ b/db/migrate/20190412062923_create_local_test_sets.rb @@ -0,0 +1,14 @@ +class CreateLocalTestSets < ActiveRecord::Migration + def change + create_table :local_test_sets do |t| + t.text :input + t.text :output + t.integer :local_challenge_id + t.integer :is_public + t.integer :result + t.integer :position + + t.timestamps + end + end +end diff --git a/db/migrate/20190412063622_create_local_challenge_tags.rb b/db/migrate/20190412063622_create_local_challenge_tags.rb new file mode 100644 index 00000000..0b35a12e --- /dev/null +++ b/db/migrate/20190412063622_create_local_challenge_tags.rb @@ -0,0 +1,10 @@ +class CreateLocalChallengeTags < ActiveRecord::Migration + def change + create_table :local_challenge_tags do |t| + t.string :name + t.integer :local_challenge_id + + t.timestamps + end + end +end diff --git a/db/migrate/20190412073015_add_test_set_score_to_local_challenge.rb b/db/migrate/20190412073015_add_test_set_score_to_local_challenge.rb new file mode 100644 index 00000000..49661522 --- /dev/null +++ b/db/migrate/20190412073015_add_test_set_score_to_local_challenge.rb @@ -0,0 +1,5 @@ +class AddTestSetScoreToLocalChallenge < ActiveRecord::Migration + def change + add_column :local_challenges, :test_set_score, :integer + end +end diff --git a/db/migrate/20190412074140_add_shixun_id_to_local_shixun.rb b/db/migrate/20190412074140_add_shixun_id_to_local_shixun.rb new file mode 100644 index 00000000..15d9853c --- /dev/null +++ b/db/migrate/20190412074140_add_shixun_id_to_local_shixun.rb @@ -0,0 +1,5 @@ +class AddShixunIdToLocalShixun < ActiveRecord::Migration + def change + add_column :local_shixuns, :shixun_id, :integer + end +end diff --git a/db/migrate/20190412080258_add_score_to_local_test_sets.rb b/db/migrate/20190412080258_add_score_to_local_test_sets.rb new file mode 100644 index 00000000..8015badd --- /dev/null +++ b/db/migrate/20190412080258_add_score_to_local_test_sets.rb @@ -0,0 +1,5 @@ +class AddScoreToLocalTestSets < ActiveRecord::Migration + def change + add_column :local_test_sets, :score, :integer + end +end diff --git a/lib/tasks/shixun_local.rake b/lib/tasks/shixun_local.rake new file mode 100644 index 00000000..e69de29b diff --git a/spec/factories/local_challenge_tags.rb b/spec/factories/local_challenge_tags.rb new file mode 100644 index 00000000..033d487b --- /dev/null +++ b/spec/factories/local_challenge_tags.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :local_challenge_tag do + name "MyString" + local_challenge_id 1 + end +end diff --git a/spec/factories/local_challenges.rb b/spec/factories/local_challenges.rb new file mode 100644 index 00000000..f8094a5c --- /dev/null +++ b/spec/factories/local_challenges.rb @@ -0,0 +1,25 @@ +FactoryGirl.define do + factory :local_challenge do + local_shixun_id 1 + subject "MyString" + status 1 + position 1 + task_pass "" + answer "" + score 1 + visits 1 + path "MyString" + evaluation_way 1 + difficulty 1 + exec_path "MyString" + code_line 1 + st 1 + web_route "MyText" + picture_path "MyText" + expect_picture_path "MyText" + challenge_tags_count 1 + modify_time "2019-04-12 14:27:18" + original_picture_path "MyString" + show_type 1 + end +end diff --git a/spec/factories/local_mirror_repositories.rb b/spec/factories/local_mirror_repositories.rb new file mode 100644 index 00000000..c56d4106 --- /dev/null +++ b/spec/factories/local_mirror_repositories.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :local_mirror_repository do + local_shixun_id 1 + mirror_repository_id 1 + end +end diff --git a/spec/factories/local_shixun_tag_repertoires.rb b/spec/factories/local_shixun_tag_repertoires.rb new file mode 100644 index 00000000..c0368634 --- /dev/null +++ b/spec/factories/local_shixun_tag_repertoires.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :local_shixun_tag_repertoire do + tag_repertoire_id 1 + local_shixun_id 1 + end +end diff --git a/spec/factories/local_shixuns.rb b/spec/factories/local_shixuns.rb new file mode 100644 index 00000000..c0e0b736 --- /dev/null +++ b/spec/factories/local_shixuns.rb @@ -0,0 +1,37 @@ +FactoryGirl.define do + factory :local_shixun do + name "MyString" + description "MyText" + user_id 1 + gpid 1 + visits 1 + status 1 + language "MyString" + authentication false + identifier "MyString" + propaedeutics "MyText" + trainee 1 + major_id 1 + webssh 1 + homepage_show false + hidden false + fork_from 1 + can_copy false + modifiy_tim "2019-04-12 09:21:34" + reset_time "" + closer_id 1 + end_time "2019-04-12 09:21:34" + git_url "MyString" + vnc false + myshixuns_count 1 + challenges_count 1 + use_scope 1 + evaluate_script "" + mirror_script_id 1 + image_text "MyString" + code_hidden false + task_pass false + exec_time 1 + test_set "MyString" + end +end diff --git a/spec/factories/local_test_sets.rb b/spec/factories/local_test_sets.rb new file mode 100644 index 00000000..792da326 --- /dev/null +++ b/spec/factories/local_test_sets.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + factory :local_test_set do + input "" + output "" + local_challenge_id 1 + is_public 1 + result 1 + position 1 + end +end diff --git a/spec/models/local_challenge_spec.rb b/spec/models/local_challenge_spec.rb new file mode 100644 index 00000000..b123e68c --- /dev/null +++ b/spec/models/local_challenge_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalChallenge, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/local_challenge_tag_spec.rb b/spec/models/local_challenge_tag_spec.rb new file mode 100644 index 00000000..7aac14ed --- /dev/null +++ b/spec/models/local_challenge_tag_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalChallengeTag, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/local_mirror_repository_spec.rb b/spec/models/local_mirror_repository_spec.rb new file mode 100644 index 00000000..d88aab72 --- /dev/null +++ b/spec/models/local_mirror_repository_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalMirrorRepository, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/local_shixun_spec.rb b/spec/models/local_shixun_spec.rb new file mode 100644 index 00000000..deff5715 --- /dev/null +++ b/spec/models/local_shixun_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalShixun, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/local_shixun_tag_repertoire_spec.rb b/spec/models/local_shixun_tag_repertoire_spec.rb new file mode 100644 index 00000000..f3d780b0 --- /dev/null +++ b/spec/models/local_shixun_tag_repertoire_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalShixunTagRepertoire, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/local_test_set_spec.rb b/spec/models/local_test_set_spec.rb new file mode 100644 index 00000000..117ff963 --- /dev/null +++ b/spec/models/local_test_set_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LocalTestSet, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end