本地实践课程导入

dev_aliyun
cxt 5 years ago
parent 4bb10aa183
commit f3b2aa6f69

@ -31,6 +31,9 @@ class WelcomeController < ApplicationController
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) 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 def local_init
LocalSubject.delete_all
LocalStage.delete_all
LocalStageShixun.delete_all
LocalShixun.delete_all LocalShixun.delete_all
LocalMirrorRepository.delete_all LocalMirrorRepository.delete_all
LocalShixunTagRepertoire.delete_all LocalShixunTagRepertoire.delete_all
@ -63,65 +66,56 @@ class WelcomeController < ApplicationController
end end
end end
def shixun_to_local def subject_to_local
identifiers = params[:identifiers].split(",") subject_ids = params[:subject_ids].split(",")
shixuns = Shixun.where(identifier: identifiers) subjects = Subject.where(id: subject_ids)
# 不重复导入
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
begin subjects.each do |subject|
shixuns.each do |shixun| if LocalSubject.where(subject_id: subject.id).blank?
if LocalShixun.where(shixun_id: shixun.id).blank?
local_subject = LocalSubject.create!(name: subject.name, description: subject.description, status: 0,
local_shixun = LocalShixun.create!(name: shixun.name, description: shixun.description, user_id: User.current.id, status: 0, learning_notes: subject.learning_notes, introduction: subject.introduction,
trainee: shixun.trainee, webssh: shixun.webssh, multi_webssh: shixun.multi_webssh, stages_count: subject.stages_count, stage_shixuns_count: subject.stage_shixuns_count,
can_copy: shixun.can_copy, identifier: generate_identifier, shixun_id: shixun.id, subject_id: subject.id, shixuns_count: subject.shixuns_count)
use_scope: shixun.use_scope, visits: 1, evaluate_script: shixun.evaluate_script,
local_giturl: shixun.git_url) # 同步复制章节
if subject.stages.present?
# 同步镜像 subject.stages.each do |stage|
if shixun.mirror_repositories.present? new_stage = LocalStage.new
shixun.mirror_repositories.each do |mirror| new_stage.attributes = stage.attributes.dup.except("id","user_id")
# 本地版的mirror id和线上的可能不一样所以按名字取然后再存 new_stage.local_subject_id = local_subject.id
# local_mirror = MirrorRepository.where(type_name: mirror.type_name).first.try(:id) new_stage.save!
LocalMirrorRepository.create!(:local_shixun_id => local_shixun.id, :mirror_repository_id => mirror.id, type_name: mirror.type_name)
end if stage.stage_shixuns.present?
end stage.stage_shixuns.each do |stage_shixun|
new_stage_shixun = LocalStageShixun.new
# 同步技术标签 new_stage_shixun.attributes = stage_shixun.attributes.dup.except("id")
shixun.shixun_tag_repertoires.each do |str| new_stage_shixun.local_subject_id = local_subject.id
LocalShixunTagRepertoire.create!(:tag_repertoire_id => str.tag_repertoire_id, :local_shixun_id => local_shixun.id) new_stage_shixun.local_subject_id = new_stage.id
end new_stage_shixun.save!
# 不需要同步版本库,版本库应该是从本地导入到线上的时候由线上版本创建的
# 同步复制关卡
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
# 复制实训
_shixun_to_local(stage.shixuns)
end end
end end
end end
end
end
end
def local_to_subject
end
def shixun_to_local
identifiers = params[:identifiers].split(",")
shixuns = Shixun.where(identifier: identifiers)
# 不重复导入
ActiveRecord::Base.transaction do
begin
_shixun_to_local(shixuns)
render :json => {status: 0, message: "success"} render :json => {status: 0, message: "success"}
rescue Exception => e rescue Exception => e
logger.error("shixun_local_in ##{e.message}") logger.error("shixun_local_in ##{e.message}")
@ -519,6 +513,66 @@ class WelcomeController < ApplicationController
end end
private private
def _shixun_to_local shixuns
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,
local_giturl: shixun.git_url)
# 同步更新实践课程中对应的shixun_id
LocalStageShixun.where(shixun_id: shixun.id).update_all(local_shixun_id: local_shixun.id)
# 同步镜像
if shixun.mirror_repositories.present?
shixun.mirror_repositories.each do |mirror|
# 本地版的mirror id和线上的可能不一样所以按名字取然后再存
# local_mirror = MirrorRepository.where(type_name: mirror.type_name).first.try(:id)
LocalMirrorRepository.create!(:local_shixun_id => local_shixun.id, :mirror_repository_id => mirror.id, type_name: mirror.type_name)
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
end
# 判断网站的入口,是课程 course 则跳过index去渲染 course 方法 # 判断网站的入口,是课程 course 则跳过index去渲染 course 方法
def entry_select def entry_select
# url = request.original_url.gsub('/','') # url = request.original_url.gsub('/','')

@ -24,7 +24,7 @@
# 116challenge创建关卡的时候返回结果错误 # 116challenge创建关卡的时候返回结果错误
# #
class Shixun < ActiveRecord::Base class Shixun < ActiveRecord::Base
attr_accessible :description, :name, :changeset_num, :status, :user_id, :gpid, :language, :identifier, :authentication, :closer_id, :end_time, :publish_time, attr_accessible :name, :changeset_num, :status, :user_id, :gpid, :language, :identifier, :authentication, :closer_id, :end_time, :publish_time,
:propaedeutics, :trainee, :major_id, :homepage_show, :webssh, :hidden, :fork_from, :can_copy, :modify_time, :reset_time, :git_url, :use_scope, :propaedeutics, :trainee, :major_id, :homepage_show, :webssh, :hidden, :fork_from, :can_copy, :modify_time, :reset_time, :git_url, :use_scope,
:vnc, :evaluate_script, :image_text, :exec_time, :test_set_permission, :hide_code, :excute_time, :forbid_copy :vnc, :evaluate_script, :image_text, :exec_time, :test_set_permission, :hide_code, :excute_time, :forbid_copy
@ -54,6 +54,8 @@ class Shixun < ActiveRecord::Base
has_many :shixun_tag_repertoires, :dependent => :destroy has_many :shixun_tag_repertoires, :dependent => :destroy
has_many :shixun_service_configs, :dependent => :destroy has_many :shixun_service_configs, :dependent => :destroy
has_one :shixun_info, dependent: :destroy
scope :visible, lambda{where(status: [2,3])} scope :visible, lambda{where(status: [2,3])}
scope :min, lambda { select([:id, :name, :gpid, :modify_time, :reset_time, :language, :status, :identifier, scope :min, lambda { select([:id, :name, :gpid, :modify_time, :reset_time, :language, :status, :identifier,
:test_set_permission, :hide_code, :forbid_copy, :hidden, :webssh, :user_id, :code_hidden, :test_set_permission, :hide_code, :forbid_copy, :hidden, :webssh, :user_id, :code_hidden,
@ -66,6 +68,10 @@ class Shixun < ActiveRecord::Base
#scope :visible, -> { where(status: -1) } #scope :visible, -> { where(status: -1) }
after_create :send_tiding after_create :send_tiding
def description
shixun_info.try(:description)
end
def should_compile? def should_compile?
self.mirror_repositories.published_main_mirror.first.try(:should_compile) self.mirror_repositories.published_main_mirror.first.try(:should_compile)
end end

@ -0,0 +1,3 @@
class ShixunInfo < ActiveRecord::Base
belongs_to :shixun
end
Loading…
Cancel
Save