This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 483
/
main.py
1225 lines (1183 loc) · 55.9 KB
/
main.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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding=utf-8
import base64
import os
import re
import time
import datetime
import hashlib
import urllib
import string
import random
import pickle
import math
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from google.appengine.api.labs import taskqueue
from google.appengine.api import mail
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 Counter
from v2ex.babel import Section
from v2ex.babel import Node
from v2ex.babel import Topic
from v2ex.babel import Reply
from v2ex.babel import PasswordResetToken
from v2ex.babel import SYSTEM_VERSION
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.handlers import BaseHandler
from django.utils import simplejson as json
from v2ex.babel.ext import captcha
template.register_template_library('v2ex.templatetags.filters')
import config
class HomeHandler(webapp.RequestHandler):
def head(self):
pass
def get(self):
host = self.request.headers['Host']
if host == 'beta.v2ex.com':
return self.redirect('http://www.v2ex.com/')
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = GetSite()
template_values['canonical'] = 'http://' + site.domain + '/'
template_values['rnd'] = random.randrange(1, 100)
template_values['page_title'] = site.title
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
if member:
if member.my_home != None and len(member.my_home) > 0:
return self.redirect(member.my_home)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
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=/'
template_values['member'] = member
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
if (len(blocked) > 0):
template_values['blocked'] = ','.join(map(str, blocked))
if member:
recent_nodes = memcache.get('member::' + str(member.num) + '::recent_nodes')
if recent_nodes:
template_values['recent_nodes'] = recent_nodes
nodes_new = []
nodes_new = memcache.get('home_nodes_new')
if nodes_new is None:
nodes_new = []
qnew = db.GqlQuery("SELECT * FROM Node ORDER BY created DESC LIMIT 10")
if (qnew.count() > 0):
i = 0
for node in qnew:
nodes_new.append(node)
i = i + 1
memcache.set('home_nodes_new', nodes_new, 86400)
template_values['nodes_new'] = nodes_new
ignored = ['newbie', 'in', 'flamewar', 'pointless', 'tuan', '528491', 'chamber', 'autistic', 'blog', 'love', 'flood', 'beforesunrise', 'diary', 'fanfou', 'closed']
if browser['ios']:
home_rendered = memcache.get('home_rendered_mobile')
if home_rendered is None:
latest = memcache.get('q_latest_16')
if (latest):
template_values['latest'] = latest
else:
q2 = db.GqlQuery("SELECT * FROM Topic ORDER BY last_touched DESC LIMIT 16")
topics = []
for topic in q2:
if topic.node_name not in ignored:
topics.append(topic)
memcache.set('q_latest_16', topics, 600)
latest = topics
template_values['latest'] = latest
path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'home_mobile.html')
home_rendered = template.render(path, template_values)
memcache.set('home_rendered_mobile', home_rendered, 600)
template_values['home'] = home_rendered
else:
home_rendered = memcache.get('home_rendered')
if home_rendered is None:
latest = memcache.get('q_latest_16')
if (latest):
template_values['latest'] = latest
else:
q2 = db.GqlQuery("SELECT * FROM Topic ORDER BY last_touched DESC LIMIT 16")
topics = []
for topic in q2:
if topic.node_name not in ignored:
topics.append(topic)
memcache.set('q_latest_16', topics, 600)
latest = topics
template_values['latest'] = latest
path = os.path.join(os.path.dirname(__file__), 'tpl', 'portion', 'home.html')
home_rendered = template.render(path, template_values)
memcache.set('home_rendered', home_rendered, 600)
template_values['home'] = home_rendered
member_total = memcache.get('member_total')
if member_total is None:
q3 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'member.total'")
if (q3.count() > 0):
member_total = q3[0].value
else:
member_total = 0
memcache.set('member_total', member_total, 3600)
template_values['member_total'] = member_total
topic_total = memcache.get('topic_total')
if topic_total is None:
q4 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'topic.total'")
if (q4.count() > 0):
topic_total = q4[0].value
else:
topic_total = 0
memcache.set('topic_total', topic_total, 3600)
template_values['topic_total'] = topic_total
reply_total = memcache.get('reply_total')
if reply_total is None:
q5 = db.GqlQuery("SELECT * FROM Counter WHERE name = 'reply.total'")
if (q5.count() > 0):
reply_total = q5[0].value
else:
reply_total = 0
memcache.set('reply_total', reply_total, 3600)
template_values['reply_total'] = reply_total
hottest = memcache.get('index_hottest_sidebar')
if hottest is None:
qhot = db.GqlQuery("SELECT * FROM Node ORDER BY topics DESC LIMIT 25")
hottest = u''
for node in qhot:
hottest = hottest + '<a href="/go/' + node.name + '" class="item_node">' + node.title + '</a>'
memcache.set('index_hottest_sidebar', hottest, 86400)
template_values['index_hottest_sidebar'] = hottest
c = memcache.get('index_categories')
if c is None:
c = ''
i = 0
if site.home_categories is not None:
categories = site.home_categories.split("\n")
else:
categories = []
for category in categories:
category = category.strip()
i = i + 1
c = c + '<div class="cell"><table cellpadding="0" cellspacing="0" border="0"><tr><td align="right" width="60"><span class="fade">' + category + '</span></td><td style="line-height: 200%; padding-left: 10px;">'
qx = db.GqlQuery("SELECT * FROM Node WHERE category = :1 ORDER BY topics DESC", category)
for node in qx:
c = c + '<a href="/go/' + node.name + '" style="font-size: 14px;">' + node.title + '</a> '
c = c + '</td></tr></table></div>'
memcache.set('index_categories', c, 86400)
template_values['c'] = c
if (browser['ios']):
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'index.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'index.html')
output = template.render(path, template_values)
self.response.out.write(output)
class PlanesHandler(BaseHandler):
def get(self):
c = 0
c = memcache.get('planes_c')
s = ''
s = memcache.get('planes')
if (s == None):
c = 0
s = ''
q = db.GqlQuery("SELECT * FROM Section ORDER BY nodes DESC")
if (q.count() > 0):
for section in q:
q2 = db.GqlQuery("SELECT * FROM Node WHERE section_num = :1 ORDER BY topics DESC", section.num)
n = ''
if (q2.count() > 0):
nodes = []
i = 0
for node in q2:
nodes.append(node)
i = i + 1
random.shuffle(nodes)
for node in nodes:
fs = random.randrange(12, 16)
n = n + '<a href="/go/' + node.name + '" class="item_node">' + node.title + '</a>'
c = c + 1
s = s + '<div class="sep20"></div><div class="box"><div class="cell"><div class="fr"><strong class="snow">' + section.title_alternative + u'</strong><small class="snow"> • ' + str(section.nodes) + ' nodes</small></div>' + section.title + '</div><div class="inner" align="center">' + n + '</div></div>'
memcache.set('planes', s, 86400)
memcache.set('planes_c', c, 86400)
self.values['c'] = c
self.values['s'] = s
self.values['page_title'] = self.site.title.decode('utf-8') + u' › ' + self.l10n.planes.decode('utf-8')
self.finalize(template_name='planes')
class RecentHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['system_version'] = SYSTEM_VERSION
template_values['page_title'] = site.title + u' › 最近的 50 个主题'
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
try:
blocked = pickle.loads(member.blocked.encode('utf-8'))
except:
blocked = []
if (len(blocked) > 0):
template_values['blocked'] = ','.join(map(str, blocked))
latest = memcache.get('q_recent_50')
if (latest):
template_values['latest'] = latest
else:
q2 = db.GqlQuery("SELECT * FROM Topic ORDER BY last_touched DESC LIMIT 16,50")
topics = []
IGNORED_RECENT = ['flamewar', 'pointless', 'in', 'autistic', 'chamber', 'flood', 'diary', 'fanfou']
for topic in q2:
if topic.node_name not in IGNORED_RECENT:
topics.append(topic)
memcache.set('q_recent_50', topics, 80)
template_values['latest'] = topics
template_values['latest_total'] = len(topics)
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'recent.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'recent.html')
output = template.render(path, template_values)
expires_date = datetime.datetime.utcnow() + datetime.timedelta(minutes=2)
expires_str = expires_date.strftime("%d %b %Y %H:%M:%S GMT")
self.response.headers.add_header("Expires", expires_str)
self.response.headers['Cache-Control'] = 'max-age=120, must-revalidate'
self.response.out.write(output)
class UAHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
template_values['member'] = member
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
template_values['ua'] = os.environ['HTTP_USER_AGENT']
template_values['page_title'] = site.title + u' › 用户代理字符串'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'ua.html')
output = template.render(path, template_values)
self.response.out.write(output)
class SigninHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
member = False
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 登入'
template_values['system_version'] = SYSTEM_VERSION
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
errors = 0
template_values['errors'] = errors
template_values['next'] = self.request.referer
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'signin.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'signin.html')
output = template.render(path, template_values)
self.response.out.write(output)
def post(self):
site = GetSite()
member = False
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 登入'
template_values['system_version'] = SYSTEM_VERSION
u = self.request.get('u').strip()
p = self.request.get('p').strip()
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
errors = 0
error_messages = ['', '请输入用户名和密码', '你输入的用户名或密码不正确']
if (len(u) > 0 and len(p) > 0):
p_sha1 = hashlib.sha1(p).hexdigest()
if '@' in u:
q = db.GqlQuery("SELECT * FROM Member WHERE email = :1 AND password = :2", u.lower(), p_sha1)
else:
q = db.GqlQuery("SELECT * FROM Member WHERE username_lower = :1 AND password = :2", u.lower(), p_sha1)
if (q.count() == 1):
member = q[0]
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=/'
next = self.request.get('next').strip()
host = self.request.host + '/'
if next.rfind(host)>0 and not next.rfind('/sign'):
self.redirect(next)
else:
self.redirect('/')
else:
errors = 2
else:
errors = 1
template_values['u'] = u
template_values['p'] = p
template_values['errors'] = errors
template_values['error_message'] = error_messages[errors]
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'signin.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'signin.html')
output = template.render(path, template_values)
self.response.out.write(output)
class SignupHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
member = False
chtml = captcha.displayhtml(
public_key = config.recaptcha_public_key,
use_ssl = False,
error = None)
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 注册'
template_values['system_version'] = SYSTEM_VERSION
template_values['errors'] = 0
template_values['captchahtml'] = chtml
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'signup.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'signup.html')
output = template.render(path, template_values)
self.response.out.write(output)
def post(self):
site = GetSite()
member = False
browser = detect(self.request)
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 注册'
template_values['system_version'] = SYSTEM_VERSION
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
errors = 0
# Verification: username
member_username_error = 0
member_username_error_messages = ['',
l10n.username_empty,
l10n.username_too_long,
l10n.username_too_short,
l10n.username_invalid,
l10n.username_taken]
member_username = self.request.get('username').strip()
# Special cases
if 'vpn' in member_username:
return self.redirect('http://www.v2ex.com/')
if (len(member_username) == 0):
errors = errors + 1
member_username_error = 1
else:
if (len(member_username) > 16):
errors = errors + 1
member_username_error = 2
else:
if (len(member_username) < 3):
errors = errors + 1
member_username_error = 3
else:
if (re.search('^[a-zA-Z0-9\_]+$', member_username)):
q = db.GqlQuery('SELECT __key__ FROM Member WHERE username_lower = :1', member_username.lower())
if (q.count() > 0):
errors = errors + 1
member_username_error = 5
else:
errors = errors + 1
member_username_error = 4
template_values['member_username'] = member_username
template_values['member_username_error'] = member_username_error
template_values['member_username_error_message'] = member_username_error_messages[member_username_error]
# Verification: password
member_password_error = 0
member_password_error_messages = ['',
u'请输入你的密码',
u'密码长度不能超过 32 个字符'
]
member_password = self.request.get('password').strip()
if (len(member_password) == 0):
errors = errors + 1
member_password_error = 1
else:
if (len(member_password) > 32):
errors = errors + 1
member_password_error = 2
template_values['member_password'] = member_password
template_values['member_password_error'] = member_password_error
template_values['member_password_error_message'] = member_password_error_messages[member_password_error]
# 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 __key__ FROM Member WHERE email = :1', member_email.lower())
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: reCAPTCHA
challenge = self.request.get('recaptcha_challenge_field')
response = self.request.get('recaptcha_response_field')
remoteip = os.environ['REMOTE_ADDR']
cResponse = captcha.submit(
challenge,
response,
config.recaptcha_private_key,
remoteip)
if cResponse.is_valid:
logging.info('reCAPTCHA verification passed')
template_values['recaptcha_error'] = 0
else:
errors = errors + 1
error = cResponse.error_code
chtml = captcha.displayhtml(
public_key = config.recaptcha_public_key,
use_ssl = False,
error = cResponse.error_code)
template_values['captchahtml'] = chtml
template_values['recaptcha_error'] = 1
template_values['recaptcha_error_message'] = '请重新输入 reCAPTCHA 验证码'
template_values['errors'] = errors
if (errors == 0):
member = Member()
q = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'member.max')
if (q.count() == 1):
counter = q[0]
counter.value = counter.value + 1
else:
counter = Counter()
counter.name = 'member.max'
counter.value = 1
q2 = db.GqlQuery('SELECT * FROM Counter WHERE name = :1', 'member.total')
if (q2.count() == 1):
counter2 = q2[0]
counter2.value = counter2.value + 1
else:
counter2 = Counter()
counter2.name = 'member.total'
counter2.value = 1
member.num = counter.value
member.username = member_username
member.username_lower = member_username.lower()
member.password = hashlib.sha1(member_password).hexdigest()
member.email = member_email.lower()
member.auth = hashlib.sha1(str(member.num) + ':' + member.password).hexdigest()
member.l10n = site.l10n
member.newbie = 1
member.noob = 0
if member.num == 1:
member.level = 0
else:
member.level = 1000
member.put()
counter.put()
counter2.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=/'
memcache.delete('member_total')
self.redirect('/')
else:
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'signup.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'signup.html')
output = template.render(path, template_values)
self.response.out.write(output)
class SignoutHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
member = False
template_values = {}
template_values['site'] = site
template_values['page_title'] = site.title + u' › 登出'
template_values['system_version'] = SYSTEM_VERSION
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
cookies = Cookies(self, max_age = 86400, path = '/')
del cookies['auth']
if browser['ios']:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'signout.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'signout.html')
output = template.render(path, template_values)
self.response.out.write(output)
class ForgotHandler(webapp.RequestHandler):
def get(self):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['rnd'] = random.randrange(1, 100)
template_values['site'] = site
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
template_values['page_title'] = site.title + u' › 重新设置密码'
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'forgot.html')
output = template.render(path, template_values)
self.response.out.write(output)
def post(self):
site = GetSite()
browser = detect(self.request)
template_values = {}
template_values['rnd'] = random.randrange(1, 100)
template_values['site'] = site
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
template_values['page_title'] = site.title + u' › 重新设置密码'
# Verification: username & email
username = self.request.get('username').strip().lower()
email = self.request.get('email').strip().lower()
q = db.GqlQuery("SELECT * FROM Member WHERE username_lower = :1 AND email = :2", username, email)
if q.count() == 1:
one = q[0]
q2 = db.GqlQuery("SELECT * FROM PasswordResetToken WHERE timestamp > :1 AND email = :2", (int(time.time()) - 86400), email)
if q2.count() > 2:
error_message = '你不能在 24 小时内进行超过 2 次的密码重设操作。'
template_values['errors'] = 1
template_values['error_message'] = error_message
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'forgot.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
token = ''.join([str(random.randint(0, 9)) for i in range(32)])
prt = PasswordResetToken()
prt.token = token
prt.member = one
prt.email = one.email
prt.timestamp = int(time.time())
prt.put()
mail_template_values = {}
mail_template_values['site'] = site
mail_template_values['one'] = one
mail_template_values['host'] = self.request.headers['Host']
mail_template_values['token'] = token
mail_template_values['ua'] = self.request.headers['User-Agent']
mail_template_values['ip'] = self.request.remote_addr
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mail', 'reset_password.txt')
output = template.render(path, mail_template_values)
result = mail.send_mail(sender="[email protected]",
to=one.email,
subject="=?UTF-8?B?" + base64.b64encode((u"[" + site.title + u"] 重新设置密码").encode('utf-8')) + "?=",
body=output)
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'forgot_sent.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
error_message = '无法找到匹配的用户名和邮箱记录'
template_values['errors'] = 1
template_values['error_message'] = error_message
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'forgot.html')
output = template.render(path, template_values)
self.response.out.write(output)
class PasswordResetHandler(BaseHandler):
def get(self, token):
site = GetSite()
template_values = {}
template_values['site'] = site
member = False
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
token = str(token.strip().lower())
q = db.GqlQuery("SELECT * FROM PasswordResetToken WHERE token = :1 AND valid = 1", token)
if q.count() == 1:
prt = q[0]
template_values['page_title'] = site.title + u' › 重新设置密码'
template_values['token'] = prt
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'reset_password.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'token_not_found.html')
output = template.render(path, template_values)
self.response.out.write(output)
def post(self, token):
site = GetSite()
template_values = {}
template_values['site'] = site
member = False
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
token = str(token.strip().lower())
q = db.GqlQuery("SELECT * FROM PasswordResetToken WHERE token = :1 AND valid = 1", token)
if q.count() == 1:
prt = q[0]
template_values['page_title'] = site.title + u' › 重新设置密码'
template_values['token'] = prt
# Verification
errors = 0
new_password = str(self.request.get('new_password').strip())
new_password_again = str(self.request.get('new_password_again').strip())
if new_password is '' or new_password_again is '':
errors = errors + 1
error_message = '请输入两次新密码'
if errors == 0:
if new_password != new_password_again:
errors = errors + 1
error_message = '两次输入的新密码不一致'
if errors == 0:
if len(new_password) > 32:
errors = errors + 1
error_message = '新密码长度不能超过 32 个字符'
if errors == 0:
q2 = db.GqlQuery("SELECT * FROM Member WHERE num = :1", prt.member.num)
one = q2[0]
one.password = hashlib.sha1(new_password).hexdigest()
one.auth = hashlib.sha1(str(one.num) + ':' + one.password).hexdigest()
one.put()
prt.valid = 0
prt.put()
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'reset_password_ok.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
template_values['errors'] = errors
template_values['error_message'] = error_message
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'reset_password.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'token_not_found.html')
output = template.render(path, template_values)
self.response.out.write(output)
class NodeGraphHandler(BaseHandler):
def get(self, node_name):
site = GetSite()
browser = detect(self.request)
self.session = Session()
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
if member:
template_values['member'] = member
can_create = False
can_manage = False
if site.topic_create_level > 999:
if member:
can_create = True
else:
if member:
if member.level <= site.topic_create_level:
can_create = True
if member:
if member.level == 0:
can_manage = True
template_values['can_create'] = can_create
template_values['can_manage'] = can_manage
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
node = GetKindByName('Node', node_name)
template_values['node'] = node
if node:
template_values['feed_link'] = '/feed/' + node.name + '.xml'
template_values['feed_title'] = site.title + u' › ' + node.title
template_values['canonical'] = 'http://' + site.domain + '/go/' + node.name
if node.parent_node_name is None:
siblings = []
else:
siblings = db.GqlQuery("SELECT * FROM Node WHERE parent_node_name = :1 AND name != :2", node.parent_node_name, node.name)
template_values['siblings'] = siblings
if member:
favorited = member.hasFavorited(node)
template_values['favorited'] = favorited
recent_nodes = memcache.get('member::' + str(member.num) + '::recent_nodes')
recent_nodes_ids = memcache.get('member::' + str(member.num) + '::recent_nodes_ids')
if recent_nodes and recent_nodes_ids:
if (node.num in recent_nodes_ids) is not True:
recent_nodes.insert(0, node)
recent_nodes_ids.insert(0, node.num)
memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200)
memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200)
else:
recent_nodes = []
recent_nodes.append(node)
recent_nodes_ids = []
recent_nodes_ids.append(node.num)
memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200)
memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200)
template_values['recent_nodes'] = recent_nodes
template_values['page_title'] = site.title + u' › ' + node.title
else:
template_values['page_title'] = site.title + u' › 节点未找到'
section = False
if node:
section = GetKindByNum('Section', node.section_num)
template_values['section'] = section
if browser['ios']:
if (node):
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node_graph.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node_not_found.html')
else:
if (node):
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_graph.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_not_found.html')
output = template.render(path, template_values)
self.response.out.write(output)
class NodeHandler(webapp.RequestHandler):
def get(self, node_name):
site = GetSite()
browser = detect(self.request)
self.session = Session()
template_values = {}
template_values['site'] = site
template_values['rnd'] = random.randrange(1, 100)
template_values['system_version'] = SYSTEM_VERSION
member = CheckAuth(self)
if member:
template_values['member'] = member
can_create = False
can_manage = False
if site.topic_create_level > 999:
if member:
can_create = True
else:
if member:
if member.level <= site.topic_create_level:
can_create = True
if member:
if member.level == 0:
can_manage = True
template_values['can_create'] = can_create
template_values['can_manage'] = can_manage
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
node = GetKindByName('Node', node_name)
template_values['node'] = node
pagination = False
pages = 1
page = 1
page_size = 15
start = 0
has_more = False
more = 1
has_previous = False
previous = 1
if node:
template_values['feed_link'] = '/feed/' + node.name + '.xml'
template_values['feed_title'] = site.title + u' › ' + node.title
template_values['canonical'] = 'http://' + site.domain + '/go/' + node.name
if member:
favorited = member.hasFavorited(node)
template_values['favorited'] = favorited
recent_nodes = memcache.get('member::' + str(member.num) + '::recent_nodes')
recent_nodes_ids = memcache.get('member::' + str(member.num) + '::recent_nodes_ids')
if recent_nodes and recent_nodes_ids:
if (node.num in recent_nodes_ids) is not True:
recent_nodes.insert(0, node)
recent_nodes_ids.insert(0, node.num)
memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200)
memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200)
else:
recent_nodes = []
recent_nodes.append(node)
recent_nodes_ids = []
recent_nodes_ids.append(node.num)
memcache.set('member::' + str(member.num) + '::recent_nodes', recent_nodes, 7200)
memcache.set('member::' + str(member.num) + '::recent_nodes_ids', recent_nodes_ids, 7200)
template_values['recent_nodes'] = recent_nodes
template_values['page_title'] = site.title + u' › ' + node.title
# Pagination
if node.topics > page_size:
pagination = True
else:
pagination = False
if pagination:
if node.topics % page_size == 0:
pages = int(node.topics / page_size)
else:
pages = int(node.topics / page_size) + 1
page = self.request.get('p')
if (page == '') or (page is None):
page = 1
else:
page = int(page)
if page > pages:
page = pages
else:
if page < 1:
page = 1
if page < pages:
has_more = True
more = page + 1
if page > 1:
has_previous = True
previous = page - 1
start = (page - 1) * page_size
template_values['canonical'] = 'http://' + site.domain + '/go/' + node.name + '?p=' + str(page)
else:
template_values['page_title'] = site.title + u' › 节点未找到'
template_values['pagination'] = pagination
template_values['pages'] = pages
template_values['page'] = page
template_values['page_size'] = page_size
template_values['has_more'] = has_more
template_values['more'] = more
template_values['has_previous'] = has_previous
template_values['previous'] = previous
section = False
if node:
section = GetKindByNum('Section', node.section_num)
template_values['section'] = section
topics = False
if node:
q3 = db.GqlQuery("SELECT * FROM Topic WHERE node_num = :1 ORDER BY last_touched DESC LIMIT " + str(start) + ", " + str(page_size), node.num)
topics = q3
template_values['latest'] = topics
if browser['ios']:
if (node):
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'mobile', 'node_not_found.html')
else:
if (node):
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node.html')
else:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'node_not_found.html')
output = template.render(path, template_values)
self.response.out.write(output)
class NodeApiHandler(webapp.RequestHandler):
def get(self, node_name):
site = GetSite()
node = GetKindByName('Node', node_name)
if node:
template_values = {}
template_values['site'] = site
template_values['node'] = node
path = os.path.join(os.path.dirname(__file__), 'tpl', 'api', 'node.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 SearchHandler(webapp.RequestHandler):
def get(self, q):
site = GetSite()
q = urllib.unquote(q)
template_values = {}
template_values['site'] = site
member = CheckAuth(self)
l10n = GetMessages(self, member, site)
template_values['l10n'] = l10n
if member:
template_values['member'] = member
template_values['page_title'] = site.title + u' › 搜索 ' + q.decode('utf-8')
template_values['q'] = q
if config.fts_enabled is not True:
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'search_unavailable.html')
output = template.render(path, template_values)
self.response.out.write(output)
else:
if re.findall('^([a-zA-Z0-9\_]+)$', q):
node = GetKindByName('Node', q.lower())
if node is not None:
template_values['node'] = node
# Fetch result
q_lowered = q.lower()
q_md5 = hashlib.md5(q_lowered).hexdigest()
topics = memcache.get('q::' + q_md5)
if topics is None:
try:
if os.environ['SERVER_SOFTWARE'] == 'Development/1.0':
fts = u'http://127.0.0.1:20000/search?q=' + str(urllib.quote(q_lowered))
else:
fts = u'http://' + config.fts_server + '/search?q=' + str(urllib.quote(q_lowered))
response = urlfetch.fetch(fts, headers = {"Authorization" : "Basic %s" % base64.b64encode(config.fts_username + ':' + config.fts_password)})
if response.status_code == 200:
results = json.loads(response.content)
topics = []
for num in results:
topics.append(GetKindByNum('Topic', num))
template_values['topics'] = topics
memcache.set('q::' + q_md5, topics, 86400)
except:
template_values['topics'] = []
else:
template_values['topics'] = topics
path = os.path.join(os.path.dirname(__file__), 'tpl', 'desktop', 'search.html')
output = template.render(path, template_values)
self.response.out.write(output)
class DispatcherHandler(webapp.RequestHandler):
def post(self):
referer = self.request.headers['Referer']
q = self.request.get('q').strip()
if len(q) > 0:
self.redirect('/q/' + q)
else:
self.redirect(referer)
class RouterHandler(webapp.RequestHandler):
def get(self, path):
if path.find('/') != -1:
# Page
parts = path.split('/')
if len(parts) == 2:
minisite_name = parts[0]
if parts[1] == '':
page_name = 'index.html'
else:
page_name = parts[1]
minisite = GetKindByName('Minisite', minisite_name)
if minisite is not False:
page = memcache.get(path)
if page is None:
q = db.GqlQuery("SELECT * FROM Page WHERE name = :1 AND minisite = :2", page_name, minisite)