forked from livid/v2ex-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
favorite.py
211 lines (196 loc) · 8.73 KB
/
favorite.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
#!/usr/bin/env python
# coding=utf-8
import os
import re
import time
import datetime
import hashlib
import string
import random
from google.appengine.ext import webapp
from google.appengine.api import memcache
from google.appengine.ext import db
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.api.labs import taskqueue
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 Note
from v2ex.babel import NodeBookmark
from v2ex.babel import TopicBookmark
from v2ex.babel import MemberBookmark
from v2ex.babel import SYSTEM_VERSION
from v2ex.babel.security import *
from v2ex.babel.ua import *
from v2ex.babel.da import *
from v2ex.babel.ext.cookies import Cookies
from v2ex.babel.ext.sessions import Session
class FavoriteNodeHandler(webapp.RequestHandler):
def get(self, node_name):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
node = GetKindByName('Node', node_name)
if node is not False:
q = db.GqlQuery("SELECT * FROM NodeBookmark WHERE node = :1 AND member = :2", node, member)
if q.count() == 0:
bookmark = NodeBookmark(parent=member)
bookmark.node = node
bookmark.member = member
bookmark.put()
member = db.get(member.key())
member.favorited_nodes = member.favorited_nodes + 1
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/n' + str(node.num) + '/m' + str(member.num)
memcache.set(n, True, 86400 * 14)
self.redirect(go)
class UnfavoriteNodeHandler(webapp.RequestHandler):
def get(self, node_name):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
node = GetKindByName('Node', node_name)
if node is not False:
q = db.GqlQuery("SELECT * FROM NodeBookmark WHERE node = :1 AND member = :2", node, member)
if q.count() > 0:
bookmark = q[0]
bookmark.delete()
member = db.get(member.key())
member.favorited_nodes = member.favorited_nodes - 1
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/n' + str(node.num) + '/m' + str(member.num)
memcache.delete(n)
self.redirect(go)
class FavoriteTopicHandler(webapp.RequestHandler):
def get(self, topic_num):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
topic = GetKindByNum('Topic', int(topic_num))
if topic is not False:
q = db.GqlQuery("SELECT * FROM TopicBookmark WHERE topic = :1 AND member = :2", topic, member)
if q.count() == 0:
bookmark = TopicBookmark(parent=member)
bookmark.topic = topic
bookmark.member = member
bookmark.put()
member = db.get(member.key())
member.favorited_topics = member.favorited_topics + 1
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/t' + str(topic.num) + '/m' + str(member.num)
memcache.set(n, True, 86400 * 14)
taskqueue.add(url='/add/star/topic/' + str(topic.key()))
self.redirect(go)
class UnfavoriteTopicHandler(webapp.RequestHandler):
def get(self, topic_num):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
topic = GetKindByNum('Topic', int(topic_num))
if topic is not False:
q = db.GqlQuery("SELECT * FROM TopicBookmark WHERE topic = :1 AND member = :2", topic, member)
if q.count() > 0:
bookmark = q[0]
bookmark.delete()
member = db.get(member.key())
member.favorited_topics = member.favorited_topics - 1
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/t' + str(topic.num) + '/m' + str(member.num)
memcache.delete(n)
taskqueue.add(url='/minus/star/topic/' + str(topic.key()))
self.redirect(go)
class FollowMemberHandler(webapp.RequestHandler):
def get(self, one_num):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
one = GetKindByNum('Member', int(one_num))
if one is not False:
if one.num != member.num:
q = db.GqlQuery("SELECT * FROM MemberBookmark WHERE one = :1 AND member_num = :2", one, member.num)
if q.count() == 0:
member = db.get(member.key())
member.favorited_members = member.favorited_members + 1
if member.favorited_members > 30:
self.session = Session()
self.session['message'] = '最多只能添加 30 位特别关注'
else:
bookmark = MemberBookmark(parent=member)
bookmark.one = one
bookmark.member_num = member.num
bookmark.put()
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/m' + str(one.num) + '/m' + str(member.num)
memcache.set(n, True, 86400 * 14)
one = db.get(one.key())
one.followers_count = one.followers_count + 1
one.put()
memcache.set('Member_' + str(one.num), one, 86400)
memcache.set('Member::' + str(one.username_lower), one, 86400)
self.session = Session()
self.session['message'] = '特别关注添加成功,还可以添加 ' + str(30 - member.favorited_members) + ' 位'
self.redirect(go)
class UnfollowMemberHandler(webapp.RequestHandler):
def get(self, one_num):
if 'Referer' in self.request.headers:
go = self.request.headers['Referer']
else:
go = '/'
member = CheckAuth(self)
if member:
one = GetKindByNum('Member', int(one_num))
if one is not False:
if one.num != member.num:
q = db.GqlQuery("SELECT * FROM MemberBookmark WHERE one = :1 AND member_num = :2", one, member.num)
if q.count() > 0:
bookmark = q[0]
bookmark.delete()
member = db.get(member.key())
member.favorited_members = member.favorited_members - 1
member.put()
memcache.set('Member_' + str(member.num), member, 86400)
n = 'r/m' + str(one.num) + '/m' + str(member.num)
memcache.delete(n)
one = db.get(one.key())
one.followers_count = one.followers_count - 1
one.put()
memcache.set('Member_' + str(one.num), one, 86400)
memcache.set('Member::' + str(one.username_lower), one, 86400)
self.redirect(go)
def main():
application = webapp.WSGIApplication([
('/favorite/node/([a-zA-Z0-9]+)', FavoriteNodeHandler),
('/unfavorite/node/([a-zA-Z0-9]+)', UnfavoriteNodeHandler),
('/favorite/topic/([0-9]+)', FavoriteTopicHandler),
('/unfavorite/topic/([0-9]+)', UnfavoriteTopicHandler),
('/follow/([0-9]+)', FollowMemberHandler),
('/unfollow/([0-9]+)', UnfollowMemberHandler)
],
debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()