From 41f315aac0c0f622b2374a3fbade2e53e5034d86 Mon Sep 17 00:00:00 2001 From: yanxd Date: Mon, 14 Apr 2014 17:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/test_controller.rb | 196 ++++++----------------------- app/views/test/courselist.html.erb | 42 +++++++ app/views/test/zip.html.erb | 1 + config/routes.rb | 7 +- 4 files changed, 88 insertions(+), 158 deletions(-) create mode 100644 app/views/test/courselist.html.erb create mode 100644 app/views/test/zip.html.erb diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb index 46ae016ed..f16eb939a 100644 --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -1,160 +1,44 @@ class TestController < ApplicationController - - before_filter :find_user, :only => [:new, :create, :destroy] - - -def index - # @users = User.where('status = ?', 1) - # for user in @users - # if user.user_extensions.nil? - # UserExtensions.create(:user_id => user.id) - # end - # end - - # @message = Message.all - # @message.each do |m| - # Activity.create(:act_id => m.id, :act_type => 'Message', :user_id => m.author_id) - # end - # activity = Message.all - # activity += News.all - # activity += Journal.all - # activity += Issue.all - # activity += Bid.all - # @activity = activity.sort {|x,y| x.created_on <=> y.created_on} - # @activity.each do |act| - # if act.instance_of?(Bid) - # act.acts << Activity.new(:user_id => act.author_id) - # elsif act.instance_of?(News) - # act.acts << Activity.new(:user_id => act.author_id) - # elsif act.instance_of?(Message) - # act.acts << Activity.new(:user_id => act.author_id) - # elsif act.instance_of?(Journal) - # act.acts << Activity.new(:user_id => act.user_id) - # elsif act.instance_of?(Issue) - # act.acts << Activity.new(:user_id => act.author_id) - # elsif act.instance_of?(Changeset) - # act.acts << Activity.new(:user_id => act.user_id) - # end - # end - - - #@watchers_of_projects = WatchersOfProjects.new - #@watchers_of_projects.user_id = 1 - #@watchers_of_projects.project_id = 1 - #@watchers_of_projects.save - - #测试user表与watch_project表之间的关联是否成功 - #@user = User.find(params[:id]) - #@watch_table = @user.watch_projects.to_a.first - - #@watch = WatchProject.find(1) - #@watcher = @watch.user - - #测试通过watch_project表使user表可以访问project表 - #@watch_project = @user.projects - #watch_project_path(@watch) - #@project = Project.find(11) - #project_path(@project) - #@member = @project.users - #@watched = @project.watch_projects - #@issue = Issue.find(6) - - - #user_path(@user) - #issue_path(@issue) - - #@watcher2=WatchProject.where("#{WatchProject.table_name}.project_id = ?" , temp) - - #测试where语句 - #temp = 1 - #@watcher2=WatchProject.where(:project_id => temp).to_a - - #测试新建记录 - #@watch_new = WatchProject.new - #@watch_new.user_id = 4 - #@watch_new.project_id = 1 - #@watch_new.save - #@id = params[:id] - - #测试添加关注项目功能 - #WatchersOfProjects.watch(3,10) - #Project.find(50) - #测试统计关注该项目的用户数 - #@count = WatchersOfProjects.watcher_count(@watch_project.to_a.first) - #测试取消关注功能 - #WatchersOfProjects.watch_cancle(10,35) - - #测试关注用户功能 - #测试关注功能 - #WatchersOfUser.watch_user(7,7) - #测试取消关注功能 - #WatchersOfUser.cancel_watching_user(1,2) - #测试查找关注的人功能 - #@user = WatchersOfUser.find_users(1) - #测试查找被关注的人功能 - #@user = WatchersOfUser.find_watchers(10) - - #测试用户留言功能 - #测试留言功能 - # MessagesForUser.leave_message(User.current.id, 6, 'test') - #测试查找留言功能 - #@message_table = MessagesForUser.find_message(3) - #测试查找留言用户功能 - #@messager=@message_table.first.find_messager - - - #测试需求 - #测试新建需求 - #bids = Bid.creat_bids(10000, '2013.7.25', 'test', 'sfsadgfag') - #测试修改需求 - #bids.update_bids(10, '2014.7.222', 'asdf') - #测试删除需求 - # bids = Bid.where('id = ?', 5) - # bids.each do |bid| - # bid.delete_bids - # end + def zip + homeworks_attach_path = [] + homework_id = params[:homework_id] + bid = Bid.find_by_id(homework_id) + + bid.homeworks.each do |homeattach| + homeattach.attachments.each do |attach| + homeworks_attach_path << attach.diskfile + end end - - # ##########留言功能 message by fq - # def new - # end -# - # def create -# - # if params[:user_search].size>0 - # unless params[:user_id].nil? - # message = params[:user_search] - # MessagesForUser.leave_message(User.current.id, params[:user_id], message) - # @message = MessagesForUser.find_message(@user.id) - # end - # end -# - # respond_to do |format| - # # format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} - # format.js - # #format.api { render_api_ok } - # end - # end -# - # def destroy - # MessagesForUser.delete_message(params[:object_id]) - # @message = MessagesForUser.find_message(@user.id) - # respond_to do |format| - # format.html { redirect_to :back } - # format.js - # #format.api { render_api_ok } - # end - # end - - # private -# - # def find_user - # if params[:user_id] - # @user = User.find(params[:user_id]) - # end - # rescue - # render_404 - # end - #######end of message + @paths = homeworks_attach_path + zipfile = ziping homeworks_attach_path + send_file zipfile, :filename => bid.name, + :type => detect_content_type(zipfile) + + end + + def courselist + @courses = Project.course_entities + end + + def ziping files_path + folder = "/tmp" + input_filename = files_path + zipfile_name = "/tmp/archive_#{Time.now.to_i}.zip" + + Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| + input_filename.each do |filename| + zipfile.add(filename, folder + '/' + filename) + end + zipfile.get_ouput_stream("ReadMe"){ |os| + os.write "myFile contains just this" + } + end + zipfile_name + end + + def detect_content_type(name) + content_type = Redmine::MimeType.of(name) + content_type.to_s + end end \ No newline at end of file diff --git a/app/views/test/courselist.html.erb b/app/views/test/courselist.html.erb new file mode 100644 index 000000000..b078c85a8 --- /dev/null +++ b/app/views/test/courselist.html.erb @@ -0,0 +1,42 @@ + +<% @courses.each do |course| %> +
+ <%= course.name %> + + <% course.homeworks.each do |homework| %> + <% homeworks_attach_path = [] %> +
+ <%= homework.name %>(<%=homework.homeworks.count %>)<%#Bid%> +
+ <%= link_to "package", test_zip_path(:homework_id => homework.id)%>
+ <% homework.homeworks.each do |homeattach|%><%#homework.class == Bid %> + <% homeattach.attachments.each do |attach|%> + <%= link_to_attachment attach, author: true %> (<%=attach.author%>) +
+ <% homeworks_attach_path << attach.storage_path %> + <% end %> + <% end %> +
+ <%# 所有作业的文件列表%> + +
+ <% end %> +
+
+<% end %> diff --git a/app/views/test/zip.html.erb b/app/views/test/zip.html.erb new file mode 100644 index 000000000..8992b9bb2 --- /dev/null +++ b/app/views/test/zip.html.erb @@ -0,0 +1 @@ +<%= debug @paths.to_yaml %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index aa94a49dc..d01b71dd4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. RedmineApp::Application.routes.draw do + namespace :test do + match 'courselist' + match 'zip' + end + resources :stores do collection do match 'search', via: [:get, :post] @@ -537,8 +542,6 @@ RedmineApp::Application.routes.draw do # match 'calls/:id/set_prizes',:controller => 'bids',:action => 'set_prizes',:as => 'set_prizes' match 'calls/:id/set_reward',:controller => 'bids',:action => 'set_reward',:as => 'set_reward' - ## 测试用 - match 'test/index', :controller => 'test', :action => 'index' # added by young match 'calls', :controller => 'bids', :action => 'index'