-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacct.py
229 lines (201 loc) · 8.23 KB
/
acct.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import math
import re
from req import RequestHandler
from req import reqenv
from user import UserService
from user import UserConst
from pro import ProService
from chal import ChalService
from pack import PackService
from req import Service
from log import LogService
class AcctHandler(RequestHandler):
@reqenv
def get(self,acct_id):
acct_id = int(acct_id)
err,acct = yield from UserService.inst.info_acct(acct_id)
if err:
self.finish(err)
return
cur = yield self.db.cursor()
yield cur.execute(('SELECT '
'SUM("test_valid_rate"."rate" * '
' CASE WHEN "valid_test"."timestamp" < "valid_test"."expire" '
' THEN 1 ELSE '
' (1 - (GREATEST(date_part(\'days\',justify_interval('
' age("valid_test"."timestamp","valid_test"."expire") '
' + \'1 days\')),-1)) * 0.15) '
' END) '
'AS "rate" FROM "test_valid_rate" '
'INNER JOIN ('
' SELECT "test"."pro_id","test"."test_idx",'
' MIN("test"."timestamp") AS "timestamp","problem"."expire" '
' FROM "test" '
' INNER JOIN "account" '
' ON "test"."acct_id" = "account"."acct_id" '
' INNER JOIN "problem" '
' ON "test"."pro_id" = "problem"."pro_id" '
' WHERE "account"."acct_id" = %s '
' AND "test"."state" = %s '
' AND "account"."class" && "problem"."class" '
' GROUP BY "test"."pro_id","test"."test_idx","problem"."expire"'
') AS "valid_test" '
'ON "test_valid_rate"."pro_id" = "valid_test"."pro_id" '
'AND "test_valid_rate"."test_idx" = "valid_test"."test_idx";'),
(acct_id,ChalService.STATE_AC))
if cur.rowcount != 1:
self.finish('Unknown')
return
rate = cur.fetchone()[0]
if rate == None:
rate = 0
extrate = 0
if acct['class'] == 0:
cur = yield self.db.cursor()
yield cur.execute(('SELECT '
'SUM("test_valid_rate"."rate") '
'AS "rate" FROM "test_valid_rate" '
'INNER JOIN ('
' SELECT "test"."pro_id","test"."test_idx" '
' FROM "test" '
' INNER JOIN "problem" '
' ON "test"."pro_id" = "problem"."pro_id" '
' WHERE "test"."acct_id" = %s '
' AND "test"."state" = %s '
' AND %s && "problem"."class" '
' GROUP BY "test"."pro_id","test"."test_idx"'
') AS "valid_test" '
'ON "test_valid_rate"."pro_id" = "valid_test"."pro_id" '
'AND "test_valid_rate"."test_idx" = "valid_test"."test_idx";'),
(acct_id,ChalService.STATE_AC,[2]))
if cur.rowcount != 1:
self.finish('Unknown')
return
extrate = cur.fetchone()[0]
if extrate == None:
extrate = 0
'''
yield cur.execute(('SELECT '
'"pro_rank"."pro_id",'
'(0.3 * power(0.66,("pro_rank"."rank" - 1))) AS "weight" FROM ('
' SELECT '
' "challenge"."pro_id","challenge"."acct_id",'
' row_number() OVER ('
' PARTITION BY "challenge"."pro_id" ORDER BY MIN('
' "challenge"."chal_id") ASC) AS "rank" '
' FROM "challenge" '
' INNER JOIN ('
' SELECT "pro_id" FROM "challenge" '
' WHERE "challenge"."acct_id" = %s'
' ) AS need_id ON "challenge"."pro_id" = "need_id"."pro_id" '
' INNER JOIN "challenge_state" '
' ON "challenge"."chal_id" = "challenge_state"."chal_id" '
' INNER JOIN "problem" ON '
' "challenge"."pro_id" = "problem"."pro_id" '
' INNER JOIN "account" '
' ON "challenge"."acct_id" = "account"."acct_id" '
' WHERE "challenge_state"."state" = %s '
' AND "problem"."class" && "account"."class" '
' GROUP BY "challenge"."pro_id","challenge"."acct_id"'
') AS "pro_rank" WHERE "pro_rank"."acct_id" = %s;'),
(acct['acct_id'],ChalService.STATE_AC,acct['acct_id']))
weightmap = {}
for pro_id,weight in cur:
weightmap[pro_id] = float(weight)
bonus = 0
for pro in prolist:
pro_id = pro['pro_id']
if pro_id in weightmap:
bonus += pro['rate'] * weightmap[pro_id]
'''
err,prolist = yield from Service.Pro.list_pro(acct = self.acct,clas = None)
err,ratemap = yield from Service.Rate.map_rate(clas = None)
prolist2 = []
acct_id = acct['acct_id']
for pro in prolist:
pro_id = pro['pro_id']
tmp = {'pro_id':pro_id,'score':-1}
if acct_id in ratemap and pro_id in ratemap[acct_id]:
rate2 = ratemap[acct_id][pro_id]
tmp['score'] = rate2['rate']
prolist2.append(tmp)
isadmin = (self.acct['acct_type'] == UserConst.ACCTTYPE_KERNEL)
# force https, add by xiplus, 2018.08.24
acct['photo'] = re.sub(r'^http://', 'https://', acct['photo'])
acct['cover'] = re.sub(r'^http://', 'https://', acct['cover'])
self.render('acct',
acct = acct,
rate = math.floor(rate),
extrate = math.floor(extrate),
prolist = prolist2,
isadmin = isadmin)
return
@reqenv
def post(self):
reqtype = self.get_argument('reqtype')
if reqtype == 'profile':
name = self.get_argument('name')
photo = self.get_argument('photo')
cover = self.get_argument('cover')
err,ret = yield from UserService.inst.update_acct(
self.acct['acct_id'],
self.acct['acct_type'],
self.acct['class'],
name,
photo,
cover)
if err:
self.finish(err)
return
self.finish('S')
return
elif reqtype == 'reset':
old = self.get_argument('old')
pw = self.get_argument('pw')
acct_id = self.get_argument('acct_id')
if acct_id != self.acct['acct_id']:
yield from LogService.inst.add_log((self.acct['name']+" was changing the password of user #"+str(acct_id)+"."))
err,ret = yield from UserService.inst.update_pw(
acct_id,old,pw,(self.acct['acct_type'] == UserConst.ACCTTYPE_KERNEL))
if err:
self.finish(err)
return
self.finish('S')
return
self.finish('Eunk')
return
class SignHandler(RequestHandler):
@reqenv
def get(self):
self.render('sign')
return
@reqenv
def post(self):
reqtype = self.get_argument('reqtype')
if reqtype == 'signin':
mail = self.get_argument('mail')
pw = self.get_argument('pw')
err,sign = yield from UserService.inst.sign_in(mail,pw)
if err:
self.finish(err)
return
self.set_secure_cookie('sign',sign,
path = '/oj',httponly = True)
self.finish('S')
return
elif reqtype == 'signup':
mail = self.get_argument('mail')
pw = self.get_argument('pw')
name = self.get_argument('name')
err,sign = yield from UserService.inst.sign_up(mail,pw,name)
if err:
self.finish(err)
return
self.set_secure_cookie('sign',sign,
path = '/oj',httponly = True)
self.finish('S')
return
elif reqtype == 'signout':
self.clear_cookie('sign',path = '/oj')
self.finish('S')
return