diff --git a/app/controllers/admin/e_paper_subscribers_controller.rb b/app/controllers/admin/e_paper_subscribers_controller.rb index 9fe5271..58428c1 100644 --- a/app/controllers/admin/e_paper_subscribers_controller.rb +++ b/app/controllers/admin/e_paper_subscribers_controller.rb @@ -15,7 +15,11 @@ class Admin::EPaperSubscribersController < OrbitAdminController @subscribers = EPaperSubscriber.order_by(sort) @subscribers = search_data(@subscribers,[:email]).page(params[:page]).per(10) - render :partial => "index" if request.xhr? + @thread = (params[:thread_id] ? Multithread.find(params[:thread_id]) : nil rescue nil) + if @thread && @thread.status[:status] == 'finish' + @thread = nil + end + render :partial => "index" if request.xhr? end def destroy @@ -43,52 +47,77 @@ class Admin::EPaperSubscribersController < OrbitAdminController render :partial => 'modal_select', :layout => false end def import_from_excel - workbook = RubyXL::Parser.parse(params["import_file"].tempfile) - subscribe_sheet = workbook['Subscribe'] - unsubscribe_sheet = workbook['Unsubscribe'] - subscribe_sheet.each_with_index do |row, i| - next if i < 1 - c0 = row.cells[0] - c1 = row.cells[1] - if c0 - email = c0.value - if email.present? - subscriber = EPaperSubscriber.where(:email=>email).first - if subscriber.nil? - subscriber = EPaperSubscriber.new(:email=>email) + thread = Multithread.where(:key=>'import_epaper_subscribers').first + if thread.nil? + thread = Multithread.create(:key=>'import_epaper_subscribers',:status=>{:status=>'Processing'}) + else + thread.update(:status=>{:status=>'Processing'}) + end + Thread.new do + workbook = RubyXL::Parser.parse(params["import_file"].tempfile) + subscribe_sheet = workbook['Subscribe'] + unsubscribe_sheet = workbook['Unsubscribe'] + all_count = (subscribe_sheet ? (subscribe_sheet.count - 1) : 0) + (unsubscribe_sheet ? (unsubscribe_sheet.count - 1) : 0) + puts_every_count = all_count * 3 / 100 + current_count = 0 + finish_percent = 0 + thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + subscribe_sheet.each_with_index do |row, i| + next if i < 1 + c0 = row.cells[0] + c1 = row.cells[1] + if c0 + email = c0.value + if email.present? + subscriber = EPaperSubscriber.where(:email=>email).first + if subscriber.nil? + subscriber = EPaperSubscriber.new(:email=>email) + end + language = c1.value + if language.blank? + language = I18n.locale.to_s + end + subscriber.subscribed = true + subscriber.language = language + subscriber.save end - language = c1.value - if language.blank? - language = I18n.locale.to_s - end - subscriber.subscribed = true - subscriber.language = language - subscriber.save + end + current_count += 1 + if current_count % puts_every_count == 0 + finish_percent = (current_count * 100.0 / all_count).round(1) + thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) end end - end - unsubscribe_sheet.each_with_index do |row, i| - next if i < 1 - c0 = row.cells[0] - c1 = row.cells[1] - if c0 - email = c0.value - if email.present? - subscriber = EPaperSubscriber.where(:email=>email).first - if subscriber.nil? - subscriber = EPaperSubscriber.new(:email=>email) + unsubscribe_sheet.each_with_index do |row, i| + next if i < 1 + c0 = row.cells[0] + c1 = row.cells[1] + if c0 + email = c0.value + if email.present? + subscriber = EPaperSubscriber.where(:email=>email).first + if subscriber.nil? + subscriber = EPaperSubscriber.new(:email=>email) + end + language = c1.value + if language.blank? + language = I18n.locale.to_s + end + subscriber.subscribed = false + subscriber.language = language + subscriber.save end - language = c1.value - if language.blank? - language = I18n.locale.to_s - end - subscriber.subscribed = false - subscriber.language = language - subscriber.save + end + current_count += 1 + if current_count % puts_every_count == 0 + finish_percent = (current_count * 100.0 / all_count).round(1) + thread.update(:status=>{:status=>'Importing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) end end + finish_percent = 100 + thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) end - redirect_to admin_e_paper_subscribers_path + redirect_to admin_e_paper_subscribers_path(thread_id: thread.id) end def download_excel_format @@ -100,12 +129,42 @@ class Admin::EPaperSubscribersController < OrbitAdminController } end end - + def batch_delete_subscribers + @thread = (params[:thread_id] ? Multithread.find(params[:thread_id]) : nil rescue nil) + if @thread && @thread.status[:status] == 'finish' + @thread = nil + end + end def delete_subscribers subscriber_ids = params['subscriber_ids'] - if subscriber_ids - EPaperSubscriber.where(:id.in=>subscriber_ids).destroy + thread = Multithread.where(:key=>'delete_epaper_subscribers').first + if thread.nil? + thread = Multithread.create(:key=>'delete_epaper_subscriber',:status=>{:status=>'Processing'}) + else + thread.update(:status=>{:status=>'Processing'}) end - redirect_to admin_e_paper_subscribers_path + if subscriber_ids + all_count = subscriber_ids.count + puts_every_count = all_count * 3 / 100 + current_count = 0 + finish_percent = 0 + thread.update(:status=>{:status=>'Deleting','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + Thread.new do + EPaperSubscriber.where(:id.in=>subscriber_ids).to_a.each do |s| + s.destroy + current_count += 1 + if current_count % puts_every_count == 0 + finish_percent = (current_count * 100.0 / all_count).round(1) + thread.update(:status=>{:status=>'Deleting','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end + end + finish_percent = 100 + thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end + thread.update(:status=>{:status=>'finish'}) + else + thread.update(:status=>{:status=>'finish'}) + end + redirect_to admin_e_paper_subscribers_batch_delete_subscribers_path(thread_id: thread.id) end end \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/_index.html.erb b/app/views/admin/e_paper_subscribers/_index.html.erb index a238e5c..5f13572 100644 --- a/app/views/admin/e_paper_subscribers/_index.html.erb +++ b/app/views/admin/e_paper_subscribers/_index.html.erb @@ -1,3 +1,22 @@ +<% if @thread %> + +<% end %> @@ -34,6 +53,9 @@ content_tag(:div, link_to(t("e_paper.export"), admin_e_paper_subscribers_export_excel_path + '.xlsx', :class=>"btn btn-success"), class: "pull-right") %>
+ <% if @thread %> + + <% end %>
- \ No newline at end of file + + \ No newline at end of file diff --git a/app/views/admin/e_paper_subscribers/_modal_select.html.erb b/app/views/admin/e_paper_subscribers/_modal_select.html.erb index 8158fd3..4c32e40 100644 --- a/app/views/admin/e_paper_subscribers/_modal_select.html.erb +++ b/app/views/admin/e_paper_subscribers/_modal_select.html.erb @@ -161,6 +161,10 @@ }; }; function select_subscribers(){ + if(window.still_processing){ + return; + } + window.still_processing = true; window.selected_subscriber_ids = []; var subscriber_cards = $('#member-filter [name="subscriber_ids[]"]:checked').map(function(i, subscriber){ window.selected_subscriber_ids.push(subscriber.value); @@ -177,6 +181,7 @@ } $('#members_field').change(); $("#member-filter").modal('hide'); + window.still_processing = false; } $(document).ready(function(){ $('#subscribers_form [type="submit"]').click(select_subscribers); diff --git a/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb b/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb index 93c3b67..1ec505c 100644 --- a/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb +++ b/app/views/admin/e_paper_subscribers/batch_delete_subscribers.html.erb @@ -40,6 +40,9 @@ + <% if @thread %> + + <% end %> <% if @thread %> @@ -47,10 +50,12 @@