-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
152 lines (138 loc) · 3.14 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# encoding: utf-8
require_relative 'db'
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'haml'
require 'rack/csrf'
require 'json'
require 'sinatra/cookies'
require 'sinatra/json'
require 'redis'
configure do
use Rack::Session::Cookie, :secret => "xfpSs949xpMQVmKC6mSQrWKP"
use Rack::Csrf, :raise => true
end
helpers do
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
def check_login
unless cookies[:sid]
redirect '/login/'
end
school = School.first(:id => cookies[:sid])
unless school
redirect '/login/'
end
return school
end
end
get '/' do
@title = "选学校"
@schools = School.all
haml :index
end
get '/login/?' do
@title = "登录"
haml :login
end
post '/login/?' do
@errors = []
unless params[:username].length > 0
@errors.push("请填写用户名")
end
unless params[:password].length > 0
@errors.push("请填写密码")
end
if @errors.length == 0
school = School.first(:username => params[:username])
unless school
@errors.push "无效的用户名或密码"
else
if school.password == params[:password]
cookies[:sid] = school.id
redirect '/teachers/'
else
@errors.push "无效的用户名或密码"
end
end
end
@title = "登录"
haml :login
end
get '/teachers/?' do
@title = "选教师"
school = check_login
teas = school.teachers
@teachers = {}
teas.each do |tea|
unless @teachers.keys.include? tea.subject
@teachers[tea.subject] = Array.new
end
@teachers[tea.subject] << tea
end
haml :teachers
end
get '/vote/:tid/?' do
@title = "投票"
school = check_login
@teacher = Teacher.get(params[:tid])
unless school.teachers.include? @teacher
redirect '/login/'
end
@questions = Question.all
haml :vote
end
post '/vote/?' do
school = check_login
teacher = Teacher.get(params[:tid])
unless school.teachers.include? teacher
redirect '/login/'
end
redis = Redis.new(:port => 7772)
if (redis.sismember "t#{teacher.id}ips", request.ip)
redirect '/err/'
else
redis.multi do
redis.sadd "t#{teacher.id}ips", request.ip
redis.sadd "t#{teacher.id}votes", {ip: request.ip, raw: params.to_json}
questions = Question.all
questions.each do |quest|
if params[quest.id.to_s] == "yes"
redis.incr "t#{teacher.id}q#{quest.id}"
end
end
end
end
redirect '/thx/'
end
get '/thx/?' do
@title = "谢谢"
haml :thx
end
get '/err/?' do
@title = "错误"
haml :err
end
get '/dash/?' do
if params[:token] == "gGQtQbfMbnrdcUJknAgAHRmZeDAMJYXNF9xFQwG6vCYcWHzbHxCJqAd7dWEv2xegnDHZ5PSQ"
redis = Redis.new(:port => 7772)
@qids = Question.all.map { |e| e.id }
@tinfos = []
schools = School.all
schools.each do |sch|
sch.teachers.each do |t|
tea_info = {count: redis.scard("t#{t.id}votes"), name: t.name, subject: t.subject, school: t.school}
@qids.each do |qid|
tea_info[qid] = redis.get "t#{t.id}q#{qid}"
unless tea_info[qid]
tea_info[qid] = 0
end
end
@tinfos.push tea_info
end
end
haml :dash
end
end