From 65d62667d632cdc252e906e7af5aaedacbe1d74c Mon Sep 17 00:00:00 2001 From: guange <8863824@gmail.com> Date: Thu, 23 Apr 2015 17:42:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=8A=A0=E5=85=A5=E4=B8=89?= =?UTF-8?q?=E4=B8=AA=E9=82=AE=E4=BB=B6=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 3 +- app/controllers/my_controller.rb | 15 ++- app/controllers/test_controller.rb | 95 ++++++++++++++++++- app/models/mailer.rb | 18 ++-- app/models/token.rb | 8 ++ app/views/mailer/issue_add.html.erb | 14 ++- app/views/mailer/issue_edit.html.erb | 11 ++- .../mailer/send_for_user_activities.html.erb | 12 ++- config/configuration.yml | 8 ++ config/locales/my/zh.yml | 1 + config/locales/zh.yml | 6 +- config/routes.rb | 2 + script/delayed_job | 10 +- 13 files changed, 170 insertions(+), 33 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 47251cf85..741af2aee 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -894,7 +894,6 @@ class ApplicationController < ActionController::Base set_autologin_cookie(user) end call_hook(:controller_account_success_authentication_after, {:user => user }) - - end + end diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 7ff015fbc..d42a750a1 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -20,7 +20,7 @@ class MyController < ApplicationController # edit before_filter :auth_login1, :only => [:account] # - before_filter :require_login + before_filter :require_login, except: [:change_mail_notification] helper :issues helper :users @@ -75,6 +75,19 @@ class MyController < ApplicationController end end + def change_mail_notification + token = params[:token] + user = try_to_autologin1 + if user + user.mail_notification = params[:mail_notification] + user.save + flash[:notice] = l(:notice_mail_notification_updated) + redirect_to my_account_url + else + redirect_to signin_url + end + end + # Edit user's account def account @user = User.current diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb index 22cf7d1d5..51ff293f1 100644 --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -57,5 +57,98 @@ class TestController < ApplicationController attach.filename end + def mailer() + raise unless Rails.env.development? + @user = User.find(params[:user_id]) + send_for_user_activities(@user, Time.now,1) + render 'mailer/send_for_user_activities' + end + def send_for_user_activities(user, date_to, days) + date_from = date_to - days.days -end \ No newline at end of file + subject = "[ #{user.show_name}#{l(:label_day_mail)}]" + @subject = " #{user.show_name}#{l(:label_day_mail)}" + + date_from = "#{date_from} 17:59:59" + date_to = "#{date_to} 17:59:59" + + # 生成token用于直接点击登录 + @user = user + token = Token.new(:user =>user , :action => 'autologin') + token.save + @token = token + + # 查询user参加的项目及课程 + projects = user.projects + courses = user.courses + project_ids = projects.map{|project| project.id}.join(",") + course_ids = courses.map {|course| course.id}.join(",") + + # 查询user的缺陷,包括发布的,跟踪的以及被指派的缺陷 + sql = "select DISTINCT i.* from issues i, watchers w + where (i.assigned_to_id = #{user.id} or i.author_id = #{user.id} + or (w.watchable_type = 'Issue' and w.watchable_id = i.id and w.user_id = #{user.id})) + and (i.created_on between '#{date_from}' and '#{date_to}') order by i.created_on desc" + @issues = Issue.find_by_sql(sql) + + # @bids 查询课程作业,包括老师发布的作业,以及user提交作业 + # @attachments查询课程课件更新 + @attachments ||= [] + + @bids ||= [] # 老师发布的作业 + + unless courses.first.nil? + count = courses.count + count = count - 1 + for i in 0..count do + bids = courses[i].homeworks.where("bids.created_on between '#{date_from}' and '#{date_to}'").order("bids.created_on desc") + attachments = courses[i].attachments.where("attachments.created_on between '#{date_from}' and '#{date_to}'").order('attachments.created_on DESC') + @bids += bids if bids.count > 0 + @attachments += attachments if attachments.count > 0 + end + end + # user 提交的作业 + @homeworks = HomeworkAttach.where("user_id=#{user.id} and (created_at between '#{date_from}' and '#{date_to}')").order("created_at desc") + + # 查询user在课程。项目中发布的讨论帖子 + messages = Message.find_by_sql("select DISTINCT * from messages where author_id = #{user.id} and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + @course_messages ||= [] + @project_messages ||= [] + unless messages.first.nil? + messages.each do |msg| + if msg.project + @project_messages << msg + elsif msg.course + @course_messages << msg + end + end + end + # 查询user在课程中发布的通知,项目中发的新闻 + @course_news = (course_ids && !course_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n + where n.course_id in (#{course_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + @project_news = (project_ids && !project_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n where n.project_id in (#{project_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + + # 查询user在课程及个人中留言 + @course_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT * from journals_for_messages where + jour_type='Course' and user_id = #{user.id} + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + @user_journal_messages = user.journals_for_messages.where("m_parent_id IS NULL and (created_on between '#{date_from}' and '#{date_to}')").order('created_on DESC') + + + # 查询user新建贴吧或发布帖子 + @forums = Forum.find_by_sql("select DISTINCT * from forums where creator_id = #{user.id} and (created_at between '#{date_from}' and '#{date_to}') order by created_at desc") + @memos = Memo.find_by_sql("select DISTINCT m.* from memos m, forums f where (m.author_id = #{user.id} or (m.forum_id = f.id and f.creator_id = #{user.id})) + and (m.created_at between '#{date_from}' and '#{date_to}') order by m.created_at desc") + + + has_content = [@issues,@homeworks,@course_messages,@project_messages,@course_news,@project_news, + @course_journal_messages,@user_journal_messages,@forums,@memos,@attachments,@bids].any? {|o| + !o.empty? + } + #有内容才发,没有不发 + end + + +end diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 3741e43cb..eacead013 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -72,11 +72,8 @@ class Mailer < ActionMailer::Base # 生成token用于直接点击登录 @user = user - token = Token.new(:user =>user , :action => 'autologin') - token.save - @token = token + @token = Token.get_token_from_user(user, 'autologin') - @user_url = url_for(my_account_url(user,:token => @token.value)) # 查询user参加的项目及课程 projects = user.projects courses = user.courses @@ -256,10 +253,9 @@ class Mailer < ActionMailer::Base @author = issue.author @issue = issue user = User.find_by_mail(recipients) - token = Token.new(:user =>user , :action => 'autologin') - token.save - @token = token - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value) + @user = user + @token = Token.get_token_from_user(user, 'autologin') + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value) # edit @issue_author_url = url_for(user_activities_url(@author,:token => @token.value)) @@ -299,10 +295,8 @@ class Mailer < ActionMailer::Base @author = journal.user user = User.find_by_mail(recipients) - - token = Token.new(:user =>user , :action => 'autologin') - token.save - @token = token + @user = user + @token = Token.get_token_from_user(user, 'autologin') # edit diff --git a/app/models/token.rb b/app/models/token.rb index 3131bce8d..c89ff30bc 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -27,6 +27,14 @@ class Token < ActiveRecord::Base self.value = Token.generate_token_value end + def self.get_token_from_user(user, action) + token = Token.where(:action => action, :user_id => user).first + unless token + token = Token.create(user: user, action: action) + end + token + end + # Return true if token has expired def expired? return Time.now > self.created_on + @@validity_time diff --git a/app/views/mailer/issue_add.html.erb b/app/views/mailer/issue_add.html.erb index 0b60097a7..d77149831 100644 --- a/app/views/mailer/issue_add.html.erb +++ b/app/views/mailer/issue_add.html.erb @@ -1,9 +1,13 @@
- - <%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :issue_url => @issue_url } %> - -
-
<%= link_to( l(:mail_issue_footer), @user_url , :style=>'font-size:12px; font-weight:normal; color:#1b55a7;') %>
+
+ <% [:label_user_mail_option_all, :label_user_mail_option_day, :label_user_mail_option_none].each do |mail_option| %> + <% if Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten][@user.mail_notification] == mail_option %> + + <% else %> + <%= link_to l(mail_option), my_change_mail_notification_url(token: @token.value,mail_notification: Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten].invert[mail_option]), :style => "margin-top:20px;color:#2775d2; margin-left:10px;" %> + <% end %> + <% end %> +
diff --git a/app/views/mailer/issue_edit.html.erb b/app/views/mailer/issue_edit.html.erb index 35b963b41..a02fe47f5 100644 --- a/app/views/mailer/issue_edit.html.erb +++ b/app/views/mailer/issue_edit.html.erb @@ -3,5 +3,14 @@ <%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :issue_url => @issue_url } %> -
<%= link_to( l(:mail_issue_footer), @user_url, :style=>'font-size:12px; font-weight:normal; color:#1b55a7;') %>
+ +
+ <% [:label_user_mail_option_all, :label_user_mail_option_day, :label_user_mail_option_none].each do |mail_option| %> + <% if Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten][@user.mail_notification] == mail_option %> + + <% else %> + <%= link_to l(mail_option), my_change_mail_notification_url(token: @token.value,mail_notification: Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten].invert[mail_option]), :style => "margin-top:20px;color:#2775d2; margin-left:10px;" %> + <% end %> + <% end %> +
diff --git a/app/views/mailer/send_for_user_activities.html.erb b/app/views/mailer/send_for_user_activities.html.erb index 712e11246..73a561ca3 100644 --- a/app/views/mailer/send_for_user_activities.html.erb +++ b/app/views/mailer/send_for_user_activities.html.erb @@ -3,6 +3,8 @@ + +

<%= @subject %> @@ -365,9 +367,13 @@

<% end %>
- <%= link_to l(:mail_footer), @user_url, :style => "margin-top:20px;color:#2775d2; margin-left:10px;" %> + <% [:label_user_mail_option_all, :label_user_mail_option_day, :label_user_mail_option_none].each do |mail_option| %> + <% if Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten][@user.mail_notification] == mail_option %> + + <% else %> + <%= link_to l(mail_option), my_change_mail_notification_url(token: @token.value,mail_notification: Hash[*User::MAIL_NOTIFICATION_OPTIONS.flatten].invert[mail_option]), :style => "margin-top:20px;color:#2775d2; margin-left:10px;" %> + <% end %> + <% end %>
- - diff --git a/config/configuration.yml b/config/configuration.yml index 415d4b072..5eba1c71d 100644 --- a/config/configuration.yml +++ b/config/configuration.yml @@ -215,3 +215,11 @@ production: # specific configuration options for development environment # that overrides the default ones development: + delivery_method: :smtp + smtp_settings: + address: mail.trustie.net + port: 25 + domain: mail.trustie.net + authentication: :login + user_name: "mail@trustie.net" + password: "loong2010" diff --git a/config/locales/my/zh.yml b/config/locales/my/zh.yml index 5a4a7c255..35fd4e48a 100644 --- a/config/locales/my/zh.yml +++ b/config/locales/my/zh.yml @@ -68,6 +68,7 @@ zh: label_user_extensions: 其他信息 notice_account_updated: 帐号更新成功 + notice_mail_notification_updated: 邮件通知设置成功 # diff --git a/config/locales/zh.yml b/config/locales/zh.yml index 1d5add364..de3ae4d53 100644 --- a/config/locales/zh.yml +++ b/config/locales/zh.yml @@ -744,9 +744,10 @@ zh: label_theme: 主题 label_default: 默认 label_search_titles_only: 仅在标题中搜索 - label_user_mail_option_all: "收取我的所有通知" + label_user_mail_option_all: "实时发送邮件" + label_user_mail_option_day: "按日发送邮件" + label_user_mail_option_none: "不发送邮件" label_user_mail_option_week: "按周收取我的所有通知" - label_user_mail_option_day: "按天收取我的所有通知" label_must_answer: "必答" label_poll_title: 问卷调查_问卷页面 #huang @@ -782,7 +783,6 @@ zh: label_project_cousre_studentun: "你还未加入任何课程,赶快加入吧!" #end by huang label_user_mail_option_selected: "收取选中项目的所有通知" - label_user_mail_option_none: "不收取任何通知" label_user_mail_option_only_my_events: "只收取我跟踪或参与的项目的通知" label_user_mail_option_only_assigned: "只收取分配给我的" label_user_mail_option_only_owner: 只收取由我创建的 diff --git a/config/routes.rb b/config/routes.rb index ffab908bd..d3b8a1b37 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -109,6 +109,7 @@ RedmineApp::Application.routes.draw do namespace :test do match 'courselist' match 'zip' + match 'mailer' end ##new added by linchun #以发布应用的形式参与竞赛 resources :softapplications do @@ -328,6 +329,7 @@ RedmineApp::Application.routes.draw do match 'my/add_block', :via => :post match 'my/remove_block', :via => :post match 'my/order_blocks', :via => :post + match 'my/change_mail_notification', via: :get get 'my/page2', :to => 'my#page2', :as => "my_page2" diff --git a/script/delayed_job b/script/delayed_job index 17f9f6e90..edf195985 100644 --- a/script/delayed_job +++ b/script/delayed_job @@ -1,5 +1,5 @@ -#!/usr/bin/env ruby - -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) -require 'delayed/command' -Delayed::Command.new(ARGV).daemonize +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment')) +require 'delayed/command' +Delayed::Command.new(ARGV).daemonize