From 59b23772c30f179edc1ba4bf0250888232424308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sun, 15 Sep 2024 10:22:04 +0800 Subject: [PATCH] Add email_sentdate setting. --- .../admin/e_paper_criterias_controller.rb | 8 +++++++- app/models/paper_criteria.rb | 9 +++++++++ app/views/admin/e_paper_criterias/_form.html.erb | 8 ++++++++ app/views/admin/e_paper_criterias/_index.html.erb | 2 +- config/locales/en.yml | 3 +++ config/locales/zh_tw.yml | 3 +++ lib/tasks/e_paper_tasks.rake | 13 +++++++++---- 7 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb index 55480e1..c7ec817 100644 --- a/app/controllers/admin/e_paper_criterias_controller.rb +++ b/app/controllers/admin/e_paper_criterias_controller.rb @@ -60,7 +60,13 @@ def initialize # rake = Rake::Task['epaper:send_email'].invoke([params[:id], request.host_with_port]) paper = PaperCriteria.find(params[:id]) rescue nil if !paper.nil? && paper.invalid_emails.blank? - render :json => {"success" => true}.to_json + email_sentdate = paper.get_email_sentdate + if paper.get_email_sentdate + msg = I18n.t('e_paper.newsletter_will_be_sent', {time: email_sentdate.strftime("%Y-%m-%d %H:%M")}) + else + msg = I18n.t('e_paper.newsletter_sent') + end + render :json => {"success" => true, "msg": msg}.to_json else invalid_email_list = paper.invalid_emails rescue 'can not find paper' render :json => {"success" => false,"invalid_emails"=>invalid_email_list}.to_json diff --git a/app/models/paper_criteria.rb b/app/models/paper_criteria.rb index 283f131..b8fd8c4 100644 --- a/app/models/paper_criteria.rb +++ b/app/models/paper_criteria.rb @@ -16,6 +16,7 @@ class PaperCriteria field :other_emails field :invalid_emails, type: Array, :default => [] field :send_failed_emails, type: Array, :default => [] + field :email_sentdate, :type => DateTime mount_uploader :image, ImageUploader @@ -32,5 +33,13 @@ class PaperCriteria return [] end end + + def get_email_sentdate + _email_sentdate = self.email_sentdate + if _email_sentdate && _email_sentdate < DateTime.now + _email_sentdate = nil + end + _email_sentdate + end end \ No newline at end of file diff --git a/app/views/admin/e_paper_criterias/_form.html.erb b/app/views/admin/e_paper_criterias/_form.html.erb index 42ca732..00c01fe 100644 --- a/app/views/admin/e_paper_criterias/_form.html.erb +++ b/app/views/admin/e_paper_criterias/_form.html.erb @@ -96,6 +96,14 @@
Separated by , (comma)
+
+
+ +
+ <%= f.datetime_picker :email_sentdate, :no_label => true, :new_record => @paper_criteria.new_record? %> +
+
+
diff --git a/app/views/admin/e_paper_criterias/_index.html.erb b/app/views/admin/e_paper_criterias/_index.html.erb index 1680f2f..f788cc2 100644 --- a/app/views/admin/e_paper_criterias/_index.html.erb +++ b/app/views/admin/e_paper_criterias/_index.html.erb @@ -73,7 +73,7 @@ type : "get", success: function(data){ if (data.success){ - alert("Newsletter sent.") + alert(data.msg) }else{ var window_width = window.innerWidth var window_height = window.innerHeight diff --git a/config/locales/en.yml b/config/locales/en.yml index 69a1a5d..a2231ff 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -51,3 +51,6 @@ en: receivers: Receivers send_newsletter: Send Newsletter language_to_send: "Languages allow be subscribed" + email_sentdate: Email Time + newsletter_sent: "Newsletter sent." + newsletter_will_be_sent: "Newsletter will be sent at %{time}." diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 20b1222..e156e03 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -52,3 +52,6 @@ zh_tw: receivers: 收件人 send_newsletter: 發送時事通訊 language_to_send: 開放訂閱語系 + email_sentdate: 寄送時間 + newsletter_sent: "電子報已寄送!" + newsletter_will_be_sent: "電子報會於%{time}寄送!" \ No newline at end of file diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake index 963535f..30e95d7 100644 --- a/lib/tasks/e_paper_tasks.rake +++ b/lib/tasks/e_paper_tasks.rake @@ -11,6 +11,7 @@ namespace :epaper do papers_data = Admin::EpaperHelper.get_paper_data(paper) domain = args[:domain] if !paper.nil? + email_sentdate = paper.get_email_sentdate email_format = /^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$/ subscribers = EPaperSubscriber.where(:language.in=>paper.language_to_send,:email.nin=>['',nil],:subscribed.ne=>false) subscribers.update_all(:last_paper_sent => paper.id.to_s) @@ -41,11 +42,15 @@ namespace :epaper do emails_valid.each_slice(mail_limit).each do |slice_emails| invalid_mails = epaper_verify_email(from_addr,slice_emails,tls) deliver_emails = slice_emails - invalid_mails - email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => deliver_emails, :module_app_key=>'e_paper') + email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => deliver_emails, :module_app_key=>'e_paper', :mail_sentdate=> email_sentdate) begin email.save - email.deliver - puts "Sending #{deliver_emails.count} emails" + if email_sentdate + puts "Emails will be sent at #{email_sentdate.strftime("%Y-%m-%d %H:%M")}" + else + email.deliver + puts "Sending #{deliver_emails.count} emails" + end rescue => e puts [deliver_emails.inspect,e,e.backtrace] tmp += deliver_emails @@ -53,7 +58,7 @@ namespace :epaper do error_flag = 0 begin invalid_mails.each do |invalid_mail| - email = Email.create(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => invalid_mail, :module_app_key=>'e_paper') + email = Email.create(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper_id" => paper.id, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => invalid_mail, :module_app_key=>'e_paper', :mail_sentdate=> email_sentdate) end rescue => e puts [e,e.backtrace]