Browse Source

Added "PUT /people/:id"

Dmitry Yu Okunev 3 years ago
parent
commit
38f6b7deae

+ 3 - 0
app/assets/javascripts/passes.coffee

@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/

+ 3 - 0
app/assets/stylesheets/passes.scss

@@ -0,0 +1,3 @@
+// Place all the styles related to the Passes controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/

+ 45 - 0
app/controllers/passes_controller.rb

@@ -0,0 +1,45 @@
+class PassesController < ApplicationController
+	protect_from_forgery
+
+	before_action :common
+	before_action :determine_user
+
+	def common
+		@current_url = url_for(:only_path => false)
+	end
+
+	# deduplicate this with people_controller.rb
+	def determine_user
+
+		if session[:cas].respond_to?(:with_indifferent_access)
+			session[:cas] = session[:cas].with_indifferent_access
+		end
+		if session[:cas].nil? || session[:cas][:user].nil? #|| (session[:cas][:last_validated_at].blank? || session[:cas][:last_validated_at] < 15.minutes.ago)
+			redirect_to "https://login.mephi.ru/login?service=#{url_for(:only_path => false)}"
+		end
+
+		@user = session[:cas]
+
+		unless @user.nil?
+			@user["user"] = @user["user"].downcase
+
+			@allowed_users = [ 'dyokunev', 'nnromanov', 'aatruttse', 'avsolodovnikov', 'pvmakarov', 'kvmartinov', 'earoganov' ]
+
+			unless @allowed_users.include?(@user["user"])
+				render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+			end
+		end
+	end
+
+	def show
+		@pass = Pass.find(params[:id])
+
+		respond_to do |format|
+			format.html
+			format.json { render :json => @pass.to_json }
+			format.xml  { render :xml  => @pass.to_xml }
+			format.yaml { render :yaml => @pass.to_yaml }
+			format.jpeg { send_data @pass.GetPhoto, :type => 'image/jpeg', :disposition => 'inline' }
+		end
+	end
+end

+ 24 - 0
app/controllers/people_controller.rb

@@ -46,6 +46,11 @@ class PeopleController < ApplicationController
 
 	def show
 		@person = Person.find(params[:id])
+		if @person.PassIdIsManual or @person.PassId.nil?
+			@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е'), :patronymic => @person.Patronymic.gsub('Ё', 'Е').gsub('ё', 'е'))
+			@availablePasses = Pass.where(:lastname => @person.Lastname.gsub('Ё', 'Е').gsub('ё', 'е'), :firstname => @person.Firstname.gsub('Ё', 'Е').gsub('ё', 'е')) if @availablePasses.nil? or @availablePasses.size == 0
+			@availablePasses = Pass.find_by_sql('SELECT `passes`.* FROM `passes` LEFT JOIN `people` ON `passes`.`id` = `people`.`PassId` WHERE `people`.`id` IS NULL') if @availablePasses.nil? or @availablePasses.size == 0
+		end
 
 		respond_to do |format|
 			format.html
@@ -81,6 +86,15 @@ class PeopleController < ApplicationController
 				ofz_links[key] = Array.new if ofz_links[key].nil?
 				ofz_links[key] << row["UserID"]
 				file.write("#{key}\n")
+
+				pass = Pass.find_by_id ( row["UserID"].to_i )
+				if pass.nil?
+					pass = Pass.ParsePass(row)
+					pass.save
+				else
+					pass.UpdatePass(row)
+					pass.save if pass.changed?
+				end
 			end
 		}
 
@@ -151,4 +165,14 @@ class PeopleController < ApplicationController
 			end
 		end
 	end
+
+	def update
+		@person = Person.find(params[:id])
+		@person.PassIdIsManual = true
+		@person.PassId         = params["PassId"].to_i
+
+		@person.save
+
+		redirect_to "/people/#{ @person.id }"
+	end
 end

+ 2 - 0
app/helpers/passes_helper.rb

@@ -0,0 +1,2 @@
+module PassesHelper
+end

+ 25 - 0
app/models/pass.rb

@@ -0,0 +1,25 @@
+class Pass < ApplicationRecord
+	def self.ParsePass(json)
+		pass = Pass.new
+
+		pass.id = json["UserID"].to_i
+		pass.UpdatePass(json)
+
+		return pass
+	end
+
+	def UpdatePass(json)
+		self.Lastname    = json["Lastname"  ].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
+		self.Firstname   = json["Firstname" ].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip
+		self.Patronymic  = json["Patronymic"].delete(' ').delete('?').gsub('Ё', 'Е').gsub('ё', 'е').strip unless json["Patronymic"].nil?
+	end
+
+	def GetPhoto
+		url = URI.parse("https://cps.mephi.ru/imagelookup.php?UserID=#{self.id}&secret=#{Rails.application.config.cps_api_key}")
+		http = Net::HTTP.new(url.host, url.port)
+		http.use_ssl = true
+		reply = http.get(url.request_uri)
+
+		return reply.body
+	end
+end

+ 5 - 6
app/models/person.rb

@@ -23,12 +23,11 @@ class Person < ApplicationRecord
 		end
 	end
 
-	def GetPhoto
-		url = URI.parse("https://cps.mephi.ru/imagelookup.php?UserID=#{self.PassId}&secret=#{Rails.application.config.cps_api_key}")
-		http = Net::HTTP.new(url.host, url.port)
-		http.use_ssl = true
-		reply = http.get(url.request_uri)
+	def GetPass
+		return Pass.find( self.PassId )
+	end
 
-		return reply.body
+	def GetPhoto
+		return self.GetPass.GetPhoto
 	end
 end

+ 5 - 0
app/views/layouts/application.html.erb

@@ -7,6 +7,11 @@
 
 		<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
 		<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
+		<style>
+			table td {
+				border: 1px solid #aaa;
+			}
+		</style>
 	</head>
 
 	<body>

File diff suppressed because it is too large
+ 1 - 4
app/views/people/index.html.erb


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


+ 0 - 0
app/views/people/update.html.erb


+ 4 - 0
config/routes.rb

@@ -1,8 +1,12 @@
 Rails.application.routes.draw do
+
 	get 'people/index'
 	get 'people/download'
 
 	get 'people/:id', to: 'people#show'
+	put 'people/:id', to: 'people#update'
+
+	get 'passes/:id', to: 'passes#show'
 
 	root 'people#index'
 	# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

+ 11 - 0
db/migrate/20161107123537_create_passes.rb

@@ -0,0 +1,11 @@
+class CreatePasses < ActiveRecord::Migration[5.0]
+  def change
+    create_table :passes do |t|
+      t.string :Firstname
+      t.string :Lastname
+      t.string :Patronymic
+
+      t.timestamps
+    end
+  end
+end

+ 5 - 0
db/migrate/20161107163200_add_index_to_pass_id_in_people.rb

@@ -0,0 +1,5 @@
+class AddIndexToPassIdInPeople < ActiveRecord::Migration
+	def change
+		add_index :people, :PassId
+	end
+end

+ 10 - 1
db/schema.rb

@@ -10,7 +10,15 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20161103160230) do
+ActiveRecord::Schema.define(version: 20161107163200) 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
+  end
 
   create_table "people", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
     t.string   "Firstname"
@@ -22,6 +30,7 @@ ActiveRecord::Schema.define(version: 20161103160230) do
     t.datetime "created_at",     null: false
     t.datetime "updated_at",     null: false
     t.boolean  "PassIdIsManual"
+    t.index ["PassId"], name: "index_people_on_PassId", using: :btree
   end
 
   create_table "sessions", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|

+ 7 - 0
test/controllers/passes_controller_test.rb

@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class PassesControllerTest < ActionDispatch::IntegrationTest
+  # test "the truth" do
+  #   assert true
+  # end
+end

+ 11 - 0
test/fixtures/passes.yml

@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+  Firstname: MyString
+  Lastname: MyString
+  Patronymic: MyString
+
+two:
+  Firstname: MyString
+  Lastname: MyString
+  Patronymic: MyString

+ 7 - 0
test/models/pass_test.rb

@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class PassTest < ActiveSupport::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end