From 622b7a49796717326b588ad2a4ca5a9b62a3da33 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 8 Mar 2017 14:59:40 +0800 Subject: [PATCH] =?UTF-8?q?TPI=E5=BC=80=E5=90=AF=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/shixuns_controller.rb | 62 +++++++++++++++++++++++++ app/helpers/shixuns_helper.rb | 3 ++ app/models/shixun.rb | 10 ++-- app/views/shixuns/_monitor_tip.html.erb | 4 +- app/views/shixuns/_shixun_top.html.erb | 28 +++++++---- config/locales/zh.yml | 1 + config/routes.rb | 2 + 7 files changed, 97 insertions(+), 13 deletions(-) diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 2db7410a4..2cff0fea7 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -20,8 +20,35 @@ class ShixunsController < ApplicationController end end + # copy_shixun:复制一个新的实训模块包括版本库 + # publish_challenges:自动创建第一个Challenge def shixun_exec monitor_filter + if @had_exec == true || User.current.id == @shixun.user_id + render_403 + end + repository = @shixun.repository + ActiveRecord::Base.transaction do + begin + g = Gitlab.client + if User.current.gid.nil? + s = Trustie::Gitlab::Sync.new + s.sync_user(User.current) + end + gshixun = g.fork(@shixun.gpid, User.current.gid) + new_shixun = copy_shixun(@shixun, gshixun) + publish_challenges(@shixun.id, new_shixun.id, position = 1) + respond_to do |format| + format.html{redirect_to shixun_path(new_shixun)} + end + rescue Exception => e + respond_to do |format| + format.html{ + flash[:notice] = l(:notice_shixun_failed_exec)+ ":" + e.message + redirect_to shixun_path(@shixun)} + end + end + end end def new @@ -156,6 +183,41 @@ class ShixunsController < ApplicationController end private + def publish_challenges original_shixun_id, new_shixun_id, position + original_challenge = Challenge.where(:shixun_id => original_shixun_id, :position => position).first + challenge = Challenge.create(:subject => original_challenge.subject, :description => original_challenge.description, + :position => 1, :shixun_id => new_shixun_id, :user_id => User.current.id) + end + + # 复制项目 + # gshixun --> gitlab project + def copy_shixun tpm_shixun, gshixun + shixun = Shixun.new + shixun.name = tpm_shixun.name + shixun.description = tpm_shixun.description + shixun.is_public = tpm_shixun.is_public + shixun.parent_id = tpm_shixun.id + shixun.user_id = User.current.id + shixun.gpid = gshixun.id + shixun.forked_form = tpm_shixun.id + shixun.status = 2 + if shixun.save + m = ShixunMember.new(:user_id => User.current.id, :role => 1) + shixun.shixun_members << m + copy_shixun_repository(shixun, gshixun) + return shixun + end + end + + def copy_shixun_repository(shixun, gshixun) + repository = Repository.factory('Git') + repository.shixun_id = shixun.id + repository.type = 'Repository::Gitlab' + repository.url = gshixun.name + repository.identifier = gshixun.name + repository = repository.save + end + def uri_exec uri, params res = Net::HTTP.post_form(uri, params).body res = JSON.parse(res) diff --git a/app/helpers/shixuns_helper.rb b/app/helpers/shixuns_helper.rb index d706dab91..e4192e4d5 100644 --- a/app/helpers/shixuns_helper.rb +++ b/app/helpers/shixuns_helper.rb @@ -1,5 +1,8 @@ module ShixunsHelper + def tpi_allow_show shixun + (shixun.tpm? && User.current.logged? && User.current.id != shixun.user_id) + end #显示项目配置菜单 def show_project_memu user diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 98331ebca..fd535b9ff 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -1,6 +1,6 @@ -# status 控制实训的状态,0:未开启;1: 已开启 +# status 控制实训的状态,0:未开启;1: 已开启(TPM) 2: 子项目(TPI) class Shixun < ActiveRecord::Base - attr_accessible :description, :is_public, :name, :parent_id, :changeset_num, :forked_form, :status + attr_accessible :description, :is_public, :name, :parent_id, :changeset_num, :forked_form, :status, :user_id, :gpid, :forked_count has_many :users, :through => :shixun_members has_many :shixun_members @@ -13,7 +13,11 @@ class Shixun < ActiveRecord::Base end def tpi? - (!self.parent_id.nil? && !self.forked_form.nil?) ? true : false + (!self.parent_id.nil? && !self.forked_form.nil? && self.status == 2) ? true : false + end + + def tpm? + (self.parent_id.nil? && self.status == 1) ? true : false end def owner diff --git a/app/views/shixuns/_monitor_tip.html.erb b/app/views/shixuns/_monitor_tip.html.erb index 5f83f98ff..b4d836cd0 100644 --- a/app/views/shixuns/_monitor_tip.html.erb +++ b/app/views/shixuns/_monitor_tip.html.erb @@ -12,10 +12,10 @@
  • 取  消 - <% if @had_exec %> + <% if @had_exec == true %> <%= link_to "确 定", project_path(@tpm), :class => "sy_btn_blue fl ml20", :onclick => "hideModal();", :target => "_blank" %> <% else %> - <%= link_to "确 定", {:controller => 'shixuns', :action => 'shixun_exec'}, :class => "sy_btn_blue fl ml20", :onclick => "hideModal();", :target => "_blank" %> + <%= link_to "确 定", shixun_exec_shixun_path(@shixun), :class => "sy_btn_blue fl ml20", :onclick => "hideModal();", :target => "_blank" %> <% end %>
  • diff --git a/app/views/shixuns/_shixun_top.html.erb b/app/views/shixuns/_shixun_top.html.erb index 0b7b2806d..20ac401cb 100644 --- a/app/views/shixuns/_shixun_top.html.erb +++ b/app/views/shixuns/_shixun_top.html.erb @@ -11,6 +11,18 @@ <%=link_to @shixun.name, shixun_path(@shixun), :class => "pro_new_username break_word" %> + <% if tpi_allow_show(@shixun) %> +
    + +
    + <% end %> +
    <% unless @shixun.parent_id %>
    @@ -39,20 +51,20 @@
  • <%= link_to (1 > 0 ? "#{l(:project_module_repository)}#{switch_integer_into_k 99999}".html_safe : "#{l(:project_module_repository)}"), ({:controller => 'repositories', :action => 'shixun_show', :id => @shixun, :repository_id => shixun_repository(@shixun).try(:identifier)}), - :class => "pro_new_proname", :title => "#{99999}" %>
  • + :class => "pro_new_proname", :title => "#{99999}" %> <% end %>
  • <%= link_to "#{l(:button_configure)}", settings_shixun_path(@shixun), :class => "pro_new_proname" %>
  • - + - - \ No newline at end of file + }) + \ No newline at end of file diff --git a/config/locales/zh.yml b/config/locales/zh.yml index a24f77914..adb6f22a4 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -33,6 +33,7 @@ zh: notice_account_wrong_password: 密码错误 name_can_be_empty: 可以不填写真实姓名[保密所需] notice_successful_create: 创建成功 + notice_shixun_failed_exec: 实训开启失败,错误信息如下 notice_create_failed: 创建失败,请先完善个人信息 notice_failed_create: 创建失败 notice_successful_update: 更新成功 diff --git a/config/routes.rb b/config/routes.rb index db2bec5ea..80c67b869 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -30,6 +30,8 @@ RedmineApp::Application.routes.draw do resources :shixuns do member do + get 'shixun_monitor' + get 'shixun_exec' post 'add_script' get 'settings(/:tab)', :action => 'settings', :as => 'settings' get 'shixun_job_create'