forked from justdoit0823/ueue-server
/
useraction.py
300 lines (247 loc) · 10.1 KB
/
useraction.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
'''
This module mainly handles user basic actions.
Details of request are login,logout,sign up,init,active...
'''
'''
status for the user:
0 ---- unactive
1 ---- locked
2 ---- uninit
3 ---- normal
4 ---- infoset
5 ---- unauthenticate
6 ---- authenticate
'''
from __init__ import BaseHandler, send, USER_STATUS, set_image_size
import time
import random
import sys
import os
from hashlib import sha224, md5
import tornado.web
from tornado.options import define, options
define("noreply_account", default="noreply@ueue.cc",
help="signup email check account")
class UserLoginHandler(BaseHandler):
def get(self):
self.render("register1.0beta/login.html")
def post(self):
email = self.get_argument("email")
psw = self.get_argument("password")
psw = sha224(psw).hexdigest()
print psw
go = self.get_argument("next", "/")
result = self.db.get(("select uid,account,status from user where "
"email='%s' and password='%s'") % (email, psw))
print result
json = {}
if result:
if(result.status > USER_STATUS["unactive"]):
if(result.status == USER_STATUS["lock"]):
json = dict(error=1, msg='你的帐号以被暂停使用,请联系客服', url=go)
else:
self.set_secure_cookie("_yoez_uid",
str(result.uid), expires_days=7)
json = dict(error=0, msg='', url=go)
else:
tip = "请完成邮箱激活"
json = dict(error=1, msg=tip, url='/user/action/login')
else:
tip = "用户名或密码错误"
json = dict(error=1, msg=tip, url='/user/action/login')
self.write(json)
class UserLogoutHandler(BaseHandler):
def get(self):
cuser = self.get_current_user()
self.clear_current_user()
go = self.get_argument("next", "/")
self.redirect(go)
class UserSignupHandler(BaseHandler):
def get(self):
self.render("register1.0beta/register-1.html")
def post(self):
act = self.get_argument("nick")
mail = self.get_argument("email")
psw = self.get_argument("password")
print psw
psw = sha224(psw).hexdigest()
num = random.randint(1, 10)
img = "/static/img/common/avt"+str(num)+".jpg"
reg_time = time.strftime("%Y-%m-%d %X", time.localtime())
uid = 0
while(1):
uid = random.randint(1000000, 10000000)
chk_sql = "select * from user where uid=%d" % uid
rt = self.db.get(chk_sql)
if not rt:
break
code = md5(str(uid)+reg_time).hexdigest()
sql = ("insert into user(uid,account,email,password,img,"
"time,code) values ('%d','%s','%s','%s','%s','%s',"
"'%s')") % (uid, act, mail, psw, img, reg_time, code)
msg = self.render_string("register1.0beta/config_email.htm",
act=act, co=code)
sub = "请完成账号激活"
send(options.noreply_account, mail, sub, msg)
self.db.execute(sql)
left, right = mail.split('@')
rlft, rght = right.split('.')
if rlft == "gmail":
right = "google."+rght
mailpath = "mail."+right
#self.set_secure_cookie("_yoez_uid",str(uid),expires_days=7)
self.render('register1.0beta/register-2.html', actname=act,
mailpath=mailpath, code=code)
class UserResendmailHandler(BaseHandler):
def get(self):
code = self.get_argument('code')
user_sql = "select * from user where code='%s'" % code
rt = self.db.get(user_sql)
if not rt:
msg = 'send error!pleae contact Customer Services.'
kwargs = dict(status=0, msg=msg)
return self.write()
msg = self.render_string("register1.0beta/config_email.htm",
act=rt.account, co=code)
sub = "请完成账号激活"
send(options.noreply_account, rt.email, sub, msg)
self.write(dict(status=1, msg='go to email and for your active.'))
class UserActiveHandler(BaseHandler):
#@tornado.web.authenticated
def get(self):
#cuid=int(self.get_secure_cookie("_yoez_uid"))
code = self.get_argument("code")
check_sql = "select * from user where code='%s'" % code
result = self.db.get(check_sql)
if(result):
if result.status == USER_STATUS["unactive"]:
self.set_secure_cookie("_yoez_uid", str(result.uid), 7)
active_sql = ("update user set status=%d where "
"uid='%d'") % (USER_STATUS["uninit"], result.uid)
self.db.execute(active_sql)
print sys.argv[0]
print sys.argv
workdir = os.path.dirname(sys.argv[0])
path = "%s/static/img/user/%d" % (workdir, result.uid)
print os.path.dirname(sys.argv[0])
print path
os.mkdir(path)
self.render("register1.0beta/register-3.html")
else:
self.write(('''你的账户已经激活,请<a href="/user/action/login">'''
'''点此登陆</a>'''))
else:
self.write(('''验证错误,请检查地址有错误没。如果还是不行,请<a href='''
'''"mailto:shareyou.net.cn@gmail.com">联系我们</a>,'''
'''我们会为你解决的。'''))
class UserConfirmHandler(BaseHandler):
def post(self):
data = self.get_argument("user_d", "")
tp = self.get_argument("type", "")
sql = "SELECT * from user where %s='%s'" % (tp, data)
result = self.db.get(sql)
if result:
if tp == "email":
tip = "该邮箱已注册,请换一个!"
else:
tip = "该用户名已存在,请换一个!"
else:
if tp == "email":
tip = "你可以使用该邮箱注册"
else:
tip = "你可以使用该用户名"
self.write(tip)
class UserInitializeHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
cuser = self.get_current_user()
if(cuser.status >= USER_STATUS["normal"]):
return self.redirect("/user/set-basic")
self.render("user1.0beta/user-beginning-1.html", cuser=cuser)
def post(self):
cuid = int(self.get_secure_cookie("_yoez_uid"))
usertype = self.get_argument("type", "0")
sex = self.get_argument("sex", "0")
img = self.get_argument("avatar")
setrst = set_image_size((200, 200), img)
upd_sql = ("update user set img='%s',status=%d where "
"uid=%d") % (img, USER_STATUS["normal"], cuid)
chk_sql = "select proper_id from property where proper_id=%d" % cuid
chk = self.db.get(chk_sql)
if not chk:
pro_sql = ("insert into property(proper_id,ptype,sex) values "
"(%d,'%s','%s')") % (cuid, usertype, sex)
bsc_sql = "insert into basicinfo(bsc_id) values(%d)" % (cuid)
con_sql = "insert into contactinfo(con_id) values(%d)" % (cuid)
self.db.execute(pro_sql)
self.db.execute(bsc_sql)
self.db.execute(con_sql)
if setrst:
self.db.execute(upd_sql)
result = dict(url="/"+str(cuid), status=1, code='')
else:
result = dict(url="/", status=0, code='set image error!')
self.write(result)
# SUPPORT_ACTIONS = dict(follow=follow, cancel=cancel)
class UserFollowHandler(BaseHandler):
def follow(self, flwid):
cuid = int(self.get_secure_cookie("_yoez_uid"))
chksql = "select * from follow where flwid=%d" % (flwid)
result = self.db.get(chksql)
if result:
if int(result.relation):
print "repeat follow"
else:
updsql = ("update follow set relation='1' where fid=%d "
"and flwid=%d") % (cuid, flwid)
self.db.execute(updsql)
else:
addsql = ("insert into follow(fid,flwid,relation) values "
"(%d,%d,'1')") % (cuid, flwid)
self.db.execute(addsql)
def cancel(self, flwid):
cuid = int(self.get_secure_cookie("_yoez_uid"))
chksql = "select * from follow where flwid=%d" % flwid
result = self.db.get(chksql)
if result:
if int(result.relation):
updsql = ("update follow set relation='0' where fid=%d "
"and flwid=%d") % (cuid, flwid)
self.db.execute(updsql)
else:
print "cancel error!"
else:
print "cancel error!"
def post(self):
action = self.get_argument("action", None)
flwid = int(self.get_argument("flwid", None))
result = {}
if not action:
result = dict(status=0, msg="unknow action!")
return self.write(result)
if not flwid:
result = dict(status=0, msg="argument action!")
return self.write(result)
UserFollowHandler.SUPPORT_ACTIONS[action](self, flwid)
result = dict(status=1, msg="action success!")
self.write(result)
class UserPostHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("editor1.0beta/editor-loading.html")
class UserAuthbindHandler(BaseHandler):
def get(self):
self.render("register1.0beta/register-4.html")
HandlerList = [
(r"/user/action/login", UserLoginHandler),
(r"/user/action/logout", UserLogoutHandler),
(r"/user/action/signup", UserSignupHandler),
(r"/user/action/resendmail", UserResendmailHandler),
(r"/user/account/active", UserActiveHandler),
(r"/user/action/confirm", UserConfirmHandler),
(r"/user/action/init", UserInitializeHandler),
(r"/user/action/follow", UserFollowHandler),
(r"/user/action/post", UserPostHandler),
(r"/user/action/authbind", UserAuthbindHandler),
]