fix error

This commit is contained in:
chiu 2020-08-26 17:57:30 +08:00
parent 2656b83ae8
commit 6c4c028dcc
3 changed files with 44 additions and 30 deletions

View File

@ -47,26 +47,8 @@ def initialize
def send_test_email
paper_criteria = PaperCriteria.find(params[:id])
papers = paper_criteria.epaper_topics.group_by(&:category)
papers_data = []
page = Page.where(:module => "e_paper").first.url rescue ""
papers_sorted = get_all_categories.map do |v|
tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)}
if tmp.count==0
tmp = nil
end
tmp
end.compact
papers_sorted.each do |paper|
paper.each do |category, topics|
topics_data = Array(topics).compact
papers_data << {
"category_title" => (category.title rescue nil),
"category" => category,
"topics" => topics_data
}
end
end
papers_data = Admin::EpaperHelper.get_paper_data(paper_criteria)
email = Email.new(:mail_subject => paper_criteria.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page, "siteurl" => "http://" + request.host_with_port ,"papers_data" => papers_data }, :mail_to => params[:email])
email.deliver
render :json => {"success" => true}.to_json
@ -74,7 +56,13 @@ def initialize
def send_newsletter
Orbit::Application.load_tasks
rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port}))
Thread.new do
begin
rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port}))
rescue => e
puts [e,e.backtrace]
end
end
# rake = Rake::Task['epaper:send_email'].invoke([params[:id], request.host_with_port])
render :json => {"success" => true}.to_json
end
@ -84,12 +72,4 @@ def initialize
def paper_criteria_params
params.require(:paper_criteria).permit!
end
def get_all_categories
app = ModuleApp.where(key: 'e_paper').first
asc_flag = app.asc rescue true
app.categories.enabled.sort_by do |category|
tmp = category.sort_number.to_i rescue 0
asc_flag ? tmp : -tmp
end
end
end

View File

@ -0,0 +1,33 @@
module Admin::EpaperHelper
extend self
def get_all_categories
app = ModuleApp.where(key: 'e_paper').first
asc_flag = app.asc rescue true
app.categories.enabled.sort_by do |category|
tmp = category.sort_number.to_i rescue 0
asc_flag ? tmp : -tmp
end
end
def get_paper_data(paper_criteria)
papers = paper_criteria.epaper_topics.group_by(&:category)
papers_data = []
papers_sorted = get_all_categories.map do |v|
tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)}
if tmp.count==0
tmp = nil
end
tmp
end.compact
papers_sorted.each do |paper|
paper.each do |category, topics|
topics_data = Array(topics).compact
papers_data << {
"category_title" => (category.title rescue nil),
"category" => category,
"topics" => topics_data
}
end
end
papers_data
end
end

View File

@ -2,7 +2,8 @@
namespace :epaper do
task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args|
paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil
page = Page.where(:module => "e_paper").first.url rescue ""
page = Page.where(:module => "e_paper").first.url rescue ""
papers_data = Admin::EpaperHelper.get_paper_data(paper)
domain = args[:domain]
if !paper.nil?
subscribers = EPaperSubscriber.all
@ -14,7 +15,7 @@ namespace :epaper do
subscriber.save
if emails.count == mail_limit
puts "Sending #{mail_limit} mails"
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain }, :mail_to => emails)
email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => emails)
email.deliver
emails = []
sleep 0.5