diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 7646ffe..7302021 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -169,14 +169,54 @@ class Admin::AnnouncementsController < OrbitAdminController end def export_excel - @announcements = Bulletin.all.desc(:created_at) - respond_to do |format| - format.xlsx { - response.headers['Content-Disposition'] = 'attachment; filename="announcement_export.xlsx"' - } + @thread = Multithread.where(:key=>'export_announcements').first + update_flag = true + if @thread.nil? + @thread = Multithread.create(:key=>'export_announcements',:status=>{:status=>'Processing'}) + else + update_flag = false if @thread.status[:status] == 'Processing' && @thread.respond_to?(:updated_at) && (@thread.updated_at > DateTime.now - 1.minute rescue false) + if update_flag + @thread.update(:status=>{:status=>'Processing'}) + end + end + if update_flag + Thread.new do + begin + @announcements = Bulletin.all.desc(:created_at) + last_updated = Bulletin.max(:updated_at).to_i + filename = "public/announcement_export_#{last_updated}.xlsx" + if File.exist?(filename) + @thread.update(:status=>{:status=>'finish','finish_percent'=>100,'info'=>I18n.t('announcement.read_from_cache')}) + else + excel_contents = render_to_string( handlers: [:axlsx], formats: [:xlsx] ,partial: 'export_excel.xlsx',locals: {:@announcements=>@announcements,:@thread=>@thread} ) + File.open(filename, 'w') do |f| + f.write excel_contents + end + end + @thread.status[:file] = filename + @thread.status[:filename] = "announcement_export_#{DateTime.now.in_time_zone(Time.zone.utc_offset / 3600).strftime('%Y_%m_%d_%H%M')}.xlsx" + @thread.save + rescue => e + @thread.status[:status] = 'error' + # @thread.status[:info] = [e.to_s, e.backtrace] + puts [e.to_s, e.backtrace] + @thread.save + end + end + end + redirect_to admin_announcement_import_path(:thread_id=>@thread.id.to_s) + end + def render_404 + render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => 404, :formats => [:html] + end + def download_file_from_thread + @thread = Multithread.where(:id=>params[:id]).first if params[:id].present? + if @thread && @thread.status[:file] + send_file(@thread.status[:file],:filename=>@thread.status[:filename]) + else + render_404 end end - def import_from_xml download_tmp_xml params["import_xml"] import_from_tmp_xml File.read(File.join(Rails.root, "tmp", "ann_cc_ntu.xml")) @@ -184,6 +224,7 @@ class Admin::AnnouncementsController < OrbitAdminController end def import + @thread = Multithread.where(:id=>params[:thread_id]).first if params[:thread_id].present? end diff --git a/app/views/admin/announcements/export_excel.xlsx.axlsx b/app/views/admin/announcements/_export_excel.xlsx.axlsx similarity index 86% rename from app/views/admin/announcements/export_excel.xlsx.axlsx rename to app/views/admin/announcements/_export_excel.xlsx.axlsx index d2454a2..128cf7c 100644 --- a/app/views/admin/announcements/export_excel.xlsx.axlsx +++ b/app/views/admin/announcements/_export_excel.xlsx.axlsx @@ -123,7 +123,13 @@ wb.add_worksheet(name: "Annoucement") do |sheet| sheet.add_row row, :style => heading sheet.add_row row1 sheet.add_row row2, :style => example - + if @thread + all_count = @announcements.count + puts_every_count = [all_count * 3 / 100, 1].max + current_count = 0 + finish_percent = 0 + @thread.update(:status=>{:status=>'Processing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end @announcements.each do |anns| row = [] row << categories.to_a.index(anns.category) @@ -168,10 +174,20 @@ wb.add_worksheet(name: "Annoucement") do |sheet| t = files.collect{|l|l.title_translations["zh_tw"]} row << t.join(";") sheet.add_row row + if @thread + current_count += 1 + if current_count % puts_every_count == 0 + finish_percent = (current_count * 100.0 / all_count).round(1) + @thread.update(:status=>{:status=>'Processing','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) + end + end + end + if @thread + finish_percent = 100 + @thread.update(:status=>{:status=>'finish','all_count'=>all_count,'current_count'=>current_count,'finish_percent'=>finish_percent}) end - end \ No newline at end of file diff --git a/app/views/admin/announcements/import.html.erb b/app/views/admin/announcements/import.html.erb index 00ce443..126758c 100644 --- a/app/views/admin/announcements/import.html.erb +++ b/app/views/admin/announcements/import.html.erb @@ -1,12 +1,41 @@ <% content_for :page_specific_javascript do %> <% end %> +<% if @thread %> +
+<% end %>