From 4b302c8b2aa811daf7de661d47fc46d0ab743188 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 15 Oct 2014 20:43:35 +0800 Subject: [PATCH] added rake for orbit 4.2 upgrade --- lib/tasks/upgrade.rake | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 lib/tasks/upgrade.rake diff --git a/lib/tasks/upgrade.rake b/lib/tasks/upgrade.rake new file mode 100644 index 0000000..8dd6516 --- /dev/null +++ b/lib/tasks/upgrade.rake @@ -0,0 +1,87 @@ +require "net/http" +require "uri" +require 'json' + +namespace :upgrade do + desc "Imports announcements from orbit 4.2" + task :import_announcement,[:url] => :environment do |task,args| + puts "Starting to import announcments from #{args.url}" + uri = URI.parse("#{args.url}/panel/upgrade/module?module=announcement") + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = JSON.parse(response.body) rescue nil + if !data.nil? && data["success"] == true + puts "Importing categories...." + categories = data["categories"] + tags = data["tags"] + announcements = data["announcements"] + module_app = ModuleApp.find_by_key("announcement") + I18n.locale = :zh_tw + categories.each do |category| + c = Category.where(:title => category["title"]["zh_tw"], :module_app_id => module_app.id).first + c = Category.where(:title => category["title"]["en"], :module_app_id => module_app.id).first if c.nil? + if c.nil? + c = Category.new + c.title_translations = category["title"] + c.disable = category["disable"] + c.module_app_id = module_app.id + c.save + end + end + puts "Categories imported." + puts "Importing tags..." + tags.each do |tag| + t = module_app.tags.where(:name => tag["name"]["zh_tw"]).first + t = module_app.tags.where(:name => tag["name"]["en"]).first + if t.nil? + t = Tag.new + t.name_translations = tag["name"] + t.is_default = tag["is_default"] + t.save + end + end + puts "Importing tags completed." + puts "Importing announcements..." + announcements.each do |announcement| + bulletin = Bulletin.new + bulletin.email_sent = announcement["email_sent"] + bulletin.email_sentdate = announcement["email_sentdate"] + bulletin.other_mailaddress = announcement["other_mailaddress"] + bulletin.public = announcement["public"] + bulletin.subtitle_translations = announcement["subtitle"] + bulletin.text_translations = announcement["text"] + bulletin.title_translations = announcement["title"] + bulletin.remote_image_url = announcement["image"] + announcement["links"].each do |link| + l = BulletinLink.new + l.title_translations = link["title"] + l.url = link["url"] + l.bulletin = bulletin + l.save + end + announcement["files"].each do |file| + f = BulletinFile.new + f.title_translations = file["title"] + f.description_translations = file["description"] + f.remote_file_url = file["file"] + f.bulletin = bulletin + f.save + end + category = module_app.categories.where(:title => announcement["category"]["zh_tw"]).first + category = module_app.categories.where(:title => announcement["category"]["en"]).first if category.nil? + bulletin.category = category + announcement["tags"].each do |tag| + t = module_app.tags.where(:name => tag["name"]["zh_tw"]).first + t = module_app.tags.where(:name => tag["name"]["en"]).first if t.nil? + bulletin.tags << t + end + bulletin.approved = true + bulletin.save + end + puts "Announcements imported successfully." + else + puts "Import has some problem." + end + end +end \ No newline at end of file