From 93909eeca0eeb74606dc70618e6f823bf4a87990 Mon Sep 17 00:00:00 2001 From: saurabhbhatia Date: Tue, 10 Sep 2013 17:27:43 +0800 Subject: [PATCH] Get data from database Conflicts: app/controllers/admin/import_data_controller.rb Conflicts: app/controllers/admin/import_data_controller.rb app/views/admin/import_data/get_teacher_data.html.erb --- .../admin/import_data_controller.rb | 94 +++++++++++++++++++ .../import_data/get_teacher_data.html.erb | 1 + 2 files changed, 95 insertions(+) create mode 100644 app/controllers/admin/import_data_controller.rb create mode 100644 app/views/admin/import_data/get_teacher_data.html.erb diff --git a/app/controllers/admin/import_data_controller.rb b/app/controllers/admin/import_data_controller.rb new file mode 100644 index 000000000..bda2f9ed7 --- /dev/null +++ b/app/controllers/admin/import_data_controller.rb @@ -0,0 +1,94 @@ +# encoding: utf-8 + +require 'net/http' +require 'open-uri' + +class Admin::ImportDataController < OrbitBackendController + + def get_teacher_data + uri = URI("https://15fc4b.ngrok.com/teachers.xml") + #params = {"UnitName" => "管理學院".encode('big5-uao'), "account" => "manage"} + #uri.query = URI.encode_www_form(params) + res = Net::HTTP.get_response(uri) + + + @teacher_data = Hash.from_xml(res.body) + @teachers = @teacher_data["objects"] + @i = 2000 + @teachers.each do |hash| + @roles = Role.all + @teacher = User.new + I18n.locale = :zh_tw + if hash['teacher_zh_tw'].blank? + @teacher.first_name = hash['teacher_en'] + elsif hash['teacher_zh_tw'].blank? && hash['teacher_en'].blank? + @teacher.first_name = "Please Fill the Name" + else + @teacher.first_name = hash['teacher_zh_tw'] + end + I18n.locale = :en + if hash['teacher_en'].blank? + @teacher.first_name = hash['teacher_zh_tw'] + elsif hash['teacher_zh_tw'].blank? && hash['teacher_en'].blank? + @teacher.first_name = "Please Fill the Name" + else + @teacher.first_name = hash['teacher_en'] + end + + #@teacher.first_name = hash['teacher_en'] + @teacher.ntu_seq = hash['ntu_seq'] + @teacher.role_ids = ["#{@roles.first.id}"] + @teacher.password = "testpass" + @i += 10 + if hash['email'].blank? + @teacher.email = "#{@i}@ntu.edu.tw" + Rails.logger.info"@@@@@@@@@@@@@@@@@@@@@"+@teacher.email.inspect + else + @teacher.email = hash['email'] + end + @teacher.user_id = hash['teacher_en'] + @teacher.save! + end + end + # def get_book_data + # uri = URI("http://ann.cc.ntu.edu.tw/Achv/xmlTeacherData.asp") + # params = {"UnitName" => "管理學院".encode('big5-uao'), "account" => "manage"} + # uri.query = URI.encode_www_form(params) + # res = Net::HTTP.get_response(uri) + + # teachers = User.all + + # teachers.each do |hash| + # if hash.ntu_seq.present? + # ntu_seq = hash.ntu_seq + # books_xml = Nokogiri::XML( \ + # open("http://ann.cc.ntu.edu.tw/Achv/xmlBook.asp?Seq=#{ntu_seq}")) + # @books = books_xml.xpath("//Book").map do |book_node| + # { + # author: (book_node>"Authors").text, + # year: (book_node>"PublishYear").text, + # title: (book_node>"DocTitle").text, + # remarks: (book_node>"Remarks").text, + # publisher: (book_node>"Publisher").text, + # book_title: (book_node>"BookTitle").text + # } + # end + # if @books.present? + # @books.each do |b| + # @book = WritingBook.new + # @book.authors = b[:author] + # @book.paper_title = b[:title] + # @book.book_title = b[:book_title] + # @book.year = b[:year] + # @book.note = b[:remarks] + # @book.publisher = b[:publisher] + # @book.create_user_id = hash.id + # @book.save + # end + # else + # puts "No books by Teacher" + # end + # end + # end + #end +end diff --git a/app/views/admin/import_data/get_teacher_data.html.erb b/app/views/admin/import_data/get_teacher_data.html.erb new file mode 100644 index 000000000..980c42165 --- /dev/null +++ b/app/views/admin/import_data/get_teacher_data.html.erb @@ -0,0 +1 @@ +

Successfully Imported Teacher Data!

\ No newline at end of file