now can import files and links with excel

This commit is contained in:
Harry Bomrah 2016-05-04 20:30:35 +08:00
parent 402562f743
commit fc325dce2d
5 changed files with 132 additions and 3 deletions

View File

@ -55,6 +55,11 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
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 def importanns
workbook = RubyXL::Parser.parse(params["import_file"].tempfile) workbook = RubyXL::Parser.parse(params["import_file"].tempfile)

View File

@ -1,3 +1,7 @@
require "net/http"
require "uri"
require 'json'
module Admin::AnnouncementsHelper module Admin::AnnouncementsHelper
def page_for_bulletin(bulletin) def page_for_bulletin(bulletin)
ann_page = nil ann_page = nil
@ -82,6 +86,31 @@ module Admin::AnnouncementsHelper
value["zh_tw"] = val value["zh_tw"] = val
anns.text_translations = value anns.text_translations = value
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
end end
anns.create_user_id = current_user.id.to_s anns.create_user_id = current_user.id.to_s
@ -90,6 +119,43 @@ module Admin::AnnouncementsHelper
anns.save anns.save
end 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 def load_access_level
if (current_user.is_admin? rescue false) if (current_user.is_admin? rescue false)
@access_level = "admin" @access_level = "admin"

View File

@ -88,6 +88,36 @@ wb.add_worksheet(name: "Annoucement") do |sheet|
row1 << "editor" row1 << "editor"
row2 << "" 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 row, :style => heading
sheet.add_row row1 sheet.add_row row1
sheet.add_row row2, :style => example sheet.add_row row2, :style => example

View File

@ -1,4 +1,8 @@
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" enctype="multipart/form-data"> <% content_for :page_specific_javascript do %>
<script type="text/javascript" src="/assets/validator.js"></script>
<% end %>
<form action="<%= admin_announcement_importanns_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xls" enctype="multipart/form-data">
<h3 style="padding-left: 30px;">Import from Excel</h3>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %> <%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area"> <div class="input-area">
<% if @module_app.categories.count > 0 %> <% if @module_app.categories.count > 0 %>
@ -10,7 +14,7 @@
<div class="control-group"> <div class="control-group">
<label for="import-anns" class="control-label muted">Upload :</label> <label for="import-anns" class="control-label muted">Upload :</label>
<div class="controls"> <div class="controls">
<input type="file" id="import-anns" name="import_file" /> <input type="file" id="import-anns" name="import_file" data-fv-validation="required;mustbexls;" data-fv-messages="Cannot be empty; Must be an excel file.;" />
<span class="help-block">Please create all the tags and categories before hand. Only excel file is allowed.</span> <span class="help-block">Please create all the tags and categories before hand. Only excel file is allowed.</span>
</div> </div>
</div> </div>
@ -28,3 +32,26 @@
</div> </div>
<% end %> <% end %>
</form> </form>
<!-- <form action="<%= admin_announcement_import_from_xml_path %>" method="post" class="form-horizontal main-forms" id="import-anns-xml" enctype="multipart/form-data">
<h3 style="padding-left: 30px;">Import from XML</h3>
<%#= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="input-area">
<div class="control-group">
<label for="import-anns" class="control-label muted">URL :</label>
<div class="controls">
<input type="text" id="import-anns" name="import_xml" data-fv-validation="required;url;" data-fv-messages="Cannot be empty; Must be an URL.;" />
</div>
</div>
</div>
<div class="form-actions">
<input type="submit" value="Import" class="btn btn-primary">
</div>
</form> -->
<script type="text/javascript">
var form = new FormValidator($("#import-anns-xls"));
form.validate_functions.mustbexls = function(val){
var t = val.split("."),
ext = t[t.length - 1];
return (ext == "xls" || ext == "xlsx")
}
</script>

View File

@ -7,6 +7,7 @@ Rails.application.routes.draw do
post 'announcement/preview', to: 'announcements#preview' post 'announcement/preview', to: 'announcements#preview'
post 'announcement/createfeed', to: 'announcements#createfeed' post 'announcement/createfeed', to: 'announcements#createfeed'
post 'announcement/importanns', to: 'announcements#importanns' post 'announcement/importanns', to: 'announcements#importanns'
post 'announcement/import_from_xml', to: 'announcements#import_from_xml'
get 'announcement/excel_format', to: 'announcements#excel_format' get 'announcement/excel_format', to: 'announcements#excel_format'
patch 'announcement/updatefeed', to: 'announcements#updatefeed' patch 'announcement/updatefeed', to: 'announcements#updatefeed'
delete 'announcement/deletefeed', to: 'announcements#deletefeed' delete 'announcement/deletefeed', to: 'announcements#deletefeed'