From fc325dce2dadeac451af9bb8ce70c3d80882d360 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 4 May 2016 20:30:35 +0800 Subject: [PATCH] now can import files and links with excel --- .../admin/announcements_controller.rb | 5 ++ app/helpers/admin/announcements_helper.rb | 66 +++++++++++++++++++ .../announcements/excel_format.xlsx.axlsx | 30 +++++++++ app/views/admin/announcements/import.html.erb | 33 +++++++++- config/routes.rb | 1 + 5 files changed, 132 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index ad55ed9..5f5ca32 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -55,6 +55,11 @@ class Admin::AnnouncementsController < OrbitAdminController 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")) + redirect_to admin_announcements_path + end def importanns workbook = RubyXL::Parser.parse(params["import_file"].tempfile) diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb index 68772cc..501697d 100644 --- a/app/helpers/admin/announcements_helper.rb +++ b/app/helpers/admin/announcements_helper.rb @@ -1,3 +1,7 @@ +require "net/http" +require "uri" +require 'json' + module Admin::AnnouncementsHelper def page_for_bulletin(bulletin) ann_page = nil @@ -82,6 +86,31 @@ module Admin::AnnouncementsHelper value["zh_tw"] = val anns.text_translations = value value = {} + when 16 + links = val.split(";") + desc_en = row.cells[17].value.split(";") + desc_zh_tw = row.cells[18].value.split(";") + links.each_with_index do |link,i| + bl = BulletinLink.new + bl.url = link.strip + bl.title_translations = {"en" => desc_en[i], "zh_tw" => desc_zh_tw[i]} + bl.bulletin_id = anns.id + bl.save + end + when 19 + files = val.split(";") + desc_en = row.cells[20].value.split(";") + desc_zh_tw = row.cells[21].value.split(";") + alt_en = row.cells[22].value.split(";") + alt_zh_tw = row.cells[23].value.split(";") + files.each_with_index do |file, i| + bf = BulletinFile.new + bf.remote_file_url = file.strip rescue nil + bf.title_translations = {"en" => (desc_en[i] rescue ""), "zh_tw" => (desc_zh_tw[i] rescue "")} + bf.description_translations = {"en" => (alt_en[i] rescue ""), "zh_tw" => (alt_zh_tw[i] rescue "")} + bf.bulletin_id = anns.id + bf.save + end end end anns.create_user_id = current_user.id.to_s @@ -90,6 +119,43 @@ module Admin::AnnouncementsHelper anns.save end + def download_tmp_xml(url) + xml = File.join(Rails.root, "tmp", "ann_cc_ntu.xml") + open(xml, 'wb') do |fo| + fo.print open(url).read + end + end + + def import_from_tmp_xml(file) + xml = Nokogiri::XML(file) + return if xml.nil? + announcements = [] + xml.xpath("//channel").xpath("//item").each do |anns| + announcements << { + :title => (anns>"title").text, + :category => (anns>"category").text, + :postdate => (anns>"pubDate").text, + :text => (anns>"description").text, + :rss2_sn => (anns>"link").text.split("=").last + } + end + announcements.each do |anns| + ma = ModuleApp.where(:key => "announcement").first + cat = Category.where(:title => anns[:category]).first rescue nil + if cat.nil? + cat = Category.create(:title_translations => {"en" => anns[:category], "zh_tw" => anns[:category]}, :module_app_id => ma.id) + end + ann = Bulletin.where(:rss2_sn => anns[:rss2_sn]).first rescue nil + if ann.nil? + ann = Bulletin.new(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}, :rss2_sn => anns[:rss2_sn], :category_id => cat.id, :approved => true, :create_user_id => current_user.id) + else + ann.update_attributes(:title_translations => {"en" => "", "zh_tw" => anns[:title]}, :postdate => anns[:postdate], :subtitle_translations => {"en" => "", "zh_tw" => anns[:title]}, :text_translations => {"en" => "", "zh_tw" => anns[:text]}) + end + ann.save + end + File.delete(file) + end + def load_access_level if (current_user.is_admin? rescue false) @access_level = "admin" diff --git a/app/views/admin/announcements/excel_format.xlsx.axlsx b/app/views/admin/announcements/excel_format.xlsx.axlsx index 1ecceaf..bc4ab8a 100644 --- a/app/views/admin/announcements/excel_format.xlsx.axlsx +++ b/app/views/admin/announcements/excel_format.xlsx.axlsx @@ -88,6 +88,36 @@ wb.add_worksheet(name: "Annoucement") do |sheet| row1 << "editor" row2 << "" + row << t("link") + row1 << "textfield" + row2 << "Seperate with ';'. Example: http://rulingcom.com; http://google.com" + + row << t("link") + " " + t("url_alt") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine" + row << t("link") + " " + t("url_alt") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Rulingcom official site; Google search engine" + + row << t("file_") + row1 << "textfield" + row2 << "Seperate with ';'. Example: http://www.example.com/images/example.png; http://www.example.com/images/example2.png" + + row << t("file_") + " " + t("description") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait" + row << t("file_") + " " + t("description") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : Great view; Nice potrait" + + row << t("file_") + " " + t("alternative") + " - " + t("en") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2" + row << t("file_") + " " + t("alternative") + " - " + t("zh_tw") + row1 << "textfield" + row2 << "Seperate with ';' with respective to the links in the link columns. Example : example1; example2" + + sheet.add_row row, :style => heading sheet.add_row row1 sheet.add_row row2, :style => example diff --git a/app/views/admin/announcements/import.html.erb b/app/views/admin/announcements/import.html.erb index 27e34c8..3a09775 100644 --- a/app/views/admin/announcements/import.html.erb +++ b/app/views/admin/announcements/import.html.erb @@ -1,4 +1,8 @@ -
+<% content_for :page_specific_javascript do %> + +<% end %> + +

Import from Excel

<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<% if @module_app.categories.count > 0 %> @@ -10,7 +14,7 @@
- + Please create all the tags and categories before hand. Only excel file is allowed.
@@ -27,4 +31,27 @@
<% end %> -
\ No newline at end of file + + + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 507ab7c..19ccf4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ Rails.application.routes.draw do post 'announcement/preview', to: 'announcements#preview' post 'announcement/createfeed', to: 'announcements#createfeed' post 'announcement/importanns', to: 'announcements#importanns' + post 'announcement/import_from_xml', to: 'announcements#import_from_xml' get 'announcement/excel_format', to: 'announcements#excel_format' patch 'announcement/updatefeed', to: 'announcements#updatefeed' delete 'announcement/deletefeed', to: 'announcements#deletefeed'