forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.py
828 lines (807 loc) · 37.7 KB
/
member.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#!/usr/bin/env python
# coding=utf-8
import os
import base64
import re
import time
import datetime
import hashlib
import httplib
import string
import pickle
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.api import images
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from v2ex.babel import Member
from v2ex.babel import Avatar
from v2ex.babel import Counter
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.l10n import *
from v2ex.babel.ext.cookies import Cookies
from v2ex.babel.ext.sessions import Session
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.handlers import BaseHandler
import config
template.register_template_library('v2ex.templatetags.filters')
class MemberHandler(webapp.RequestHandler):
def get(self, member_username):
site = GetSite()
browser = detect(self.request)
self.session = Session()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
template_values['member'] = member
template_values['show_extra_options'] = False
if member:
if member.num == 1:
template_values['show_extra_options'] = True
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
one = False
one = GetMemberByUsername(member_username)
if one is not False:
if one.followers_count is None:
one.followers_count = 0
template_values['one'] = one
template_values['page_title'] = site.title + u' › ' + one.username
template_values['canonical'] = 'http://' + site.domain + '/member/' + one.username
if one is not False:
member_blog = memcache.get('member::' + str(one.num) + '::blog')
if member_blog == None:
blog = db.GqlQuery("SELECT * FROM Topic WHERE node_name = :1 AND member_num = :2 ORDER BY created DESC LIMIT 1", 'blog', one.num)
if blog.count() > 0:
template_values['blog'] = blog[0]
memcache.set('member::' + str(one.num) + '::blog', blog[0], 7200)
else:
template_values['blog'] = member_blog
member_topics = memcache.get('member::' + str(one.num) + '::topics')
if member_topics != None:
template_values['topics'] = member_topics
else:
q2 = db.GqlQuery("SELECT * FROM Topic WHERE member_num = :1 ORDER BY created DESC LIMIT 10", one.num)
template_values['topics'] = q2
memcache.set('member::' + str(one.num) + '::topics', q2, 7200)
replies = memcache.get('member::' + str(one.num) + '::participated')
if replies is None:
q3 = db.GqlQuery("SELECT * FROM Reply WHERE member_num = :1 ORDER BY created DESC LIMIT 10", one.num)
ids = []
replies = []
i = 0
for reply in q3:
if reply.topic.num not in ids:
i = i + 1
if i > 10:
break
replies.append(reply)
ids.append(reply.topic.num)
if len(replies) > 0:
memcache.set('member::' + str(one.num) + '::participated', replies, 7200)
if len(replies) > 0:
template_values['replies'] = replies
template_values['show_block'] = False
template_values['show_follow'] = False
template_values['favorited'] = False
if one and member:
if one.num != member.num:
template_values['show_follow'] = True
template_values['show_block'] = True
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
if one.num in blocked:
template_values['one_is_blocked'] = True
else:
template_values['one_is_blocked'] = False
if member.hasFavorited(one):
template_values['favorited'] = True
else:
template_values['favorited'] = False
if 'message' in self.session:
template_values['message'] = self.session['message']
del self.session['message']
if one is not False:
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_home.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_home.html')
else:
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_not_found.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_not_found.html')
output = template.render(path, template_values)
self.response.out.write(output)
class MemberApiHandler(webapp.RequestHandler):
def get(self, member_username):
site = GetSite()
one = GetMemberByUsername(member_username)
if one:
if one.avatar_mini_url:
if (one.avatar_mini_url[0:1] == '/'):
one.avatar_mini_url = 'http://' + site.domain + one.avatar_mini_url
one.avatar_normal_url = 'http://' + site.domain + one.avatar_normal_url
one.avatar_large_url = 'http://' + site.domain + one.avatar_large_url
template_values = {}
template_values['site'] = site
template_values['one'] = one
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'member.json')
self.response.headers['Content-type'] = 'application/json;charset=UTF-8'
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.error(404)
class SettingsHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
self.session = Session()
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › ' + l10n.settings.decode('utf-8')
if (member):
template_values['member'] = member
template_values['member_username'] = member.username
template_values['member_email'] = member.email
if (member.website == None):
member.website = ''
template_values['member_website'] = member.website
if (member.twitter == None):
member.twitter = ''
template_values['member_twitter'] = member.twitter
if (member.location == None):
member.location = ''
if member.psn is None:
member.psn = ''
template_values['member_psn'] = member.psn
if (member.my_home == None):
member.my_home = ''
template_values['member_my_home'] = member.my_home
template_values['member_btc'] = member.btc
template_values['member_location'] = member.location
if (member.tagline == None):
member.tagline = ''
template_values['member_tagline'] = member.tagline
if (member.bio == None):
member.bio = ''
template_values['member_bio'] = member.bio
template_values['member_show_home_top'] = member.show_home_top
template_values['member_show_quick_post'] = member.show_quick_post
if member.l10n is None:
member.l10n = 'en'
template_values['member_l10n'] = member.l10n
s = GetLanguageSelect(member.l10n)
template_values['s'] = s
if member.twitter_sync == 1:
template_values['member_twitter_sync'] = 1
if member.use_my_css == 1:
template_values['member_use_my_css'] = 1
if (member.my_css == None):
member.my_css = ''
template_values['member_my_css'] = member.my_css
if 'message' in self.session:
message = self.session['message']
del self.session['message']
else:
message = None
template_values['message'] = message
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
template_values['member_stats_blocks'] = len(blocked)
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_settings.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_settings.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
def post(self):
self.session = Session()
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
errors = 0
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['page_title'] = site.title + u' › ' + l10n.settings.decode('utf-8')
if (member):
template_values['member'] = member
template_values['member_username'] = member.username
template_values['member_email'] = member.email
template_values['member_website'] = member.website
template_values['member_twitter'] = member.twitter
# Verification: password
password_error = 0
password_update = False
password_error_messages = ['',
'新密码长度不能超过 32 个字符',
'请输入当前密码',
'当前密码不正确'
]
password_new = self.request.get('password_new').strip()
if (len(password_new) > 0):
password_update = True
if (len(password_new) > 32):
password_error = 1
else:
password_current = self.request.get('password_current').strip()
if (len(password_current) == 0):
password = 2
else:
password_current_sha1 = hashlib.sha1(password_current).hexdigest()
if (password_current_sha1 != member.password):
password_error = 3
template_values['password_error'] = password_error
template_values['password_error_message'] = password_error_messages[password_error]
if ((password_error == 0) and (password_update == True)):
member.password = hashlib.sha1(password_new).hexdigest()
member.auth = hashlib.sha1(str(member.num) + ':' + member.password).hexdigest()
member.put()
self.response.headers['Set-Cookie'] = 'auth=' + member.auth + '; expires=' + (datetime.datetime.now() + datetime.timedelta(days=365)).strftime("%a, %d-%b-%Y %H:%M:%S GMT") + '; path=/'
self.redirect('/settings')
# Verification: email
member_email_error = 0
member_email_error_messages = ['',
u'请输入你的电子邮件地址',
u'电子邮件地址长度不能超过 32 个字符',
u'你输入的电子邮件地址不符合规则',
u'抱歉这个电子邮件地址已经有人注册过了']
member_email = self.request.get('email').strip()
if (len(member_email) == 0):
errors = errors + 1
member_email_error = 1
else:
if (len(member_email) > 32):
errors = errors + 1
member_email_error = 2
else:
p = re.compile(r"(?:^|\s)[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)", re.IGNORECASE)
if (p.search(member_email)):
q = db.GqlQuery('SELECT * FROM Member WHERE email = :1 AND num != :2', member_email.lower(), member.num)
if (q.count() > 0):
errors = errors + 1
member_email_error = 4
else:
errors = errors + 1
member_email_error = 3
template_values['member_email'] = member_email
template_values['member_email_error'] = member_email_error
template_values['member_email_error_message'] = member_email_error_messages[member_email_error]
# Verification: website
member_website_error = 0
member_website_error_messages = ['',
u'个人网站地址长度不能超过 200 个字符',
u'这个网站地址不符合规则'
]
member_website = self.request.get('website').strip()
if (len(member_website) == 0):
member_website = ''
else:
if (len(member_website) > 200):
errors = errors + 1
member_website_error = 1
else:
p = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
if (p.search(member_website)):
errors = errors
else:
errors = errors + 1
member_website_error = 2
template_values['member_website'] = member_website
template_values['member_website_error'] = member_website_error
template_values['member_website_error_message'] = member_website_error_messages[member_website_error]
# Verification: Twitter
member_twitter_error = 0
member_twitter_error_messages = ['',
u'Twitter 用户名长度不能超过 20 个字符',
u'Twitter 用户名不符合规则'
]
member_twitter = self.request.get('twitter').strip()
if (len(member_twitter) == 0):
member_twitter = ''
else:
if (len(member_twitter) > 20):
errors = errors + 1
member_twitter_error = 1
else:
p = re.compile('[a-zA-Z0-9\_]+')
if (p.search(member_twitter)):
errors = errors
else:
errors = errors + 1
member_twitter_error = 2
template_values['member_twitter'] = member_twitter
template_values['member_twitter_error'] = member_twitter_error
template_values['member_twitter_error_message'] = member_twitter_error_messages[member_twitter_error]
# Verification: psn
member_psn_error = 0
member_psn_error_messages = ['',
u'PSN ID 长度不能超过 20 个字符',
u'PSN ID 不符合规则'
]
member_psn = self.request.get('psn').strip()
if (len(member_psn) == 0):
member_psn = ''
else:
if (len(member_psn) > 20):
errors = errors + 1
member_psn_error = 1
else:
p = re.compile('^[a-zA-Z0-9\-\_]+$')
if (p.search(member_psn)):
errors = errors
else:
errors = errors + 1
member_psn_error = 2
template_values['member_psn'] = member_psn
template_values['member_psn_error'] = member_psn_error
template_values['member_psn_error_message'] = member_psn_error_messages[member_psn_error]
# Verification: my_home
member_my_home_error = 0
member_my_home_error_messages = ['',
u'不是一个合法的自定义首页跳转位置',
u'自定义首页跳转位置长度不能超过 32 个字符',
u'自定义首页跳转位置必须以 / 开头'
]
member_my_home = self.request.get('my_home').strip()
if len(member_my_home) > 0:
if member_my_home == '/' or member_my_home.startswith('/signout'):
member_my_home_error = 1
errors = errors + 1
else:
if len(member_my_home) > 32:
member_my_home_error = 2
errors = errors + 1
else:
if member_my_home.startswith('/') is not True:
member_my_home_error = 3
errors = errors + 1
template_values['member_my_home'] = member_my_home
template_values['member_my_home_error'] = member_my_home_error
template_values['member_my_home_error_message'] = member_my_home_error_messages[member_my_home_error]
# Verification: btc
member_btc_error = 0
member_btc_error_messages = ['',
u'BTC 收款地址长度不能超过 40 个字符',
u'BTC 收款地址不符合规则'
]
member_btc = self.request.get('btc').strip()
if (len(member_btc) == 0):
member_btc = ''
else:
if (len(member_btc) > 40):
errors = errors + 1
member_btc_error = 1
else:
p = re.compile('^[a-zA-Z0-9]+$')
if (p.search(member_btc)):
errors = errors
else:
errors = errors + 1
member_btc_error = 2
template_values['member_btc'] = member_btc
template_values['member_btc_error'] = member_btc_error
template_values['member_btc_error_message'] = member_btc_error_messages[member_btc_error]
# Verification: location
member_location_error = 0
member_location_error_messages = ['',
u'所在地长度不能超过 40 个字符'
]
member_location = self.request.get('location').strip()
if (len(member_location) == 0):
member_location = ''
else:
if (len(member_location) > 40):
errors = errors + 1
member_location_error = 1
template_values['member_location'] = member_location
template_values['member_location_error'] = member_location_error
template_values['member_location_error_message'] = member_location_error_messages[member_location_error]
# Verification: tagline
member_tagline_error = 0
member_tagline_error_messages = ['',
u'个人签名长度不能超过 70 个字符'
]
member_tagline = self.request.get('tagline').strip()
if (len(member_tagline) == 0):
member_tagline = ''
else:
if (len(member_tagline) > 70):
errors = errors + 1
member_tagline_error = 1
template_values['member_tagline'] = member_tagline
template_values['member_tagline_error'] = member_tagline_error
template_values['member_tagline_error_message'] = member_tagline_error_messages[member_tagline_error]
# Verification: bio
member_bio_error = 0
member_bio_error_messages = ['',
u'个人简介长度不能超过 2000 个字符'
]
member_bio = self.request.get('bio').strip()
if (len(member_bio) == 0):
member_bio = ''
else:
if (len(member_bio) > 2000):
errors = errors + 1
member_bio_error = 1
template_values['member_bio'] = member_bio
template_values['member_bio_error'] = member_bio_error
template_values['member_bio_error_message'] = member_bio_error_messages[member_bio_error]
# Verification: show_home_top and show_quick_post
try:
member_show_home_top = int(self.request.get('show_home_top'))
except:
member_show_home_top = 1
try:
member_show_quick_post = int(self.request.get('show_quick_post'))
except:
member_show_quick_post = 0
if member_show_home_top not in [0, 1]:
member_show_home_top = 1
if member_show_quick_post not in [0, 1]:
member_show_quick_post = 0
# Verification: l10n
member_l10n = self.request.get('l10n').strip()
supported = GetSupportedLanguages()
if member_l10n == '':
member_l10n = site.l10n
else:
if member_l10n not in supported:
member_l10n = site.l10n
s = GetLanguageSelect(member_l10n)
template_values['s'] = s
template_values['member_l10n'] = member_l10n
# Verification: twitter_sync
if member.twitter_oauth == 1:
member_twitter_sync = self.request.get('twitter_sync')
if member_twitter_sync == 'on':
member_twitter_sync = 1
else:
member_twitter_sync = 0
template_values['member_twitter_sync'] = member_twitter_sync
# Verification: use_my_css
member_use_my_css = self.request.get('use_my_css')
if member_use_my_css == 'on':
member_use_my_css = 1
else:
member_use_my_css = 0
template_values['member_use_my_css'] = member_use_my_css
# Verification: my_css
member_my_css_error = 0
member_my_css_error_messages = ['',
u'CSS Hack cannot be longer than 2000 characters'
]
member_my_css = self.request.get('my_css').strip()
if (len(member_my_css) == 0):
member_my_css = ''
else:
if (len(member_my_css) > 2000):
errors = errors + 1
member_my_css_error = 1
template_values['member_my_css'] = member_my_css
template_values['member_my_css_error'] = member_my_css_error
template_values['member_my_css_error_message'] = member_my_css_error_messages[member_my_css_error]
template_values['errors'] = errors
if (errors == 0):
member.email = member_email.lower()
member.website = member_website
member.twitter = member_twitter
member.psn = member_psn
member.btc = member_btc
member.location = member_location
member.tagline = member_tagline
if member.twitter_oauth == 1:
member.twitter_sync = member_twitter_sync
member.use_my_css = member_use_my_css
member.my_css = member_my_css
if member_my_home_error == 0 and len(member_my_home) > 0:
member.my_home = member_my_home
else:
if member_my_home_error == 0:
member.my_home = None
member.bio = member_bio
member.show_home_top = member_show_home_top
member.show_quick_post = member_show_quick_post
member.l10n = member_l10n
member.put()
memcache.delete('Member::' + str(member.username))
memcache.delete('Member::' + str(member.username_lower))
memcache.set('Member_' + str(member.num), member, 86400)
self.session['message'] = '个人设置成功更新'
self.redirect('/settings')
else:
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_settings.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_settings.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
class SettingsPasswordHandler(webapp.RequestHandler):
def post(self):
site = GetSite()
browser = detect(self.request)
self.session = Session()
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 密码设置'
template_values['system_version'] = SYSTEM_VERSION
errors = 0
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if (member):
template_values['member'] = member
template_values['member_username'] = member.username
template_values['member_email'] = member.email
# Verification: password
password_error = 0
password_update = False
password_error_messages = ['',
'新密码长度不能超过 32 个字符',
'请输入当前密码',
'当前密码不正确'
]
password_new = self.request.get('password_new').strip()
if (len(password_new) > 0):
password_update = True
if (len(password_new) > 32):
password_error = 1
else:
password_current = self.request.get('password_current').strip()
if (len(password_current) == 0):
password_error = 2
else:
password_current_sha1 = hashlib.sha1(password_current).hexdigest()
if (password_current_sha1 != member.password):
password_error = 3
template_values['password_error'] = password_error
template_values['password_error_message'] = password_error_messages[password_error]
if ((password_error == 0) and (password_update == True)):
member.password = hashlib.sha1(password_new).hexdigest()
member.auth = hashlib.sha1(str(member.num) + ':' + member.password).hexdigest()
member.put()
memcache.set(member.auth, member.num, 86400 * 14)
memcache.set('Member_' + str(member.num), member, 86400 * 14)
self.session['message'] = '密码已成功更新,下次请用新密码登录'
self.response.headers['Set-Cookie'] = 'auth=' + member.auth + '; expires=' + (datetime.datetime.now() + datetime.timedelta(days=365)).strftime("%a, %d-%b-%Y %H:%M:%S GMT") + '; path=/'
self.redirect('/settings')
else:
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_settings_password.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_settings_password.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
class SettingsAvatarHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
self.session = Session()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 头像'
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if (member):
if 'message' in self.session:
template_values['message'] = self.session['message']
del self.session['message']
template_values['member'] = member
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'member_settings_avatar.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'member_settings_avatar.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
self.redirect('/signin')
def post(self):
site = GetSite()
self.session = Session()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if (member):
dest = '/settings/avatar'
timestamp = str(int(time.time()))
try:
avatar = self.request.get('avatar')
except:
return self.redirect(dest)
if avatar is None:
return self.redirect(dest)
avatar_len = len(avatar)
if avatar_len == 0:
return self.redirect(dest)
avatar_73 = images.resize(avatar, 73, 73)
avatar_48 = images.resize(avatar, 48, 48)
avatar_24 = images.resize(avatar, 24, 24)
# Large 73x73
q1 = db.GqlQuery("SELECT * FROM Avatar WHERE name = :1", 'avatar_' + str(member.num) + '_large')
if (q1.count() == 1):
avatar_large = q1[0]
avatar_large.content = db.Blob(avatar_73)
avatar_large.put()
else:
qc1 = db.GqlQuery("SELECT * FROM Counter WHERE name = :1", 'avatar.max')
if (qc1.count() == 1):
counter1 = qc1[0]
counter1.value = counter1.value + 1
else:
counter1 = Counter()
counter1.name = 'avatar.max'
counter1.value = 1
counter1.put()
avatar_large = Avatar()
avatar_large.name = 'avatar_' + str(member.num) + '_large'
avatar_large.content = db.Blob(avatar_73)
avatar_large.num = counter1.value
avatar_large.put()
member.avatar_large_url = '/avatar/' + str(member.num) + '/large?r=' + timestamp
member.put()
# Normal 48x48
q2 = db.GqlQuery("SELECT * FROM Avatar WHERE name = :1", 'avatar_' + str(member.num) + '_normal')
if (q2.count() == 1):
avatar_normal = q2[0]
avatar_normal.content = db.Blob(avatar_48)
avatar_normal.put()
else:
qc2 = db.GqlQuery("SELECT * FROM Counter WHERE name = :1", 'avatar.max')
if (qc2.count() == 1):
counter2 = qc2[0]
counter2.value = counter2.value + 1
else:
counter2 = Counter()
counter2.name = 'avatar.max'
counter2.value = 1
counter2.put()
avatar_normal = Avatar()
avatar_normal.name = 'avatar_' + str(member.num) + '_normal'
avatar_normal.content = db.Blob(avatar_48)
avatar_normal.num = counter2.value
avatar_normal.put()
member.avatar_normal_url = '/avatar/' + str(member.num) + '/normal?r=' + timestamp
member.put()
# Mini 24x24
q3 = db.GqlQuery("SELECT * FROM Avatar WHERE name = :1", 'avatar_' + str(member.num) + '_mini')
if (q3.count() == 1):
avatar_mini = q3[0]
avatar_mini.content = db.Blob(avatar_24)
avatar_mini.put()
else:
qc3 = db.GqlQuery("SELECT * FROM Counter WHERE name = :1", 'avatar.max')
if (qc3.count() == 1):
counter3 = qc3[0]
counter3.value = counter3.value + 1
else:
counter3 = Counter()
counter3.name = 'avatar.max'
counter3.value = 1
counter3.put()
avatar_mini = Avatar()
avatar_mini.name = 'avatar_' + str(member.num) + '_mini'
avatar_mini.content = db.Blob(avatar_24)
avatar_mini.num = counter3.value
avatar_mini.put()
member.avatar_mini_url = '/avatar/' + str(member.num) + '/mini?r=' + timestamp
member.put()
# Upload to MobileMe
if config.mobileme_enabled:
headers = {'Authorization' : 'Basic ' + base64.b64encode(config.mobileme_username + ':' + config.mobileme_password)}
host = 'idisk.me.com'
# Sharding
timestamp = str(int(time.time()))
shard = member.num % 31
root = '/' + config.mobileme_username + '/Web/Sites/v2ex/avatars/' + str(shard)
root_mini = root + '/mini'
root_normal = root + '/normal'
root_large = root + '/large'
h = httplib.HTTPConnection(host)
# Mini
h.request('PUT', root_mini + '/' + str(member.num) + '.png', str(avatar_24), headers)
response = h.getresponse()
if response.status == 201 or response.status == 204:
member.avatar_mini_url = 'http://web.me.com/' + config.mobileme_username + '/v2ex/avatars/' + str(shard) + '/mini/' + str(member.num) + '.png?r=' + timestamp
# Normal
h.request('PUT', root_normal + '/' + str(member.num) + '.png', str(avatar_48), headers)
response = h.getresponse()
if response.status == 201 or response.status == 204:
member.avatar_normal_url = 'http://web.me.com/' + config.mobileme_username + '/v2ex/avatars/' + str(shard) + '/normal/' + str(member.num) + '.png?r=' + timestamp
# Large
h.request('PUT', root_large + '/' + str(member.num) + '.png', str(avatar_73), headers)
response = h.getresponse()
if response.status == 201 or response.status == 204:
member.avatar_large_url = 'http://web.me.com/' + config.mobileme_username + '/v2ex/avatars/' + str(shard) + '/large/' + str(member.num) + '.png?r=' + timestamp
member.put()
memcache.set('Member_' + str(member.num), member, 86400 * 14)
memcache.set('Member::' + member.username_lower, member, 86400 * 14)
memcache.delete('Avatar::avatar_' + str(member.num) + '_large')
memcache.delete('Avatar::avatar_' + str(member.num) + '_normal')
memcache.delete('Avatar::avatar_' + str(member.num) + '_mini')
self.session['message'] = '新头像设置成功'
self.redirect('/settings/avatar')
else:
self.redirect('/signin')
class MemberBlockHandler(webapp.RequestHandler):
def get(self, key):
go = '/'
member = CheckAuth(self)
if member:
member = db.get(member.key())
one = db.get(db.Key(key))
if one:
if one.num != member.num:
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
if len(blocked) == 0:
blocked = []
if one.num not in blocked:
blocked.append(one.num)
member.blocked = pickle.dumps(blocked)
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
self.redirect(go)
class MemberUnblockHandler(webapp.RequestHandler):
def get(self, key):
go = '/'
member = CheckAuth(self)
if member:
member = db.get(member.key())
one = db.get(db.Key(key))
if one:
if one.num != member.num:
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
if len(blocked) == 0:
blocked = []
if one.num in blocked:
blocked.remove(one.num)
member.blocked = pickle.dumps(blocked)
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
self.redirect(go)
def main():
application = webapp.WSGIApplication([
('/member/([a-z0-9A-Z\_\-]+)', MemberHandler),
('/member/([a-z0-9A-Z\_\-]+).json', MemberApiHandler),
('/settings', SettingsHandler),
('/settings/password', SettingsPasswordHandler),
('/settings/avatar', SettingsAvatarHandler),
('/block/(.*)', MemberBlockHandler),
('/unblock/(.*)', MemberUnblockHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()