diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 92e061f2..e5a168f6 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -70,43 +70,89 @@ class WelcomeController < ApplicationController subject_ids = params[:subject_ids].split(",") subjects = Subject.where(id: subject_ids) ActiveRecord::Base.transaction do - subjects.each do |subject| - if LocalSubject.where(subject_id: subject.id).blank? - - local_subject = LocalSubject.create!(name: subject.name, description: subject.description, status: 0, - learning_notes: subject.learning_notes, introduction: subject.introduction, - stages_count: subject.stages_count, stage_shixuns_count: subject.stage_shixuns_count, - subject_id: subject.id, shixuns_count: subject.shixuns_count) - - # 同步复制章节 - if subject.stages.present? - subject.stages.each do |stage| - new_stage = LocalStage.new - new_stage.attributes = stage.attributes.dup.except("id","user_id") - new_stage.local_subject_id = local_subject.id - new_stage.save! - - if stage.stage_shixuns.present? - stage.stage_shixuns.each do |stage_shixun| - new_stage_shixun = LocalStageShixun.new - new_stage_shixun.attributes = stage_shixun.attributes.dup.except("id") - new_stage_shixun.local_subject_id = local_subject.id - new_stage_shixun.local_subject_id = new_stage.id - new_stage_shixun.save! + begin + subjects.each do |subject| + if LocalSubject.where(subject_id: subject.id).blank? + + local_subject = LocalSubject.create!(name: subject.name, description: subject.description, status: 0, + learning_notes: subject.learning_notes, introduction: subject.introduction, + stages_count: subject.stages_count, stage_shixuns_count: subject.stage_shixuns_count, + subject_id: subject.id, shixuns_count: subject.shixuns_count) + + # 同步复制章节 + if subject.stages.present? + subject.stages.each do |stage| + new_stage = LocalStage.new + new_stage.attributes = stage.attributes.dup.except("id","user_id") + new_stage.local_subject_id = local_subject.id + new_stage.save! + + if stage.stage_shixuns.present? + stage.stage_shixuns.each do |stage_shixun| + new_stage_shixun = LocalStageShixun.new + new_stage_shixun.attributes = stage_shixun.attributes.dup.except("id") + new_stage_shixun.local_subject_id = local_subject.id + new_stage_shixun.local_subject_id = new_stage.id + new_stage_shixun.save! + end end - end - # 复制实训 - _shixun_to_local(stage.shixuns) + # 复制实训 + _shixun_to_local(stage.shixuns) + end end end end + render :json => {status: 0, message: "success"} + + rescue Exception => e + logger.error("shixun_local_in ##{e.message}") + render :json => {status: -1, message: "error,#{e.message}"} + raise ActiveRecord::Rollback end end end def local_to_subject - + ActiveRecord::Base.transaction do + subject_list = [] + LocalSubject.find_each do |local_subject| + subject = Subject.create!(name: local_subject.name, description: local_subject.description, user_id: User.current.id, + status: 0, learning_notes: local_subject.learning_notes, introduction: local_subject.introduction, + stages_count: local_subject.stages_count, stage_shixuns_count: local_subject.stage_shixuns_count, + shixuns_count: local_subject.shixuns_count, visits: 1) + m = SubjectMember.new(:user_id => User.current.id, :role => 1) + subject.subject_members << m + + # 同步章节信息 + local_stages = LocalStage.where(local_subject_id: local_subject.id) + if local_stages.present? + local_stages.each do |local_stage| + new_stage = Stage.new + new_stage.attributes = local_stage.attributes.dup.except("id","local_subject_id","subject_id") + new_stage.subject_id = subject.id + new_stage.save! + + # 同步章节实训关联表 + local_stage_shixuns = LocalStageShixun.where(local_stage_id: local_stage.id) + local_stage_shixuns.each do |local_stage_shixun| + # 先同步实训 + local_shixun = LocalShixun.where(id: local_stage_shixun.local_shixun_id).first + shixun = _local_to_shixun local_shixun if local_shixun.present? + + new_stage_shixun = StageShixun.new + new_stage_shixun.position = local_stage_shixun.position + new_stage_shixun.stage_id = new_stage.id + new_stage_shixun.subject_id = subject.id + new_stage_shixun.shixun_id = shixun.try(:id) + new_stage_shixun.save! + end + end + end + subject_list << subject.id + end + render :json => {status: 0, message: "success", identifier: subject_list} + end end def shixun_to_local @@ -130,84 +176,7 @@ class WelcomeController < ApplicationController 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| - local_mirror_new = MirrorRepository.where(type_name: local_mirror.try(:type_name)).first - ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => local_mirror_new.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_git_path = local_shixun.local_giturl.split('/').last if local_shixun.local_giturl.present? - new_giturl = "http://educoder:xinhu1ji2qu3@"+g.project(shixun.gpid).http_url_to_repo.split('//').last - # 如果有目录才执行 - if system("cd tmp/repositories/#{local_git_path}") - system("cd tmp/repositories/#{local_git_path};git remote remove origin;git remote add origin #{new_giturl}; - git add .;git commit -m '..';git push origin master") - end - - - # http://Hjqreturn:xinhu1ji2qu3@bdgit.educoder.net/Hjqreturn/pgfqe6ch8.git (fetch) - - # 同步关卡信息 - 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 = _local_to_shixun local_shixun shixun_list << shixun.identifier end render :json => {status: 0, message: "success", identifier: shixun_list} @@ -547,7 +516,7 @@ class WelcomeController < ApplicationController 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.attributes = challenge.attributes.dup.except("id","shixun_id","user_id", "test_set_score", "test_set_average", "exec_time", "praises_count") new_challenge.local_shixun_id = local_shixun.id new_challenge.save! # 评测题,选择题暂时不考虑 @@ -555,7 +524,7 @@ class WelcomeController < ApplicationController 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.attributes = test_set.attributes.dup.except("id","challenge_id","match_rule") new_test_set.local_challenge_id = new_challenge.id new_test_set.save! end @@ -573,6 +542,88 @@ class WelcomeController < ApplicationController end end + def _local_to_shixun 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| + local_mirror_new = MirrorRepository.where(type_name: local_mirror.try(:type_name)).first + ShixunMirrorRepository.create!(:shixun_id => shixun.id, :mirror_repository_id => local_mirror_new.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_git_path = local_shixun.local_giturl.split('/').last if local_shixun.local_giturl.present? + new_giturl = "http://educoder:xinhu1ji2qu3@"+g.project(shixun.gpid).http_url_to_repo.split('//').last + # 如果有目录才执行 + if system("cd tmp/repositories/#{local_git_path}") + system("cd tmp/repositories/#{local_git_path};git remote remove origin;git remote add origin #{new_giturl}; + git add .;git commit -m '..';git push origin master") + end + + + # http://Hjqreturn:xinhu1ji2qu3@bdgit.educoder.net/Hjqreturn/pgfqe6ch8.git (fetch) + + # 同步关卡信息 + 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 + end + # 判断网站的入口,是课程 course 则跳过index去渲染 course 方法 def entry_select # url = request.original_url.gsub('/','') diff --git a/app/models/shixun.rb b/app/models/shixun.rb index c8d4bb6a..cef56af3 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -72,6 +72,10 @@ class Shixun < ActiveRecord::Base shixun_info.try(:description) end + def evaluate_script + shixun_info.try(:evaluate_script) + end + def should_compile? self.mirror_repositories.published_main_mirror.first.try(:should_compile) end diff --git a/config/routes.rb b/config/routes.rb index ad5a7bdc..ef6d561c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -301,6 +301,8 @@ RedmineApp::Application.routes.draw do ## oauth相关 get 'welcome/local_to_shixun' => 'welcome#local_to_shixun' get 'welcome/local_init' => 'welcome#local_init' get 'welcome/user_agents' => 'welcome#user_agents' + get 'welcome/subject_to_local' => 'welcome#subject_to_local' + get 'welcome/local_to_subject' => 'welcome#local_to_subject' # get 'competitions/home' => 'competitions#home' # get 'competitions/hn' => 'competitions#index'