Browse Source

Migrated to https://api.cps.mephi.ru/

Dmitry Yu Okunev 2 years ago
parent
commit
515c6884fd

+ 2 - 0
.gitignore

@@ -25,3 +25,5 @@
 
 *.orig
 /3rdparty/approximate_regex/approximate_regex
+
+/public/assets/*

+ 38 - 5
app/controllers/passes_controller.rb

@@ -3,6 +3,7 @@ class PassesController < ApplicationController
 
 	def show
 		@pass = Pass.find(params[:id])
+		@availablePeople = Person.all
 
 		respond_to do |format|
 			format.html
@@ -13,15 +14,47 @@ class PassesController < ApplicationController
 		end
 	end
 
-	def index
-		@passes = Pass.all
+	def update
+		@pass = Pass.find(params[:pass_id])
+		@pass.PersonIsManual = true
+		@pass.person_id      = params[:id].to_i
+
+		@pass.save
+
+		redirect_to "/people/#{ @pass.person_id }"
+	end
+
+	def commonindex
+		@count_total  = @passes.size
+		@count_errors = 0
+		@passes.each do |pass|
+			@count_errors += 1 if pass.person_id.nil?
+		end
 
 		respond_to do |format|
 			format.html
-			format.json { render :json => @passes.to_json }
-			format.xml  { render :xml  => @passes.to_xml }
-			format.yaml { render :yaml => @passes.to_yaml }
+			format.json { render :json => @people.to_json }
+			format.xml  { render :xml  => @people.to_xml }
+			format.yaml { render :yaml => @people.to_yaml }
+		end
+	end
+
+	def notset
+		@passes = Pass.where('`passes`.`person_id` IS NULL')
+		self.commonindex
+	end
+
+	def index
+		if params[:lastname].nil?
+			@passes = Pass.where(:id => -1)
+			return self.commonindex
+		end
+		if params[:lastname].nil? or params[:lastname] == ""
+			@passes = Pass.preload(:person).all
+		else
+			@passes = Pass.preload(:person).where(:lastname => params[:lastname])
 		end
+		self.commonindex
 	end
 
 	def update

+ 24 - 16
app/controllers/people_controller.rb

@@ -16,13 +16,18 @@ class PeopleController < ApplicationController
 		end
 	end
 
-	def unset
+	def notset
 		@people = Person.left_outer_joins(:passes).distinct.where('`passes`.`person_id` IS NULL')
 
 		self.commonindex
 	end
 
 	def index
+		if params[:lastname].nil?
+			@people = Person.where(:id => -1)
+			return self.commonindex
+		end
+
 		if params[:lastname].nil? or params[:lastname] == ""
 			@people = Person.preload(:passes).all
 		else
@@ -86,15 +91,7 @@ class PeopleController < ApplicationController
 		redirect_to "/people/#{ @pass.person_id }"
 	end
 
-	def download
-		dbparam = ''
-		unless params[:db].blank?
-			dbparam="&db=#{params[:db]}"
-		end
-
-		require 'net/http'
-
-		url_str = "https://cps.mephi.ru/?cmd=fixok_people&apiKey=#{Rails.application.config.cps_api_key}#{dbparam}"
+	def get_json_data_by_url(url_str)
 		#puts "#{url_str}"
 		url = URI.parse(url_str)
 		http = Net::HTTP.new(url.host, url.port)
@@ -102,15 +99,26 @@ class PeopleController < ApplicationController
 		reply = http.get(url.request_uri)
 
 		if reply.body.blank?
-			return
+			return nil
 		end
 
-		json = ActiveSupport::JSON.decode(reply.body)
-		#puts "#{json.to_yaml}"
+		return ActiveSupport::JSON.decode(reply.body)
+	end
+
+	def download
+		require 'net/http'
+
+		people_jraw = get_json_data_by_url "https://api.cps.mephi.ru/people?prepareFormulars=true&apiKey=#{Rails.application.config.cps_api_key}"
+		return if people_jraw.nil?
+
+		passes_jraw = get_json_data_by_url "https://api.cps.mephi.ru/passes?apiKey=#{Rails.application.config.cps_api_key}"
+		return if passes_jraw.nil?
+
+		#puts "#{people_jraw.to_yaml}"
 
 		employees = Hash.new
-		json["employees"].each do |id, employee|
-			person = Person.find_by_id( employee["e_id"].to_i )
+		people_jraw["arg"].each do |employee|
+			person = Person.find_by_id( employee["EmpGUID"].to_i )
 			if person.nil?
 				person = Person.ParseEmployee(employee)
 				#puts "#{person.to_yaml}"
@@ -134,7 +142,7 @@ class PeopleController < ApplicationController
 		passes_raw = Hash.new
 		ofz_links  = Hash.new
 		File.open('/tmp/cps_keys.csv', 'w') { |file|
-			json["ofz_link_table"].each do |row|
+			passes_jraw["arg"].each do |row|
 				pass_id = row["UserID"].to_i
 
 				key="#{row["Lastname"]}#{row["Firstname"]}#{row["Patronymic"]}".delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip

+ 4 - 0
app/models/pass.rb

@@ -1,4 +1,6 @@
 class Pass < ApplicationRecord
+	belongs_to :person
+
 	def self.ParsePass(json)
 		pass = Pass.new
 
@@ -14,6 +16,8 @@ class Pass < ApplicationRecord
 		self.Firstname   = json["Firstname" ].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
 		self.Patronymic  = json["Patronymic"].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip unless json["Patronymic"].nil?
 		self.person_id   = json["EmpGUID"] unless self.PersonIsManual or json["EmpGUID"].blank?
+		self.photo_id    = json["PhotoId"].to_i
+		self.is_actual   = json['is_actual'].to_i
 	end
 
 	def GetPhoto

+ 7 - 5
app/models/person.rb

@@ -4,17 +4,19 @@ class Person < ApplicationRecord
 	def self.ParseEmployee(employee)
 		person = Person.new
 
-		person.id          = employee["e_id"].to_i
+		person.id          = employee["EmpGUID"].to_i
 		person.UpdateEmployee(employee)
 
 		return person
 	end
 
 	def UpdateEmployee(employee)
-		self.Lastname    = employee["lastname"].strip
-		self.Firstname   = employee["firstname"].strip
-		self.Patronymic  = employee["middlename"].strip unless employee["middlename"].nil?
-		self.DateOfBirth = employee["dob"]
+		self.Lastname    = employee["Lastname"].strip
+		self.Firstname   = employee["Firstname"].strip
+		self.Patronymic  = employee["Patronymic"].strip unless employee["Patronymic"].nil? || employee["Patronymic"].blank?
+		self.DateOfBirth = employee["BirthDay"]
+
+		self.is_actual = !employee["Formulars"].nil?
 
 		unless employee["WORKPOSITIONS"].nil?
 			self.Description = employee["WORKPOSITIONS"].collect{ |wp| "#{wp["subdivision"]["name"].strip} (#{wp["name"].strip})" }.uniq.join(", ").truncate(250)

+ 2 - 1
app/views/layouts/application.html.erb

@@ -29,7 +29,8 @@
 			<ul class='transition'>
 					<li><a data-no-turbolink='true' href='/people/index'>Полный список</a></li>
 					<li><a data-no-turbolink='true' href='/people/namesakes'>Полные тёзки</a></li>
-					<li><a data-no-turbolink='true' href='/people/unset'>Не обработанные</a></li>
+					<li><a data-no-turbolink='true' href='/people/notset'>Не обработ. записи ОК</a></li>
+					<li><a data-no-turbolink='true' href='/passes/index'>Записи ОФЗ</a></li>
 			</ul>
 		</div>
 

File diff suppressed because it is too large
+ 25 - 0
app/views/passes/index.html.erb


File diff suppressed because it is too large
+ 24 - 0
app/views/passes/notset.html.erb


File diff suppressed because it is too large
+ 71 - 0
app/views/passes/show.html.erb


app/views/people/unset.html.erb → app/views/people/notset.html.erb


+ 4 - 2
config/routes.rb

@@ -4,16 +4,18 @@ Rails.application.routes.draw do
 	get 'people/download'
 	get 'people/downloadlong'
 	get 'people/namesakes'
-	get 'people/unset'
+	get 'people/notset'
 
 	put 'people/:id', to: 'people#update'
 	get 'people/:id', to: 'people#show'
 
 	get 'passes/index'
+	get 'passes/notset'
+	put 'passes/:id', to: 'passes#update'
 	get 'passes/:id', to: 'passes#show'
 
 	#put 'passes/:id', to: 'passes#update'
 
-	root 'people#unset'
+	root 'people#notset'
 	# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
 end

+ 6 - 0
db/migrate/20171003150746_add_is_actual_to_people.rb

@@ -0,0 +1,6 @@
+class AddIsActualToPeople < ActiveRecord::Migration[5.0]
+  def change
+    add_column :people, :is_actual, :boolean
+    add_index :people, :is_actual
+  end
+end

+ 6 - 0
db/migrate/20171003150754_add_is_actual_to_passes.rb

@@ -0,0 +1,6 @@
+class AddIsActualToPasses < ActiveRecord::Migration[5.0]
+  def change
+    add_column :passes, :is_actual, :boolean
+    add_index :passes, :is_actual
+  end
+end

+ 6 - 0
db/migrate/20171003151446_add_photo_id_to_passes.rb

@@ -0,0 +1,6 @@
+class AddPhotoIdToPasses < ActiveRecord::Migration[5.0]
+  def change
+    add_column :passes, :photo_id, :integer
+    add_index :passes, :photo_id
+  end
+end

+ 18 - 1
db/schema.rb

@@ -10,12 +10,27 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20161108080343) do
+ActiveRecord::Schema.define(version: 20171003151446) do
 
   create_table "passes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
     t.string   "Firstname"
     t.string   "Lastname"
     t.string   "Patronymic"
+    t.datetime "created_at",                     null: false
+    t.datetime "updated_at",                     null: false
+    t.integer  "person_id"
+    t.boolean  "PersonIsManual", default: false, null: false
+    t.boolean  "is_actual"
+    t.integer  "photo_id"
+    t.index ["is_actual"], name: "index_passes_on_is_actual", using: :btree
+    t.index ["person_id"], name: "index_passes_on_person_id", using: :btree
+    t.index ["photo_id"], name: "index_passes_on_photo_id", using: :btree
+  end
+
+  create_table "passes_tmp", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
+    t.string   "Firstname"
+    t.string   "Lastname"
+    t.string   "Patronymic"
     t.datetime "created_at",     null: false
     t.datetime "updated_at",     null: false
     t.integer  "person_id"
@@ -31,6 +46,8 @@ ActiveRecord::Schema.define(version: 20161108080343) do
     t.date     "DateOfBirth"
     t.datetime "created_at",  null: false
     t.datetime "updated_at",  null: false
+    t.boolean  "is_actual"
+    t.index ["is_actual"], name: "index_people_on_is_actual", using: :btree
   end
 
   create_table "sessions", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|