diff --git a/Gemfile b/Gemfile index 91259aad8..e5f2a7054 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,7 @@ group :test do gem "mocha", "~> 0.13.3" gem 'capybara', '~> 2.0.0' gem 'nokogiri', '< 1.6.0' + gem 'factory_girl' platforms :mri, :mingw do group :rmagick do @@ -43,7 +44,7 @@ group :development, :test do gem 'spork-testunit' gem 'guard-spork' gem 'guard-test', '~> 1.0.0' - gem 'ruby-prof' + gem 'ruby-prof' unless RUBY_PLATFORM =~ /w32/ gem 'pry' gem 'pry-nav' end diff --git a/Gemfile.lock b/Gemfile.lock index 68c4f3587..4159a32a0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -78,6 +78,8 @@ GEM coffee-script-source (1.7.1) erubis (2.7.0) execjs (2.2.1) + factory_girl (4.4.0) + activesupport (>= 3.0.0) fastercsv (1.5.5) ffi (1.9.3) ffi (1.9.3-x86-mingw32) @@ -189,7 +191,6 @@ GEM sass-rails rmagick (2.13.2) ruby-openid (2.1.8) - ruby-prof (0.15.1) rubyzip (1.1.6) sass (3.3.10) sass-rails (3.2.6) @@ -248,6 +249,7 @@ DEPENDENCIES capybara (~> 2.0.0) coderay (~> 1.0.6) coffee-rails (~> 3.2.1) + factory_girl fastercsv (~> 1.5.0) guard-rails guard-spork @@ -269,7 +271,6 @@ DEPENDENCIES rich (= 1.4.6) rmagick (>= 2.0.0) ruby-openid (~> 2.1.4) - ruby-prof sass-rails (~> 3.2.3) seems_rateable! shoulda (> 3.3.2) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index fea29759d..171d34377 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -244,6 +244,7 @@ class AdminController < ApplicationController @first_page.web_title = params[:web_title] @first_page.description = params[:description] @first_page.title = params[:title] + @first_page.sort_type = params[:sort_type] if @first_page.save respond_to do |format| flash[:notice] = l(:notice_successful_update) diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index b283b4792..4964bf6a6 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -55,32 +55,35 @@ class AttachmentsController < ApplicationController candown = false if @attachment.container.has_attribute?(:project) && @attachment.container.project project = @attachment.container.project - candown= User.current.member_of?(project) + candown= User.current.member_of?(project) || (project.is_public==1 && @attachment.is_public == 1) elsif @attachment.container.is_a?(Project) project = @attachment.container - candown= User.current.member_of?(project) + candown= User.current.member_of?(project) || (project.is_public==1 && @attachment.is_public == 1) elsif @attachment.container.has_attribute?(:course) && @attachment.container.course course = @attachment.container.course - candown= User.current.member_of_course?(course) + candown= User.current.member_of_course?(course) || (course.is_public==1 && @attachment.is_public == 1) elsif @attachment.container.is_a?(Course) course = @attachment.container - candown= User.current.member_of_course?(course) + candown= User.current.member_of_course?(course) || (course.is_public==1 && @attachment.is_public == 1) elsif @attachment.container.class.to_s=="HomeworkAttach" && @attachment.container.bid.reward_type == 3 candown = true + else + candown = @attachment.is_public == 1 end if candown || User.current.admin? @attachment.increment_download + + if stale?(:etag => @attachment.digest) + # images are sent inline + send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), + :type => detect_content_type(@attachment), + :disposition => (@attachment.image? ? 'inline' : 'attachment') + end + else render_403 :message => :notice_not_authorized end - - if stale?(:etag => @attachment.digest) - # images are sent inline - send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), - :type => detect_content_type(@attachment), - :disposition => (@attachment.image? ? 'inline' : 'attachment') - end rescue => e redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html" end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index a38d9f9c8..e10d6df88 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -217,7 +217,7 @@ class CoursesController < ApplicationController @issue_category ||= IssueCategory.new @member ||= @course.members.new @trackers = Tracker.sorted.all - end + end def create if User.current.user_extensions.identity @@ -239,18 +239,18 @@ class CoursesController < ApplicationController @trackers = Tracker.sorted.all if User.current.user_extensions.identity == 0 - if @course.save + if @course.save #unless User.current.admin? - r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first - m = Member.new(:user => User.current, :roles => [r]) - m.project_id = -1 - course = CourseInfos.new(:user_id => User.current.id, :course_id => @course.id) - #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) - if params[:course][:is_public] == '1' - course_status = CourseStatus.create(:course_id => @course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => @course_tag) - end - @course.members << m - @course.course_infos << course + r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first + m = Member.new(:user => User.current, :roles => [r]) + m.project_id = -1 + course = CourseInfos.new(:user_id => User.current.id, :course_id => @course.id) + #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) + if params[:course][:is_public] == '1' + course_status = CourseStatus.create(:course_id => @course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => @course_tag) + end + @course.members << m + @course.course_infos << course #end respond_to do |format| format.html { @@ -272,7 +272,7 @@ class CoursesController < ApplicationController format.api { render_validation_errors(@course) } end end - end + end end @@ -715,4 +715,4 @@ class CoursesController < ApplicationController -end \ No newline at end of file +end diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 3b8b4b928..9c41f91d6 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -50,14 +50,14 @@ class ForumsController < ApplicationController def index @offset, @limit = api_offset_and_limit({:limit => 10}) - @forums_all = Forum.all + @forums_all = Forum.where('1=1') @forums_count = @forums_all.count @forums_pages = Paginator.new @forums_count, @limit, params['page'] @offset ||= @forums_pages.offset - # @forums = @forums_all.offset(@offset).limit(@limit).all - @forums = Forum.all + @forums = @forums_all.offset(@offset).limit(@limit).all + #@forums = Forum.all respond_to do |format| format.html # index.html.erb format.json { render json: @forums } diff --git a/app/controllers/stores_controller.rb b/app/controllers/stores_controller.rb index f34e72402..ae9336161 100644 --- a/app/controllers/stores_controller.rb +++ b/app/controllers/stores_controller.rb @@ -38,7 +38,7 @@ class StoresController < ApplicationController LIMIT = 12 unless const_defined?(:LIMIT) def index @projects_attach = project_classification(0).take(LIMIT) - @courses_attach = Attachment.includes(:course).where("courses.is_public = ?", 1). + @courses_attach = Attachment.includes(:course).where("courses.is_public = 1"). where(container_type: 'Course'). limit(LIMIT) # @projects_attach = Attachment.includes(:project).where("projects.project_type=? AND projects.is_public = ?", 0, 1). diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 36e57de5a..e81355260 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -53,6 +53,7 @@ class UsersController < ApplicationController include CustomFieldsHelper include AvatarHelper include WordsHelper + include GitlabHelper # added by liuping 关注 diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 03bca6162..6a1772c8b 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -17,12 +17,44 @@ class WelcomeController < ApplicationController include ApplicationHelper + include WelcomeHelper caches_action :robots # before_filter :fake, :only => [:index, :course] before_filter :entry_select, :only => [:index] def index @first_page = FirstPage.where("page_type = 'project'").first + @hot_projects = find_miracle_project(10, 3) + @hot_projects_ids = [] + @hot_projects.each do |p| + @hot_projects_ids << p + end + @projects_all = Project.active.visible. + joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id"). + where("#{Project.table_name}.project_type = ? and #{Project.table_name}.id in (?)", Project::ProjectType_project,@hot_projects_ids) + if @first_page.nil? || @first_page.sort_type.nil? + @projects = @projects_all.order("grade desc") + else + case @first_page.sort_type + when 0 + @projects = @projects_all.order("created_on desc") + when 1 + @projects = @projects_all.order("grade desc") + when 2 + @projects = @projects_all.order("watchers_count desc") + + #gcm + #when '3' + #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array) + # @projects=handle_project @projects_all,@project_activity_count + # @s_type = 3 + # @projects = @projects[@project_pages.offset, @project_pages.per_page] + + else + @projects = @projects_all.order("grade desc") + end + end + end def robots diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb new file mode 100644 index 000000000..ceaaf6d21 --- /dev/null +++ b/app/helpers/gitlab_helper.rb @@ -0,0 +1,247 @@ +# Gitlab5.3 API操作接口 +# Add by nwb +# 暂时没支持SSH +# 接口通过HTTP协议与服务器交互,理论上只要能安装gitlab的操作系统本接口都能支持 +# 本机的测试环境是Ubuntu 12.04 + +module GitlabHelper + + # gitlab版本库数据本地保存的根目录 + ROOT_PATH="/home/git/repositories/" + PROJECT_PATH_CUT = 40 + # gitlab版本库所在服务器 + # 注意REPO_IP_ADDRESS必须以http://开头,暂时只支持HTTP协议,未支持SSH + #REPO_IP_ADDRESS = "http://" + Setting.repository_domain + REPO_IP_ADDRESS = "http://192.168.137.100" + GITLAB_API = "/api/v3" + + # 用户在项目中的权限级别 + GUEST = 10 + REPORTER = 20 + DEVELOPER = 30 + MASTER = 40 + + # gitlab的登录验证信息 + # add by nwb + def self.gitlab_token=(token) + Thread.current[:gitlab_token] = token + end + + # gitlab的登录验证信息 + # add by nwb + def self.gitlab_token + Thread.current[:gitlab_token] ||= nil + end + + # 登录gitlab + # add by nwb + def login_gitlab(email,password) + url = REPO_IP_ADDRESS + GITLAB_API + "/session" + uri = URI.parse(url) + data = {email:email, password:password} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + GitlabHelper.gitlab_token= temp['private_token'] + return true + else + return false + end + rescue =>err + return false + end + end + + # 创建项目 + # add by nwb + def create_project(project_name) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects" + uri = URI.parse(url) + data = {name:project_name, private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + #新创建项目的版本库地址 + respo = temp['http_url_to_repo'] + respo['http://localhost'] = REPO_IP_ADDRESS + # 新创建项目的web地址 + webaddress = temp['web_url'] + webaddress['http://localhost'] = REPO_IP_ADDRESS + return true + else + return false + end + rescue =>err + return false + end + end + + # 为指定用户创建版本库 + # project_name:版本库名称 user_id:Gitlab版本库中的用户编号 + # add by nwb + def create_project_for_user(project_name,user_id) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects/user/" + user_id + uri = URI.parse(url) + data = {user_id:user_id, name:project_name,private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + #新创建项目的版本库地址 + respo = temp['http_url_to_repo'] + respo['http://localhost'] = REPO_IP_ADDRESS + # 新创建项目的web地址 + webaddress = temp['web_url'] + webaddress['http://localhost'] = REPO_IP_ADDRESS + return true + else + return false + end + rescue =>err + return false + end + end + + # 创建用户 + # loginname:登录名称(版本库路径包含) name:用户姓名 + # add by nwb + def create_user (loginname,name,password,email) + url = REPO_IP_ADDRESS + GITLAB_API + "/users" + uri = URI.parse(url) + data = {email:email,password:password,username:loginname, name:name, private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + #新创建的gitlab用户编号(需保存) + user_id = temp['id'] + return true + else + return false + end + rescue =>err + return false + end + end + + # 删除用户 + # user_id:用户在gitlab中的id + # add by nwb + def delete_user(user_id) + url = REPO_IP_ADDRESS + GITLAB_API + "/users/" + user_id + uri = URI.parse(url) + data = {id:user_id,private_token:GitlabHelper.gitlab_token} + http = Net::HTTP.new uri.host, uri.port + begin + req = Net::HTTP::Delete.new(uri.request_uri) + req.form_data = data + res= http.start { |http| http.request req } + if res.code == '200' + temp = ActiveSupport::JSON.decode(res.body) + # 删除成功,对应更新trustie用户的gitlab用户编号 + return true + else + return false + end + rescue =>err + return false + end + end + + # 给用户添加一个可操作的项目 + # 若用户已在该项目中,权限级别取新权限与现有权限的最大值 + # project_id:项目在gitlab中的id user_id:用户在gitlab中的id + # access_level为用户在项目中的权限级别,对应如下: + #GUEST = 10 + #REPORTER = 20 + #DEVELOPER = 30 + #MASTER = 40 + # add by nwb + def add_user_to_project(project_id,user_id,access_level) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members" + uri = URI.parse(url) + data = {id:project_id,user_id:user_id,access_level:access_level, private_token:GitlabHelper.gitlab_token} + begin + res = Net::HTTP.post_form(uri, data) + if res.code == '201' + temp = ActiveSupport::JSON.decode(res.body) + return true + else + return false + end + rescue =>err + return false + end + end + + # 从项目中删除用户 + # project_id:项目在gitlab中的ids user_id:用户在gitlab中的id + # add by nwb + def delete_user_from_project(project_id,user_id) + url = REPO_IP_ADDRESS + GITLAB_API + "/projects/" + project_id +"/members/" + user_id + uri = URI.parse(url) + data = {id:project_id,user_id:user_id,private_token:GitlabHelper.gitlab_token} + http = Net::HTTP.new uri.host, uri.port + begin + req = Net::HTTP::Delete.new(uri.request_uri) + req.form_data = data + res= http.start { |http| http.request req } + if res.code == '200' + temp = ActiveSupport::JSON.decode(res.body) + return true + else + return false + end + rescue =>err + return false + end + end + + # 发送post消息 + # add by nwb + def post(url, params) + uri = URI.parse(url) + http = Net::HTTP.new(uri.host, uri.port) + if uri.scheme == 'https' + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.use_ssl = true + end + begin + request = Net::HTTP::Post.new(uri.request_uri) + request['Content-Type'] = 'application/json;charset=utf-8' + request['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0' + request['X-ACL-TOKEN'] = 'xxx_token' + #request.set_form_data(params) + request.body = params.to_json + response = http.start { |http| http.request(request) } + return JSON.parse response.body + rescue =>err + return nil + end + end + + # 发送get消息 + # add by nwb + def get(url, params) + uri = URI.parse(url) + uri.query = URI.encode_www_form(params) + http = Net::HTTP.new uri.host, uri.port + if uri.scheme == 'https' + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.use_ssl = true + end + begin + request = Net::HTTP::Get.new uri.request_uri + request['Content-Type'] = 'application/json;charset=utf-8' + request['User-Agent'] = 'Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0' + request['X-ACL-TOKEN'] = 'xxx_token' + response = http.start { |http| http.request request } + return JSON.parse response.body + rescue =>err + return nil + end + end + +end \ No newline at end of file diff --git a/app/helpers/stores_helper.rb b/app/helpers/stores_helper.rb index 0c7fdac43..b4cd47eb1 100644 --- a/app/helpers/stores_helper.rb +++ b/app/helpers/stores_helper.rb @@ -16,6 +16,8 @@ module StoresHelper news_path(container) when 'Project' project_files_path(container) + when 'Course' + course_files_path(container) when 'Version' # version_path(container) project_files_path(container.project) diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb index 4ff5a48e4..99357a3d0 100644 --- a/app/helpers/welcome_helper.rb +++ b/app/helpers/welcome_helper.rb @@ -208,7 +208,7 @@ module WelcomeHelper #c1 = find_new_project(sum).to_a.dup #c2 = find_all_hot_project(sum).to_a.dup #(c2.take(sum-max)+c1.take(max)).take(sum) - find_all_hot_project(sum).to_a.dup + find_all_hot_project(sum).dup#.to_a.dup end def find_new_course limit=15 @@ -433,7 +433,7 @@ module WelcomeHelper # FROM projects AS p LEFT OUTER JOIN ( # SELECT project_id,grade FROM project_statuses # WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} ) AS t ON p.id = t.project_id ") - Project.visible.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).limit(limit).all + Project.visible.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).limit(limit)#.all end def sort_bid_by_hot_rails reward_type, limit = 10 diff --git a/app/models/first_page.rb b/app/models/first_page.rb index 113edc0f8..52c6e204d 100644 --- a/app/models/first_page.rb +++ b/app/models/first_page.rb @@ -1,5 +1,5 @@ class FirstPage < ActiveRecord::Base - attr_accessible :description, :title, :web_title,:page_type + attr_accessible :description, :title, :web_title,:page_type,:sort_type validates_presence_of :web_title, :title, :description,:page_type validates_length_of :web_title,:title, maximum: 30 validates_length_of :description, maximum: 100 diff --git a/app/models/project.rb b/app/models/project.rb index 12c932dd8..b68fc6aa9 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -156,7 +156,7 @@ class Project < ActiveRecord::Base where(nil) else pattern = "%#{arg.to_s.strip.downcase}%" - where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p ", :p => pattern) + where("LOWER(name) LIKE :p ", :p => pattern) end } scope :project_entities, -> { where(project_type: ProjectType_project) } diff --git a/app/models/user.rb b/app/models/user.rb index 9665a4fb0..195346e7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -996,4 +996,4 @@ class AnonymousUser < User def destroy false end -end \ No newline at end of file +end diff --git a/app/views/admin/first_page_made.html.erb b/app/views/admin/first_page_made.html.erb index d3b7416e4..242b7c689 100644 --- a/app/views/admin/first_page_made.html.erb +++ b/app/views/admin/first_page_made.html.erb @@ -28,6 +28,14 @@ <%= text_area_tag 'description',@first_page.description,:rows => 8, :size => 30,:style => "font-size:small;width:490px;margin-left:10px;" %>

+

+ + +

<%= submit_tag l(:button_save), :class => "small", :name => nil %> <% end %> diff --git a/app/views/files/_course_file.html.erb b/app/views/files/_course_file.html.erb index d9913539f..a1547bc2b 100644 --- a/app/views/files/_course_file.html.erb +++ b/app/views/files/_course_file.html.erb @@ -11,7 +11,9 @@
<%= link_to(l(:label_upload_files), 'javascript:void(0);', :class => 'icon m5p5 button_submit', :onclick => "$('#relation_file_div').slideUp();$('#upload_file_div').slideToggle('slow');") if User.current.allowed_to?(:manage_files, @course) %> <%= link_to(l(:label_relation_files), 'javascript:void(0);', :onclick => "$('#upload_file_div').slideUp();$('#relation_file_div').slideToggle();", :class => 'icon m5p5 button_submit') if User.current.allowed_to?(:manage_files, @course) %> - +

+
+ <% if attachmenttypes.any? %>       @@ -24,7 +26,7 @@ <%= select_tag "attach_sufix_browse", content_tag(:option, l(:attachment_all), :value => '0') +options_for_select(sufixtypes), :onchange => "course_attachment_contenttypes_searchex(this.value)" %> <% end %> - +
diff --git a/app/views/files/_project_file.html.erb b/app/views/files/_project_file.html.erb index 1212db052..67067a638 100644 --- a/app/views/files/_project_file.html.erb +++ b/app/views/files/_project_file.html.erb @@ -11,7 +11,9 @@
<%= link_to(l(:label_upload_files), 'javascript:void(0);', :class => 'icon m5p5 button_submit', :onclick => "$('#relation_file_div').slideUp();$('#upload_file_div').slideToggle('slow');") if User.current.allowed_to?(:manage_files, @project) %> <%= link_to(l(:label_relation_files), 'javascript:void(0);', :onclick => "$('#upload_file_div').slideUp();$('#relation_file_div').slideToggle();", :class => 'icon m5p5 button_submit') if User.current.allowed_to?(:manage_files, @project) %> - +

+
+ <% if attachmenttypes.any? %>       @@ -24,6 +26,7 @@ <%= select_tag "attach_sufix_browse", content_tag(:option, l(:attachment_all), :value => '0') +options_for_select(sufixtypes), :onchange => "attachment_contenttypes_searchex(this.value)" %> <% end %> +