|
|
|
@ -28,6 +28,169 @@ 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 local_init
|
|
|
|
|
LocalShixun.delete_all
|
|
|
|
|
LocalMirrorRepository.delete_all
|
|
|
|
|
LocalShixunTagRepertoire.delete_all
|
|
|
|
|
LocalChallenge.delete_all
|
|
|
|
|
LocalTestSet.delete_all
|
|
|
|
|
LocalChallengeTag.delete_all
|
|
|
|
|
render :json => {status: 0, message: "success"}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", "test_set_score")
|
|
|
|
|
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
|
|
|
|
|
shixun_list = []
|
|
|
|
|
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", "test_set_score")
|
|
|
|
|
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
|
|
|
|
|
shixun_list << shixun.identifier
|
|
|
|
|
end
|
|
|
|
|
render :json => {status: 0, message: "success", identifier: shixun_list}
|
|
|
|
|
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")
|
|
|
|
|