From b1fe5264ce2503eec6e5cce17f7b08da0846b803 Mon Sep 17 00:00:00 2001 From: manson Date: Tue, 3 Jun 2014 19:27:34 +0800 Subject: [PATCH] Import Archive from RSS2 and basic Import UI --- app/controllers/admin/import_controller.rb | 98 ++++++++++++++++++---- app/views/admin/import/index.html.erb | 31 +++++++ built_in_extensions.rb | 2 +- config/routes.rb | 4 +- 4 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 app/views/admin/import/index.html.erb diff --git a/app/controllers/admin/import_controller.rb b/app/controllers/admin/import_controller.rb index c832a03..96fc3c6 100644 --- a/app/controllers/admin/import_controller.rb +++ b/app/controllers/admin/import_controller.rb @@ -4,28 +4,15 @@ require "uri" require 'json' class Admin::ImportController < OrbitAdminController + before_action :get_data ,:except=>'index' def rss2_news - @url = params['url'] - uri = URI.parse(@url) - @host = uri.host - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - data = response.body - data = JSON.parse(data) - @categories = data['categories'] - @data = data['data'] - @xxx = [] @new_category_ids = {} - l = I18n.locale - I18n.locale = :zh_tw module_id = ModuleApp.where(:key=>"announcement").first.id @categories.each do |category| x = Category.where(:title => category["zh_tw"]).first if x.nil? - @xxx << category cat = Category.new titles = {} @site_valid_locales.each do |locale| @@ -40,8 +27,6 @@ class Admin::ImportController < OrbitAdminController end end - @user = User.where(:user_name => "rulingcom").first - @data.each do |row| bulletin = Bulletin.new bulletin.update_user = @user @@ -118,8 +103,87 @@ class Admin::ImportController < OrbitAdminController bulletin.save! end - I18n.locale = l + I18n.locale = @org_locale redirect_to('/admin/announcements') end + + def rss2_archive + unless @site_url.nil? + module_app = ModuleApp.where(:key=>"archive").first + + @new_categories = {} + @categories.each do |id, category| + old_cate = Category.where(:title => category['title']["zh-tw"]).first + if old_cate.nil? + cate = Category.new + titles = {} + @site_valid_locales.each do |locale| + titles[locale] = category['title'][locale.to_s.gsub('_', '-')] + end + cate.title_translations = titles + cate.save! + module_app.categories << cate + @new_categories[id] = cate.id + else + @new_categories[id] = old_cate.id + end + end + I18n.locale = :en + + @data.each do |id, file| + titles = {} + @site_valid_locales.each do |locale| + titles[locale] = file['title'][locale.to_s.gsub('_', '-')] + end + + the_file = ArchiveFileMultiple.new({ + :file_title_translations=>titles, + :choose_lang => @site_valid_locales, + :remote_file_url => @site_url+file['url'], + :sort_number=>file['sort_number'] + }) + the_file.save + + archive_file = ArchiveFile.new({ + :title_translations=>titles, + :sort_number=>file['sort_number'] + }) + archive_file.category = Category.find(@new_categories[file["category"]]) + + archive_file.create_user_id = current_user.id + archive_file.update_user_id = current_user.id + + archive_file.save + archive_file.archive_file_multiples << the_file + end + end + + I18n.locale = @org_locale + + redirect_to admin_archive_files_path + end + + private + + def get_data + @org_locale = I18n.locale + I18n.locale = :zh_tw + + @site_url = params['site_url'] + @url = params['url'] + uri = URI.parse(@url) + + @host = uri.host + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Get.new(uri.request_uri) + response = http.request(request) + data = response.body + data = JSON.parse(data) + + @categories = data['categories'] + @data = data['data'] + + @user = User.where(:user_name => "rulingcom").first + end end \ No newline at end of file diff --git a/app/views/admin/import/index.html.erb b/app/views/admin/import/index.html.erb new file mode 100644 index 0000000..1dc20d3 --- /dev/null +++ b/app/views/admin/import/index.html.erb @@ -0,0 +1,31 @@ + + +
+
<%= t("announcement.announcement") %>
+
+
+ +
+
+ +
+
<%= t("archive.archive") %>
+
+
+
+ +
+
\ No newline at end of file diff --git a/built_in_extensions.rb b/built_in_extensions.rb index af8bf86..0e22570 100644 --- a/built_in_extensions.rb +++ b/built_in_extensions.rb @@ -1,6 +1,6 @@ gem 'announcement', git: 'git@gitlab.tp.rulingcom.com:saurabh/announcement-test.git' gem 'ad_banner', git: 'git@gitlab.tp.rulingcom.com:saurabh/adbanner-test.git' -gem "archive", git: 'git@gitlab.tp.rulingcom.com:saurabh/archive.git' +gem "archive", path: '/home/manson/dev/workspace/modules/archive' gem 'gallery', git: 'git@gitlab.tp.rulingcom.com:saurabh/gallery.git' gem 'links', git: 'git@gitlab.tp.rulingcom.com:saurabh/links.git' gem 'page_content', git: 'git@gitlab.tp.rulingcom.com:saurabh/pagecontent.git' diff --git a/config/routes.rb b/config/routes.rb index 07c1ae6..cc46666 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -196,7 +196,9 @@ Orbit::Application.routes.draw do get 'design_list' => 'designs#design_list' get 'module_store' => 'module_store#index' - get "import/rss2_news" => "import#rss2_news" + get "import" => "import#index" + post "import/rss2_news" => "import#rss2_news" + post "import/rss2_archive" => "import#rss2_archive" end get 'mobile', to: 'pages#home'