forked from PaulGuo/F2E.im
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
348 lines (270 loc) · 12.4 KB
/
Copy pathuser.py
File metadata and controls
348 lines (270 loc) · 12.4 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
# coding=utf-8
#
# Copyright 2012 F2E.im
# Do have a faith in what you're doing.
# Make your life a story worth telling.
import uuid
import hashlib
import Image
import StringIO
import time
import json
import re
import urllib2
import urllib
import tornado.web
import lib.jsonp
from base import *
from lib.sendmail import send
from lib.variables import gen_random
from lib.gravatar import Gravatar
from form.user import *
def do_login(self, user_id):
user_info = self.user_model.get_user_by_uid(user_id)
user_id = user_info["uid"]
self.session["uid"] = user_id
self.session["username"] = user_info["username"]
self.session["email"] = user_info["email"]
self.session["password"] = user_info["password"]
self.session.save()
self.set_secure_cookie("user", str(user_id))
def do_logout(self):
# destroy sessions
self.session["uid"] = None
self.session["username"] = None
self.session["email"] = None
self.session["password"] = None
self.session.save()
# destroy cookies
self.clear_cookie("user")
class HomeHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user_email = self.current_user['email']
self.write(user_email)
class SettingHandler(BaseHandler):
@tornado.web.authenticated
def get(self, template_variables = {}):
user_info = self.get_current_user()
template_variables["user_info"] = user_info
template_variables["gen_random"] = gen_random
self.render("user/setting.html", **template_variables)
@tornado.web.authenticated
def post(self, template_variables = {}):
template_variables = {}
# validate the fields
form = SettingForm(self)
if not form.validate():
self.get({"errors": form.errors})
return
# continue while validate succeed
user_info = self.current_user
update_result = self.user_model.set_user_base_info_by_uid(user_info["uid"], {
"nickname": form.nickname.data,
"signature": form.signature.data,
"location": form.location.data,
"website": form.website.data,
"company": form.company.data,
"github": form.github.data,
"twitter": form.twitter.data,
"douban": form.douban.data,
"self_intro": form.self_intro.data,
})
template_variables["success_message"] = [u"用户基本资料更新成功"]
# update `updated`
updated = self.user_model.set_user_base_info_by_uid(user_info["uid"], {"updated": time.strftime('%Y-%m-%d %H:%M:%S')})
self.get(template_variables)
class SettingAvatarHandler(BaseHandler):
@tornado.web.authenticated
def get(self, template_variables = {}):
user_info = self.get_current_user()
template_variables["user_info"] = user_info
template_variables["gen_random"] = gen_random
self.render("user/setting_avatar.html", **template_variables)
@tornado.web.authenticated
def post(self, template_variables = {}):
template_variables = {}
if(not "avatar" in self.request.files):
template_variables["errors"] = {}
template_variables["errors"]["invalid_avatar"] = [u"请先选择要上传的头像"]
self.get(template_variables)
return
user_info = self.current_user
user_id = user_info["uid"]
avatar_name = "%s" % uuid.uuid5(uuid.NAMESPACE_DNS, str(user_id))
avatar_raw = self.request.files["avatar"][0]["body"]
avatar_buffer = StringIO.StringIO(avatar_raw)
avatar = Image.open(avatar_buffer)
# crop avatar if it's not square
avatar_w, avatar_h = avatar.size
avatar_border = avatar_w if avatar_w < avatar_h else avatar_h
avatar_crop_region = (0, 0, avatar_border, avatar_border)
avatar = avatar.crop(avatar_crop_region)
avatar_96x96 = avatar.resize((96, 96), Image.ANTIALIAS)
avatar_48x48 = avatar.resize((48, 48), Image.ANTIALIAS)
avatar_32x32 = avatar.resize((32, 32), Image.ANTIALIAS)
avatar_96x96.save("./static/avatar/b_%s.png" % avatar_name, "PNG")
avatar_48x48.save("./static/avatar/m_%s.png" % avatar_name, "PNG")
avatar_32x32.save("./static/avatar/s_%s.png" % avatar_name, "PNG")
result = self.user_model.set_user_avatar_by_uid(user_id, "%s.png" % avatar_name)
template_variables["success_message"] = [u"用户头像更新成功"]
# update `updated`
updated = self.user_model.set_user_base_info_by_uid(user_id, {"updated": time.strftime('%Y-%m-%d %H:%M:%S')})
self.get(template_variables)
class SettingAvatarFromGravatarHandler(BaseHandler):
@tornado.web.authenticated
def get(self, template_variables = {}):
user_info = self.current_user
user_id = user_info["uid"]
avatar_name = "%s" % uuid.uuid5(uuid.NAMESPACE_DNS, str(user_id))
gravatar = Gravatar(user_info["email"])
avatar_96x96 = gravatar.get_image(size = 96, filetype_extension = False)
avatar_48x48 = gravatar.get_image(size = 48, filetype_extension = False)
avatar_32x32 = gravatar.get_image(size = 32, filetype_extension = False)
urllib.urlretrieve(avatar_96x96, "./static/avatar/b_%s.png" % avatar_name)
urllib.urlretrieve(avatar_48x48, "./static/avatar/m_%s.png" % avatar_name)
urllib.urlretrieve(avatar_32x32, "./static/avatar/s_%s.png" % avatar_name)
result = self.user_model.set_user_avatar_by_uid(user_id, "%s.png" % avatar_name)
template_variables["success_message"] = [u"用户头像更新成功"]
# update `updated`
updated = self.user_model.set_user_base_info_by_uid(user_id, {"updated": time.strftime('%Y-%m-%d %H:%M:%S')})
template_variables["user_info"] = user_info
template_variables["gen_random"] = gen_random
self.render("user/setting_avatar.html", **template_variables)
class SettingPasswordHandler(BaseHandler):
@tornado.web.authenticated
def get(self, template_variables = {}):
user_info = self.get_current_user()
template_variables["user_info"] = user_info
template_variables["gen_random"] = gen_random
self.render("user/setting_password.html", **template_variables)
@tornado.web.authenticated
def post(self, template_variables = {}):
template_variables = {}
# validate the fields
form = SettingPasswordForm(self)
if not form.validate():
self.get({"errors": form.errors})
return
# validate the password
user_info = self.current_user
user_id = user_info["uid"]
secure_password = hashlib.sha1(form.password_old.data).hexdigest()
secure_new_password = hashlib.sha1(form.password.data).hexdigest()
if(not user_info["password"] == secure_password):
template_variables["errors"] = {}
template_variables["errors"]["error_password"] = [u"当前密码输入有误"]
self.get(template_variables)
return
# continue while validate succeed
update_result = self.user_model.set_user_password_by_uid(user_id, secure_new_password)
template_variables["success_message"] = [u"您的用户密码已更新"]
# update `updated`
updated = self.user_model.set_user_base_info_by_uid(user_id, {"updated": time.strftime('%Y-%m-%d %H:%M:%S')})
self.get(template_variables)
class ForgotPasswordHandler(BaseHandler):
def get(self, template_variables = {}):
do_logout(self)
self.render("user/forgot_password.html", **template_variables)
def post(self, template_variables = {}):
template_variables = {}
# validate the fields
form = ForgotPasswordForm(self)
if not form.validate():
self.get({"errors": form.errors})
return
# validate the post value
user_info = self.user_model.get_user_by_email_and_username(form.email.data, form.username.data)
if(not user_info):
template_variables["errors"] = {}
template_variables["errors"]["invalid_email_or_username"] = [u"所填用户名和邮箱有误"]
self.get(template_variables)
return
# continue while validate succeed
# update password
new_password = uuid.uuid1().hex
new_secure_password = hashlib.sha1(new_password).hexdigest()
update_result = self.user_model.set_user_password_by_uid(user_info["uid"], new_secure_password)
# send password reset link to user
mail_title = u"前端社区(F2E.im)找回密码"
template_variables = {"email": form.email.data, "new_password": new_password};
template_variables["success_message"] = [u"新密码已发送至您的注册邮箱"]
mail_content = self.render_string("user/forgot_password_mail.html", **template_variables)
send(mail_title, mail_content, form.email.data)
self.get(template_variables)
class LoginHandler(BaseHandler):
def get(self, template_variables = {}):
do_logout(self)
self.render("user/login.html", **template_variables)
def post(self, template_variables = {}):
template_variables = {}
# validate the fields
form = LoginForm(self)
if not form.validate():
self.get({"errors": form.errors})
return
# continue while validate succeed
secure_password = hashlib.sha1(form.password.data).hexdigest()
secure_password_md5 = hashlib.md5(form.password.data).hexdigest()
user_info = self.user_model.get_user_by_email_and_password(form.email.data, secure_password)
user_info = user_info or self.user_model.get_user_by_email_and_password(form.email.data, secure_password_md5)
if(user_info):
do_login(self, user_info["uid"])
# update `last_login`
updated = self.user_model.set_user_base_info_by_uid(user_info["uid"], {"last_login": time.strftime('%Y-%m-%d %H:%M:%S')})
self.redirect(self.get_argument("next", "/"))
return
template_variables["errors"] = {"invalid_email_or_password": [u"邮箱或者密码不正确"]}
self.get(template_variables)
class LogoutHandler(BaseHandler):
def get(self):
do_logout(self)
# redirect
self.redirect(self.get_argument("next", "/"))
class RegisterHandler(BaseHandler):
def get(self, template_variables = {}):
do_logout(self)
self.render("user/register.html", **template_variables)
def post(self, template_variables = {}):
template_variables = {}
# validate the fields
form = RegisterForm(self)
if not form.validate():
self.get({"errors": form.errors})
return
# validate duplicated
duplicated_email = self.user_model.get_user_by_email(form.email.data)
duplicated_username = self.user_model.get_user_by_username(form.username.data)
if(duplicated_email or duplicated_username):
template_variables["errors"] = {}
if(duplicated_email):
template_variables["errors"]["duplicated_email"] = [u"所填邮箱已经被注册过"]
if(duplicated_username):
template_variables["errors"]["duplicated_username"] = [u"所填用户名已经被注册过"]
self.get(template_variables)
return
# validate reserved
if(form.username.data in self.settings.get("reserved")):
template_variables["errors"] = {}
template_variables["errors"]["reserved_username"] = [u"用户名被保留不可用"]
self.get(template_variables)
return
# continue while validate succeed
secure_password = hashlib.sha1(form.password.data).hexdigest()
user_info = {
"email": form.email.data,
"password": secure_password,
"username": form.username.data,
"created": time.strftime('%Y-%m-%d %H:%M:%S')
}
if(self.current_user):
return
user_id = self.user_model.add_new_user(user_info)
if(user_id):
do_login(self, user_id)
# send register success mail to user
mail_title = u"前端社区(F2E.im)注册成功通知"
mail_content = self.render_string("user/register_mail.html")
send(mail_title, mail_content, form.email.data)
self.redirect(self.get_argument("next", "/"))