diff --git a/app/assets/javascripts/games.js.coffee b/app/assets/javascripts/games.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/games.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/games.css.scss b/app/assets/stylesheets/games.css.scss new file mode 100644 index 000000000..db1b7bcfa --- /dev/null +++ b/app/assets/stylesheets/games.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the games controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb new file mode 100644 index 000000000..c86cff950 --- /dev/null +++ b/app/controllers/games_controller.rb @@ -0,0 +1,54 @@ +class GamesController < ApplicationController + layout "base_myshixun" + before_filter :find_myshixun, :only => [:index, :game_build] + before_filter :find_game, :only => [:show, :game_build] + before_filter :allowd_manager, :only => [:game_build] + include ApplicationHelper + + def index + + end + + def show + + end + + # REDO:回复状态更新 + def game_build + gitUrl = git_repository_url(@myshixun, "Myshixun") + gitUrl = Base64.encode64(gitUrl) + taskId = params[:id] + jobName = @myshixun.forked_from + step = @game.stage + + if @game.status == 0 + params = {:jobName => "#{jobName}", :taskId => "#{taskId}", :step => "#{step}", :gitUrl => "#{gitUrl}"} + uri = URI.parse("http://123.59.135.74:9999/jenkins-exec/api/buildJob") + res = uri_exec uri, params + # @challenge.update_attribute(:status, 1) + end + respond_to do |format| + format.js + end + end + + private + def allowd_manager + render_403 unless User.current.manager_of_myshixun?(@myshixun) + end + + # Find myshixun of id params[:id] + def find_myshixun + myshixun_id = params[:myshixun_id] || (params[:game] && params[:game][:myshixun_id]) + @myshixun = Myshixun.find(myshixun_id) + rescue ActiveRecord::RecordNotFound + render_404 + end + + def find_game + @game = Game.find(params[:id]) + @myshixun = @game.myshixun + rescue ActiveRecord::RecordNotFound + render_404 + end +end diff --git a/app/controllers/myshixuns_controller.rb b/app/controllers/myshixuns_controller.rb index aed043f88..f0737dafb 100644 --- a/app/controllers/myshixuns_controller.rb +++ b/app/controllers/myshixuns_controller.rb @@ -1,2 +1,28 @@ class MyshixunsController < ApplicationController + layout 'base_myshixun' + skip_before_filter :verify_authenticity_token, :only => [:training_task_status] + before_filter :require_login, :except => [:training_task_status] + before_filter :find_myshixun, :only => [:show] + + def training_task_status + status = params[:status].to_i + task_id = params[:taskId] + outPut = params[:outPut] + message = Base64.decode64(params[:msg]) unless params[:msg].blank? + GameOutputs.create(:code => status, :msg => message, :game_id => task_id, :out_put => outPut) + end + + def show + respond_to do |format| + format.html{redirect_to myshixun_games_path(@myshixun)} + end + end + + private + # Find myshixun of id params[:id] + def find_myshixun + @myshixun = Myshixun.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 6db99de04..e891383ed 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -2,24 +2,13 @@ # REDO: 创建版本库权限控制 class ShixunsController < ApplicationController layout 'base_shixun' - skip_before_filter :verify_authenticity_token, :only => [:training_task_status] - - before_filter :require_login, :except => [:training_task_status] - before_filter :find_shixun, :except => [ :index, :new, :create, :training_task_status] + before_filter :require_login + before_filter :find_shixun, :except => [ :index, :new, :create] before_filter :shixun_view_allow, :only => [:show] before_filter :require_manager, :only => [ :settings, :add_script] include ApplicationHelper - def training_task_status - status = params[:status].to_i - task_id = params[:taskId] - outPut = params[:outPut] - message = Base64.decode64(params[:msg]) unless params[:msg].blank? - GameOutputs.create(:code => status, :msg => message, :game_id => task_id, :out_put => outPut) - end - - def shixun_monitor monitor_filter if @had_exec @@ -47,11 +36,11 @@ class ShixunsController < ApplicationController 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) + myshixun = copy_myshixun(@shixun, gshixun) + publish_games(@shixun.id, myshixun.id, position = 1) respond_to do |format| - format.html{redirect_to shixun_challenges_path(new_shixun)} + format.html{redirect_to myshixun_games_path(myshixun)} end rescue Exception => e respond_to do |format| @@ -120,20 +109,6 @@ class ShixunsController < ApplicationController end end - def rep_tree_changes - rev = params[:rev] - ent_path = params[:ent_path] - gpid = params[:gpid] - g = Gitlab.client - begin - result = g.rep_last_changes(gpid, :rev => rev, :path => ent_path) - result = {:message => result.message, :author_name => User.find_by_mail(result.author_email).nil? ? result.author_email : User.find_by_mail(result.author_email).show_name, :time => distance_of_time_in_words(result.time, Time.now)} - rescue Exception => e - puts e - end - render :json => result - end - # 添加实训脚本 def add_script if @shixun.update_attribute(:script, params[:shixun_script]) @@ -198,39 +173,40 @@ class ShixunsController < ApplicationController end private - def publish_challenges original_shixun_id, new_shixun_id, position + def publish_games 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) + challenge = Game.create(:subject => original_challenge.subject, :description => original_challenge.description, + :stage => position, :myshixun_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 + def copy_myshixun tpm_shixun, gshixun + myshixun = Myshixun.new + myshixun.name = tpm_shixun.name + myshixun.description = tpm_shixun.description + myshixun.is_public = tpm_shixun.is_public + myshixun.parent_id = tpm_shixun.id + myshixun.user_id = User.current.id + myshixun.gpid = gshixun.id + myshixun.forked_from = tpm_shixun.id + + if myshixun.save + m = MyshixunMember.new(:user_id => User.current.id, :role => 1) + myshixun.myshixun_members << m + copy_myshixun_repository(myshixun, gshixun) + return myshixun end end - def copy_shixun_repository(shixun, gshixun) + def copy_myshixun_repository(myshixun, gshixun) repository = Repository.factory('Git') - repository.shixun_id = shixun.id + repository.myshixun_id = myshixun.id repository.type = 'Repository::Gitlab' repository.url = gshixun.name repository.identifier = gshixun.name repository.project_id = -1 + repository.shixun_id = -2 repository = repository.save end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index df6e66c4c..a3882f1ee 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -40,6 +40,8 @@ module ApplicationHelper def git_repository_url project, type if type == "Shixun" rep_identify = Repository.where(:shixun_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) + eleif type == "Myshixun" + rep_identify = Repository.where(:my_shixun_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) else rep_identify = Repository.where(:project_id => project.id, :type => "Repository::Gitlab").first.try(:identifier) end diff --git a/app/helpers/games_helper.rb b/app/helpers/games_helper.rb new file mode 100644 index 000000000..2ef8c1f41 --- /dev/null +++ b/app/helpers/games_helper.rb @@ -0,0 +1,2 @@ +module GamesHelper +end diff --git a/app/models/game.rb b/app/models/game.rb new file mode 100644 index 000000000..835aee448 --- /dev/null +++ b/app/models/game.rb @@ -0,0 +1,6 @@ +class Game < ActiveRecord::Base + attr_accessible :description, :myshixun_id, :stage, :subject, :user_id, :status + belongs_to :myshixun,:touch=> true + belongs_to :user + has_many :game_comments +end diff --git a/app/models/myshixun.rb b/app/models/myshixun.rb index 7544c9b89..e20bd5336 100644 --- a/app/models/myshixun.rb +++ b/app/models/myshixun.rb @@ -1,3 +1,13 @@ class Myshixun < ActiveRecord::Base - attr_accessible :name,:description, :script, :is_public, :parent_id, :user_id, :forked_count + attr_accessible :description, :name, :parent_id, :user_id, :gpid, :forked_from, :visits, :is_public + has_many :users, :through => :myshixun_members + has_many :myshixun_members + has_one :repository + has_many :games, :dependent => :destroy, :order => "games.id ASC" + + def owner + User.find(self.user_id) + rescue ActiveRecord::RecordNotFound + render_404 + end end diff --git a/app/models/myshixun_member.rb b/app/models/myshixun_member.rb new file mode 100644 index 000000000..9d9320d87 --- /dev/null +++ b/app/models/myshixun_member.rb @@ -0,0 +1,5 @@ +class MyshixunMember < ActiveRecord::Base + attr_accessible :myshixun_id, :role, :user_id + belongs_to :myshixun + belongs_to :user +end diff --git a/app/models/repository.rb b/app/models/repository.rb index a4b9f4e01..06c0510ac 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -26,7 +26,7 @@ class Repository < ActiveRecord::Base belongs_to :project belongs_to :shixun - belongs_to :shixun + belongs_to :myshixun has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" has_many :filechanges, :class_name => 'Change', :through => :changesets diff --git a/app/models/shixun.rb b/app/models/shixun.rb index 5719de1dd..07fc2e0b6 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -22,5 +22,7 @@ class Shixun < ActiveRecord::Base def owner User.find(self.user_id) + rescue ActiveRecord::RecordNotFound + render_404 end end diff --git a/app/models/shixun_member.rb b/app/models/shixun_member.rb index 3dd4b9b80..c39c2521c 100644 --- a/app/models/shixun_member.rb +++ b/app/models/shixun_member.rb @@ -1,7 +1,6 @@ class ShixunMember < ActiveRecord::Base - attr_accessible :shixun_member_id, :user_id, :role + attr_accessible :shixun_id, :user_id, :role belongs_to :shixun belongs_to :user has_many :roles ,:through => :shixun_member_roles, :foreign_key => 'role_id' - has_many :shixun_member_roles,:dependent => :destroy end diff --git a/app/models/user.rb b/app/models/user.rb index e5cff7f99..98d6d7b00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -899,6 +899,12 @@ class User < Principal (!member.blank? || User.current.admin?) ? true : false end + # 超级管理员或者role为1 + def manager_of_myshixun?(myshixun) + member = MyshixunMember.where(:user_id => self.id, :myshixun_id => myshixun.id, :role => 1) + (!member.blank? || User.current.admin?) ? true : false + end + def member_of_course?(course) courses.to_a.include?(course) end diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb new file mode 100644 index 000000000..7893cadbe --- /dev/null +++ b/app/views/games/index.html.erb @@ -0,0 +1 @@ +all tpis \ No newline at end of file diff --git a/app/views/games/show.html.erb b/app/views/games/show.html.erb new file mode 100644 index 000000000..f13a574f2 --- /dev/null +++ b/app/views/games/show.html.erb @@ -0,0 +1,2 @@ +<%= link_to "提交评测", {:controller => 'games', :action => "game_build", :id => @game, :myshixun_id => @myshixun}, :class => "task-display-span bBlue mt10", :onclick => "training_task_submmit();", :remote => true %> +tpi \ No newline at end of file diff --git a/app/views/layouts/base_myshixun.html.erb b/app/views/layouts/base_myshixun.html.erb new file mode 100644 index 000000000..b4f2129a2 --- /dev/null +++ b/app/views/layouts/base_myshixun.html.erb @@ -0,0 +1,53 @@ + + + + + <%= h html_title %> + + + <%= csrf_meta_tag %> + <%= favicon %> + <%= javascript_heads %> + <%= heads_for_theme %> + <%= stylesheet_link_tag 'jquery/jquery-ui-1.9.2','css/common','css/structure','scm','css/public', 'css/project','css/popup','repository','css/gantt', 'css/calendar', 'css/moduel', 'css/font-awesome' %> + <%= call_hook :view_layouts_base_html_head %> + + <%= yield :header_tags -%> + +<%= @shixun %> + + + +
+ +
+
+ TPI实训 <%= @myshixun.id %> +
+ + <%= render_flash_messages %> + <%= yield %> + <%= call_hook :view_layouts_base_content %> +
+
+ +
+<%= render :partial => 'layouts/footer' %> +
+<% if hidden_unproject_infos %> + <%= render :partial => 'layouts/new_feedback' %> +<% end %> + + + +<%= call_hook :view_layouts_base_body_bottom %> + + + +<%= javascript_include_tag 'cookie','project',"avatars", 'header','prettify','select_list_move','attachments' %> + + diff --git a/config/routes.rb b/config/routes.rb index 805e0e01a..86b5f8b75 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,6 +56,19 @@ RedmineApp::Application.routes.draw do end end + resources :myshixuns do + member do + + end + collection do + + end + resources :games do + member do + match 'game_build', :via => [:get, :post] + end + end + end # Enable Grack support # mount Trustie::Grack.new, at: '/', constraints: lambda { |request| /[-\/\w\.]+\.git\//.match(request.path_info) }, via: [:get, :post] diff --git a/db/migrate/20170310011834_create_myshixuns.rb b/db/migrate/20170310011834_create_myshixuns.rb new file mode 100644 index 000000000..3c23fa73c --- /dev/null +++ b/db/migrate/20170310011834_create_myshixuns.rb @@ -0,0 +1,16 @@ +class CreateMyshixuns < ActiveRecord::Migration + def change + create_table :myshixuns do |t| + t.string :name + t.text :description + t.integer :parent_id + t.boolean :is_public, :default => 1 + t.integer :user_id + t.integer :gpid + t.integer :forked_from, :default => 0 + t.integer :visits, :default => 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20170310014056_create_myshixun_members.rb b/db/migrate/20170310014056_create_myshixun_members.rb new file mode 100644 index 000000000..a591bf36e --- /dev/null +++ b/db/migrate/20170310014056_create_myshixun_members.rb @@ -0,0 +1,11 @@ +class CreateMyshixunMembers < ActiveRecord::Migration + def change + create_table :myshixun_members do |t| + t.integer :myshixun_id + t.integer :user_id + t.integer :role + + t.timestamps + end + end +end diff --git a/db/migrate/20170310032024_add_myshixun_to_repositories.rb b/db/migrate/20170310032024_add_myshixun_to_repositories.rb new file mode 100644 index 000000000..fe420dc5e --- /dev/null +++ b/db/migrate/20170310032024_add_myshixun_to_repositories.rb @@ -0,0 +1,5 @@ +class AddMyshixunToRepositories < ActiveRecord::Migration + def change + add_column :repositories, :myshixun_id, :integer + end +end diff --git a/db/migrate/20170310053903_create_games.rb b/db/migrate/20170310053903_create_games.rb new file mode 100644 index 000000000..b5a1e7a0b --- /dev/null +++ b/db/migrate/20170310053903_create_games.rb @@ -0,0 +1,13 @@ +class CreateGames < ActiveRecord::Migration + def change + create_table :games do |t| + t.integer :myshixun_id + t.integer :user_id + t.string :subject + t.text :description + t.integer :stage + + t.timestamps + end + end +end diff --git a/db/migrate/20170310072101_add_status_to_game.rb b/db/migrate/20170310072101_add_status_to_game.rb new file mode 100644 index 000000000..0507bb20f --- /dev/null +++ b/db/migrate/20170310072101_add_status_to_game.rb @@ -0,0 +1,5 @@ +class AddStatusToGame < ActiveRecord::Migration + def change + add_column :games, :status, :integer, :default => 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index b12151e8d..969539d11 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20170309062850) do +ActiveRecord::Schema.define(:version => 20170310072101) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -1056,6 +1056,17 @@ ActiveRecord::Schema.define(:version => 20170309062850) do t.datetime "updated_at", :null => false end + create_table "games", :force => true do |t| + t.integer "myshixun_id" + t.integer "user_id" + t.string "subject" + t.text "description" + t.integer "stage" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.integer "status", :default => 0 + end + create_table "groups_users", :id => false, :force => true do |t| t.integer "group_id", :null => false t.integer "user_id", :null => false @@ -1474,6 +1485,27 @@ ActiveRecord::Schema.define(:version => 20170309062850) do add_index "messages", ["parent_id"], :name => "messages_parent_id" add_index "messages", ["root_id"], :name => "index_messages_on_root_id" + create_table "myshixun_members", :force => true do |t| + t.integer "myshixun_id" + t.integer "user_id" + t.integer "role" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + + create_table "myshixuns", :force => true do |t| + t.string "name" + t.text "description" + t.integer "parent_id" + t.boolean "is_public", :default => true + t.integer "user_id" + t.integer "gpid" + t.integer "forked_from", :default => 0 + t.integer "visits", :default => 0 + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "news", :force => true do |t| t.integer "project_id" t.string "title", :limit => 60, :default => "", :null => false @@ -1941,6 +1973,7 @@ ActiveRecord::Schema.define(:version => 20170309062850) do t.boolean "is_default", :default => false t.boolean "hidden", :default => false t.integer "shixun_id" + t.integer "myshixun_id" end add_index "repositories", ["project_id"], :name => "index_repositories_on_project_id" diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb new file mode 100644 index 000000000..87a6bbed0 --- /dev/null +++ b/spec/controllers/games_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe GamesController, :type => :controller do + +end diff --git a/spec/factories/games.rb b/spec/factories/games.rb new file mode 100644 index 000000000..81731c73a --- /dev/null +++ b/spec/factories/games.rb @@ -0,0 +1,10 @@ +FactoryGirl.define do + factory :game do + myshixun_id 1 +user_id 1 +subject "MyString" +description "MyText" +stage "" + end + +end diff --git a/spec/factories/myshixun_members.rb b/spec/factories/myshixun_members.rb new file mode 100644 index 000000000..674469904 --- /dev/null +++ b/spec/factories/myshixun_members.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :myshixun_member do + myshixun_id 1 +user_id 1 +role 1 + end + +end diff --git a/spec/factories/myshixuns.rb b/spec/factories/myshixuns.rb index 3a6dd4bad..154b9e88a 100644 --- a/spec/factories/myshixuns.rb +++ b/spec/factories/myshixuns.rb @@ -1,6 +1,8 @@ FactoryGirl.define do factory :myshixun do name "MyString" +description "MyText" +parent_id 1 end end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb new file mode 100644 index 000000000..7da4110e1 --- /dev/null +++ b/spec/models/game_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Game, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/myshixun_member_spec.rb b/spec/models/myshixun_member_spec.rb new file mode 100644 index 000000000..8c5b1c01f --- /dev/null +++ b/spec/models/myshixun_member_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe MyshixunMember, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end